データが与えられた時、まずは可視化してデータの特徴を把握することが大切です。しかし、何を軸にしてどのように可視化するのかということに関しては、あまりルール化されていないのが現状だと思います。
データから何を知りたいのか?ということから、パターン別にどのように可視化したらいいのかということをチートシート形式で示し、さらにpythonでの可視化方法を順に紹介していきたいと思います。
![Screen Shot 2018-06-06 at 14.29.04.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F8332e817-92a0-7848-6d83-397b04f0c75f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=fc03b48284ddc5464b5ab801849f88bd)
上のチートシートを参考に、
- Distribution|分布
- Composition|構成
- Relationship|関係
- Comparison|比較
の4つの項目に分けて、どのようなデータパターンではどのように可視化するとわかりやすいか、pythonではどのように実装するのかを記していきます。
準備
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
plt.style.use('ggplot')
plt.rcParams.update({'font.size':15})
%matplotlib inline
可視化には主に、matplotlib.pyplot
、seaborn
を使います。seabornに馴染みのない方は、この記事を見るといいと思います。
Distribution|分布
![Screen Shot 2018-06-06 at 17.57.06.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fc2c9c477-0707-2f1f-6cfe-136478c3f25a.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c15dda090f93d43e0cf940bf30f3c934)
1変数の分布
データポイントが数個の場合|ヒストグラム
x = np.random.normal(size = 1000)
plt.hist(x)
plt.show()
![Screen Shot 2018-06-06 at 17.55.10.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F1a17f91e-6489-7b93-308c-fc6fb949f81b.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=938adee2f495da8f817a343196b1a30d)
データポイントが多い場合|線グラフヒストグラム
x = np.random.normal(size = 1000)
# ここのみ、seabornを用いて可視化
sns.distplot(x)
![Screen Shot 2018-06-06 at 17.55.15.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fcad26a2f-42dc-c6a4-4cd0-e6109772679f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f78140830d85811ddcf531132f2b63f3)
2変数の分布|散布図
x = range(1, 101)
y = np.random.randn(100)*15 + range(1, 101)
plt.scatter(x, y)
plt.show()
![Screen Shot 2018-06-06 at 17.55.21.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Ffeb3f926-612a-1715-cd65-bdcda8a887ca.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=add43cd2136e0c3514d92ff4a0885b05)
Composition|構成
![Screen Shot 2018-06-06 at 17.59.41.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fa3c22513-21c3-1ed3-d42b-d5a0d3ba1ceb.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=e6e2b66105050e8ba8b0ec92e7e9e033)
時間ごとの構成
時間軸が複数個の場合
割合のみを見たい場合|累積棒グラフ
x = [1, 2, 3, 4, 5]
y1 = [80, 60, 40, 20, 5]
y2 = [20, 40, 60, 80, 95]
plt.bar(x, y1)
plt.bar(x, y2, bottom=y1)
![Screen Shot 2018-06-06 at 18.04.21.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fb10ac7c2-67cc-dd1f-4209-a6c088bfe982.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=07471cf9ad6a76df44b292404517dc66)
割合と値を両方見たい場合|棒グラフ
x = [1, 2, 3, 4, 5]
y1 = [100, 200, 300, 400, 500]
y2 = [1000, 800, 600, 400, 200]
plt.bar(x, y1)
plt.bar(x, y2, bottom=y1)
![Screen Shot 2018-06-06 at 18.04.26.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fe4a71ca0-a3a7-482c-332a-03a63932b54a.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c29d635a390f4c86766f98832c3e869f)
時間軸が多い場合
割合のみを見たい場合|累積エリアチャート
x = range(1, 7)
y1 = [0, 20, 40, 30, 40, 100]
y2 = [40, 50, 20, 40, 55, 0]
y3 = [60, 30, 40, 30, 5, 0]
plt.stackplot(x, y1, y2, y3)
plt.show()
![Screen Shot 2018-06-06 at 18.04.31.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F1d30e3c8-4f02-c0d8-cbf2-0098f3c2b397.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c9159ac6c1c4bea6fcd2c2425380b976)
割合と値自体を両方見たい場合|エリアチャート
x=range(1, 7)
y1=[1, 4, 6, 8, 9, 3]
y2=[2, 2, 7, 10, 12, 10]
y3=[2, 8, 5, 10, 6, 8]
plt.stackplot(x,y1, y2, y3)
plt.show()
![Screen Shot 2018-06-06 at 18.04.35.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fab2fdd9e-8391-63b9-d382-f0514dcfa100.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=50c41e2b983dce4c445921630e46cff8)
静的カテゴリごとの構成
全体に占める割合を見たい場合|円グラフ
x = [100, 200, 300, 400, 500]
label = ["A", "B", "C", "D", "E"]
plt.pie(x, labels=label)
plt.axis('equal') # 出力が楕円形になるのを防ぐため
plt.show()
![Screen Shot 2018-06-06 at 18.04.39.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Ff3f1ce03-cd22-36f5-cb69-9cd5aec90209.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=0042fb66b06b2f794c0f4146749f1736)
構成の構成を見たい場合|累積棒グラフ
x = [1, 2, 3, 4, 5]
y1 = [80, 60, 40, 20, 5]
y2 = [20, 40, 60, 80, 95]
plt.bar(x, y1)
plt.bar(x, y2, bottom=y1)
![Screen Shot 2018-06-06 at 18.04.21.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fe65838a3-eb85-485a-21a0-7025e6f8bf24.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=11c93b98b54693c263c9fa3d9c471628)
全体に対する累積値と値自体を見たい場合|ツリーマップ
import squarify
x = [13,22,35,5]
label = ["A", "B", "C", "D"]
squarify.plot(x, label=label)
plt.axis('off')
plt.show()
![Screen Shot 2018-06-06 at 18.04.43.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fc28792a9-88f8-7622-c9be-05cd7c59f180.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=9ee7dea16a097d5842b286227e9cfd13)
Relationship|関係
![Screen Shot 2018-06-06 at 18.09.25.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Ff6a6aca1-bde0-04d2-18e7-3cdb9e820cbb.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f483e0c3816b7ee37e511ae3953dcd60)
変数が2つの場合|散布図
x = range(1, 101)
y = np.random.randn(100)*15 + range(1, 101)
plt.scatter(x, y)
plt.show()
![Screen Shot 2018-06-06 at 18.09.45.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fb3cf9ac1-8d3c-7289-4399-08e08d4ae409.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f227324f7b0eb02cc4e9453721143b0f)
変数が3つの場合|散布図
x = np.random.rand(40)
y = np.random.rand(40)
z = np.random.rand(40)
plt.scatter(x, y, s=z*1000, alpha=0.5)
plt.show()
![Screen Shot 2018-06-06 at 18.09.49.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F4f9de2d9-0479-0208-749a-4384cbd4a7e9.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=2944e326813efad5479c58e7e17e38db)
Comparison|比較
![Screen Shot 2018-06-06 at 18.09.32.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F66ad3eef-d00a-5eee-d4ca-7985b1e77e02.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f56c73aa9c0d668b0a6c989f6c59d91d)
アイテム間の比較
アイテムごとの1変数を比較したい場合
カテゴリが少ない場合|縦棒グラフ・横棒グラフ
x = [1, 2, 3, 4, 5]
y = [80, 60, 40, 20, 5]
plt.bar(x, y)
plt.show()
![Screen Shot 2018-06-06 at 18.10.03.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fca0f8959-bc8b-b7b5-8448-12b6fc06f423.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=d3907405421605318d9a6055949b4773)
x = [1, 2, 3, 4, 5]
y = [80, 60, 40, 20, 5]
plt.barh(x, y)
plt.show()
![Screen Shot 2018-06-06 at 18.09.58.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fc401b631-30c0-2749-b4cf-b6d7b9110f2f.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=6a4865446e3c18c8e04e3e47294d3237)
カテゴリが多い場合
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue="species", size=2.5)
![Screen Shot 2018-06-06 at 18.25.40.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F68983516-2b6e-1fd0-e24f-dd1fedf06de3.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=08aa1be19f4f580f12c2eb1f488d3a14)
アイテムごとの2変数を比較したい場合|棒グラフ
y = [3, 12, 5, 18, 45]
x = ('A', 'B', 'C', 'D', 'E')
x_width = [0.1, 0.2, 3, 1.5, 0.3]
y_pos = [0, 0.3, 2, 4.5, 5.5]
plt.bar(y_pos, y, width=x_width)
plt.xticks(y_pos, x)
plt.show()
![Screen Shot 2018-06-06 at 18.09.53.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Ff7421301-f0ce-81d0-f7a9-4da6013e7f03.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c9f84f859c0e9fed6cd142ba6dd5250e)
時間軸による比較
時間軸が少ない場合
カテゴリが1つもしくは数個の場合|棒グラフ
x = [1, 2, 3, 4, 5]
y = [80, 60, 40, 20, 5]
plt.bar(x, y)
plt.show()
![Screen Shot 2018-06-06 at 18.10.03.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fdb19b474-0bf8-e52b-3175-51b3b9ddeb59.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c634cef2e8ed2e0a6f50348c740369be)
カテゴリが多い場合|線グラフ
x = range(1,11)
y1 = np.random.randn(10)
y2 = np.random.randn(10)+range(1,11)
y3 = np.random.randn(10)+range(11,21)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.show()
![Screen Shot 2018-06-06 at 18.10.25.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2F80a7d20d-aeb3-05d7-f0eb-6e4b975b1a65.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ccd32e724a480327ef7dfa3538b0cec3)
時間軸が多い場合
非循環データの場合|線グラフ
y = np.cumsum(np.random.randn(1000,1))
plt.plot(y)
![Screen Shot 2018-06-06 at 18.10.20.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F108729%2Fbf14a730-5cfc-8c74-039b-1a8b4af8aed8.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ea5c9780eaa22482738937f6471f9795)