Matplotlib
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
「[[.NET 開発基盤部会 Wiki>http://dotnetdevelopmentinfras...
-[[戻る>Python]] > [[Pythonセカンド・ステップ]]
--[[NumPy]]
--[[Pandas]]
--Matplotlib
*目次 [#l835e365]
#contents
*概要 [#o6feac6a]
Matplotlib(マットプロットリブ)は二次元の平面にグラフを...
-オブジェクト指向のAPIを提供しており、様々な種類のグラフ...
-描画できるのは主に2次元のプロットだが、3次元プロットの機...
-BSDスタイルのライセンスの下で配布されている。
*準備 [#d9d250e4]
**インストール [#mf318e02]
***matplotlib [#oad82f0e]
>pip install matplotlib
***seaborn [#ff60ac06]
matplotlibラッパ
>pip install seaborn
**インポート [#gf0d310a]
***別名 [#xdb01dc1]
numpyも一緒にインポート
>>>import numpy as np
>>>import matplotlib.pyplot as plt
>>>import seaborn as sns
***jupyter上で利用するとき [#abbbf4d4]
>>>%matplotlib inline
*ファースト・ステップ [#we6a680f]
**ヒストグラム [#dd9449c5]
***データの生成 [#nc49bae7]
-最も単純な例
x = np.random.normal(70, 10, 1000) # 平均70・標準偏差10...
-階層別表示の例
y=[0,1,2]
# valのベクトルを生成し、n行1列の2次元配列(行列)化
y[0]=np.random.normal(50, 10, 1000).reshape(-1, 1) # 平...
y[1]=np.random.normal(150, 20, 1000).reshape(-1, 1) # 平...
y[2]=np.random.normal(100, 30, 1000).reshape(-1, 1) # 平...
# clsのベクトルを生成し、n行1列の2次元配列(行列)化...
y[0]=np.hstack([y[0], np.full(1000, 0).reshape(-1, 1)])
y[1]=np.hstack([y[1], np.full(1000, 1).reshape(-1, 1)])
y[2]=np.hstack([y[2], np.full(1000, 2).reshape(-1, 1)])
# numpy.ndarray(行列)をDataFrame化
y[0]=pd.DataFrame(y[0],columns=['val','cls'])
y[1]=pd.DataFrame(y[1],columns=['val','cls'])
y[2]=pd.DataFrame(y[2],columns=['val','cls'])
# 3つのclsのDataFrameを結合
df=y[0]
df=pd.concat([df,y[1]],axis=0)
df=pd.concat([df,y[2]],axis=0)
# インデックス再設定
df.index=np.arange(len(df))
***データの可視化 [#jb02e34e]
-最も単純な例
--binsの自動算出~
>>>plt.hist(x)
>>>plt.show()
--最適なbinsを算出 ~
スタージェスの公式(1+log2n)を基に最適なbinsを算出
>>>import math
>>>bins = math.log(len(x), 2) + 1
>>>plt.hist(x, bins=round(bins))
>>>plt.show()
-階層別表示の例~
cls列の値(階層)毎にグループ分けしてval値をヒストグラム...
for _class, _color in zip([1,2,3], ['r','g','b']):
plt.hist(df[df.cls==_class]['val'], bins=round(bins)...
plt.show()
**散布図 [#p8bdbbdb]
***データの生成 [#t51afa55]
線形単回帰している風のデータを生成する。
-データ1
>>>np.random.seed(0) # 乱数生成器の状態を指定
>>>x1=np.random.normal(50,10,100) # 平均50 標準偏差10の...
>>>y1=x1+np.random.normal(0,10,100) # 線形単回帰している...
>>>x2
...見ても解らん数字の羅列...
>>>y2
...見ても解らん数字の羅列...
-データ2~
より、相関係数が大きい感じのデータ。
>>>np.random.seed(0) # 乱数生成器の状態を指定
>>>x2=np.random.normal(30,10,100) # 平均30 標準偏差10の...
>>>y2=2*x2+np.random.normal(0,10,100) # 線形単回帰してい...
>>>x2
...見ても解らん数字の羅列...
>>>y2
...見ても解らん数字の羅列...
***データの可視化 [#n69cc525]
-可視化
>>>plt.scatter(x,y)
>>>plt.title('グラフのタイトル')
>>>plt.xlabel('x軸のラベル')
>>>plt.ylabel('y軸のラベル')
>>>plt.xticks(np.arange(下限,上限,間隔))
>>>plt.yticks(np.arange(下限,上限,間隔))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
-scatterメソッドのオプション
--プロットと凡例の色~
https://matplotlib.org/2.0.2/users/colors.html
color='blue'
--プロットのマーカー~
https://matplotlib.org/stable/api/markers_api.html
marker='x'
--凡例に表示する文字
label='data x'
--凡例を表示する場合、以下を実行
>>>plt.legend(loc='upper right') # 凡例位置
-例
--1つのデータ群
---最も単純な例
>>>plt.scatter(x1,y1)
>>>plt.show()
---修飾を加えた例
>>>plt.scatter(x1,y1)
>>>plt.title('title')
>>>plt.xlabel('xlabel')
>>>plt.ylabel('ylabel')
>>>plt.xticks(np.arange(0,100,10))
>>>plt.yticks(np.arange(0,100,10))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
--2つのデータ群
>>>plt.scatter(x1,y1,color='red',marker='x',label='data1')
>>>plt.scatter(x2,y2,color='blue',marker='+',label='data...
>>>plt.title('title')
>>>plt.xlabel('xlabel')
>>>plt.ylabel('ylabel')
>>>plt.legend(loc='upper right') # 凡例位置
>>>plt.xticks(np.arange(0,100,10))
>>>plt.yticks(np.arange(0,100,10))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
**三次関数グラフ [#s3ccebdc]
***データの生成 [#cfc43738]
コチラは関数なので、式(y=x^3+x^2x+x+1)からデータを生成...
>>>#x座標
>>>x=np.arange(-10,10,0.1) # -10 - +10まで0.1刻みの配列
>>>#x座標
>>>y=0.01*(x**3+x**2+x+1) # 三次関数:y=x^3+x^2x+x+1
***データの可視化 [#b69a4be4]
グラフのプロット
-最も単純な例
>>>plt.plot(x,y) # グラフをプロットする
>>>plt.show() # グラフを表示する
-修飾を加えた例~
[[前述の修飾項目>#n69cc525]]を除く
>>>plt.plot(x,y) # グラフをプロットする
>>>plt.hlines([0], -10, 10, linestyles='dashed', color='...
>>>plt.vlines([0], -1, 1, linestyles='dashed', color='gr...
>>>plt.xlim([-5, 5]) # x軸方向の表示範囲を指定
>>>plt.ylim([-1, 1]) # y軸方向の表示範囲を指定
>>>plt.show() # グラフを表示する
**三角関数グラフ [#o5bea06c]
***sin関数グラフ [#m6ad31a6]
sin関数からデータを生成する。
-データの生成
>>>#x座標
>>>x=np.arange(0,6,0.1) # 0 - 6まで0.1刻みの配列
>>>x
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, ...
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, ...
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, ...
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , ...
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, ...
5.5, 5.6, 5.7, 5.8, 5.9])
>>>#y座標
>>>y=np.sin(x)
>>>y
array([ 0. , 0.09983342, 0.19866933, 0.2955202...
0.47942554, 0.56464247, 0.64421769, 0.7173560...
0.84147098, 0.89120736, 0.93203909, 0.9635581...
0.99749499, 0.9995736 , 0.99166481, 0.9738476...
0.90929743, 0.86320937, 0.8084964 , 0.7457052...
0.59847214, 0.51550137, 0.42737988, 0.3349881...
0.14112001, 0.04158066, -0.05837414, -0.1577456...
-0.35078323, -0.44252044, -0.52983614, -0.6118578...
-0.7568025 , -0.81827711, -0.87157577, -0.9161659...
-0.97753012, -0.993691 , -0.99992326, -0.9961646...
-0.95892427, -0.92581468, -0.88345466, -0.8322674...
-0.70554033, -0.63126664, -0.55068554, -0.4646021...
-データの描画
>>>#グラフ描画
>>>plt.plot(x,y)
>>>plt.show()
#ref(sin_Matplotlib.png,left,nowrap,sin関数グラフ,60%)
***sin, cos関数グラフ [#m6ad31a6]
同様に、sin, cos関数からデータを生成する。
-データの生成
>>>#x座標
>>>x=np.arange(0,6,0.1) # 0 - 6まで0.1刻みの配列
>>>#y座標
>>>y1=np.sin(x)
>>>y2=np.cos(x)
-データの描画
>>>#グラフ描画
>>> plt.plot(x,y1, label="sin")
>>> plt.plot(x,y2, linestyle="--", label="cos")
>>> plt.xlabel("x")
>>> plt.ylabel("y")
>>> plt.title("sin & cos")
>>> plt.legend()
>>> plt.show()
#ref(SinAndCos_Matplotlib.png,left,nowrap,sin & cos関数グ...
*セカンド・ステップ [#v413b986]
**パレート図 [#oc3f2690]
結構手数が要る。
**散布図行列 [#a41dcf52]
matplotlibラッパであるseabornを使って、~
表の全ペアの[[ヒストグラムと散布図>統計解析#ff177fed]]を...
***データの生成 [#e4327406]
-準備~
scikit-learnのデータセットを使う
>pip install scikit-learn
-生成~
[[dfは数値表として初期化されたDataFrame>Pandas#w1afd0f6]]
from sklearn import datasets
iris = datasets.load_iris()
df_data = pd.DataFrame(iris.data, columns=iris.feature_n...
df_target = pd.DataFrame(iris.target, columns=['species'])
df = pd.concat([df_data, df_target], axis=1)
df.head()
***データの描画 [#b5eb3ea6]
-既定値
>>>sns.pairplot(df, height=2.0)
>>>plt.show()
-カテゴリ列値で色分け
>>>sns.pairplot(df, hue='カテゴリ列')
>>>plt.show()
**ヒートマップ [#eddbc4be]
***データの生成 [#ye1810a9]
[[dfは数値表として初期化されたDataFrame>Pandas#w1afd0f6]]。
# 10x12 の一様乱数を生成
np.random.seed(0)
uniform_data = np.random.rand(10, 12)
***データの描画 [#k8730ed6]
# 図表のサイズを指定
plt.figure(figsize=(10, 7))
# numpy.ndarray(行列)でもDataFrameでもOK
# annot : 数値を表示するかどうか(annotation)
# square: 四角を正方形に合わせるかどうか
# fmt : 表示する数値の形式(formatting)
sns.heatmap(uniform_data, annot=True, square=True, fmt='...
plt.show()
*サード・ステップ(その他) [#gafb135d]
**画像の表示 [#m63803cc]
>>>from matplotlib.image import imread
>>>img=imread('C:\Windows\Web\Wallpaper\Theme1\img1.jpg')
>>>plt.imshow(img)
>>>plt.show()
#ref(img_Matplotlib.png,left,nowrap,画像,60%)
**3次元のプロット [#fb63aada]
***準備 [#y0763b81]
インポート
# import
from mpl_toolkits.mplot3d import Axes3D
***追加 [#maa3a8ef]
-点
# (x, y, z)
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.scatter(x, y, z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
-線
--直線
# (x, y, z)
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = [1, 2, 3, 4, 5]
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot(x, y, z, "o-")
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
--曲線
# (x, y, z)
x = np.linspace(0, 8 * np.pi, num=50)
y = x * np.cos(x)
z = x * np.sin(x)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot(x, y, z, "o-")
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
-面
--平面
# (x, y, z)
# 2次元のグリッド座標
x, y = np.meshgrid(np.arange(0, 40, 2.5), np.arange(1, 1...
# 平面の式
z = 2*x + 3*y + 4
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot_wireframe(x,y,z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
--曲面
# (x, y, z)
# 2次元のグリッド座標
x, y = np.meshgrid(np.arange(-3, 3, 0.25), np.arange(-3,...
# 曲面の式
z = np.sin(x)+ np.cos(y)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot_wireframe(x,y,z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
*参考 [#d52305ac]
-matplotlib - Wikipedia~
https://ja.wikipedia.org/wiki/Matplotlib
-DxCommon/MatplotlibTraining.ipynb at develop · OpenToury...
https://github.com/OpenTouryoProject/DxCommon/blob/develo...
終了行:
「[[.NET 開発基盤部会 Wiki>http://dotnetdevelopmentinfras...
-[[戻る>Python]] > [[Pythonセカンド・ステップ]]
--[[NumPy]]
--[[Pandas]]
--Matplotlib
*目次 [#l835e365]
#contents
*概要 [#o6feac6a]
Matplotlib(マットプロットリブ)は二次元の平面にグラフを...
-オブジェクト指向のAPIを提供しており、様々な種類のグラフ...
-描画できるのは主に2次元のプロットだが、3次元プロットの機...
-BSDスタイルのライセンスの下で配布されている。
*準備 [#d9d250e4]
**インストール [#mf318e02]
***matplotlib [#oad82f0e]
>pip install matplotlib
***seaborn [#ff60ac06]
matplotlibラッパ
>pip install seaborn
**インポート [#gf0d310a]
***別名 [#xdb01dc1]
numpyも一緒にインポート
>>>import numpy as np
>>>import matplotlib.pyplot as plt
>>>import seaborn as sns
***jupyter上で利用するとき [#abbbf4d4]
>>>%matplotlib inline
*ファースト・ステップ [#we6a680f]
**ヒストグラム [#dd9449c5]
***データの生成 [#nc49bae7]
-最も単純な例
x = np.random.normal(70, 10, 1000) # 平均70・標準偏差10...
-階層別表示の例
y=[0,1,2]
# valのベクトルを生成し、n行1列の2次元配列(行列)化
y[0]=np.random.normal(50, 10, 1000).reshape(-1, 1) # 平...
y[1]=np.random.normal(150, 20, 1000).reshape(-1, 1) # 平...
y[2]=np.random.normal(100, 30, 1000).reshape(-1, 1) # 平...
# clsのベクトルを生成し、n行1列の2次元配列(行列)化...
y[0]=np.hstack([y[0], np.full(1000, 0).reshape(-1, 1)])
y[1]=np.hstack([y[1], np.full(1000, 1).reshape(-1, 1)])
y[2]=np.hstack([y[2], np.full(1000, 2).reshape(-1, 1)])
# numpy.ndarray(行列)をDataFrame化
y[0]=pd.DataFrame(y[0],columns=['val','cls'])
y[1]=pd.DataFrame(y[1],columns=['val','cls'])
y[2]=pd.DataFrame(y[2],columns=['val','cls'])
# 3つのclsのDataFrameを結合
df=y[0]
df=pd.concat([df,y[1]],axis=0)
df=pd.concat([df,y[2]],axis=0)
# インデックス再設定
df.index=np.arange(len(df))
***データの可視化 [#jb02e34e]
-最も単純な例
--binsの自動算出~
>>>plt.hist(x)
>>>plt.show()
--最適なbinsを算出 ~
スタージェスの公式(1+log2n)を基に最適なbinsを算出
>>>import math
>>>bins = math.log(len(x), 2) + 1
>>>plt.hist(x, bins=round(bins))
>>>plt.show()
-階層別表示の例~
cls列の値(階層)毎にグループ分けしてval値をヒストグラム...
for _class, _color in zip([1,2,3], ['r','g','b']):
plt.hist(df[df.cls==_class]['val'], bins=round(bins)...
plt.show()
**散布図 [#p8bdbbdb]
***データの生成 [#t51afa55]
線形単回帰している風のデータを生成する。
-データ1
>>>np.random.seed(0) # 乱数生成器の状態を指定
>>>x1=np.random.normal(50,10,100) # 平均50 標準偏差10の...
>>>y1=x1+np.random.normal(0,10,100) # 線形単回帰している...
>>>x2
...見ても解らん数字の羅列...
>>>y2
...見ても解らん数字の羅列...
-データ2~
より、相関係数が大きい感じのデータ。
>>>np.random.seed(0) # 乱数生成器の状態を指定
>>>x2=np.random.normal(30,10,100) # 平均30 標準偏差10の...
>>>y2=2*x2+np.random.normal(0,10,100) # 線形単回帰してい...
>>>x2
...見ても解らん数字の羅列...
>>>y2
...見ても解らん数字の羅列...
***データの可視化 [#n69cc525]
-可視化
>>>plt.scatter(x,y)
>>>plt.title('グラフのタイトル')
>>>plt.xlabel('x軸のラベル')
>>>plt.ylabel('y軸のラベル')
>>>plt.xticks(np.arange(下限,上限,間隔))
>>>plt.yticks(np.arange(下限,上限,間隔))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
-scatterメソッドのオプション
--プロットと凡例の色~
https://matplotlib.org/2.0.2/users/colors.html
color='blue'
--プロットのマーカー~
https://matplotlib.org/stable/api/markers_api.html
marker='x'
--凡例に表示する文字
label='data x'
--凡例を表示する場合、以下を実行
>>>plt.legend(loc='upper right') # 凡例位置
-例
--1つのデータ群
---最も単純な例
>>>plt.scatter(x1,y1)
>>>plt.show()
---修飾を加えた例
>>>plt.scatter(x1,y1)
>>>plt.title('title')
>>>plt.xlabel('xlabel')
>>>plt.ylabel('ylabel')
>>>plt.xticks(np.arange(0,100,10))
>>>plt.yticks(np.arange(0,100,10))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
--2つのデータ群
>>>plt.scatter(x1,y1,color='red',marker='x',label='data1')
>>>plt.scatter(x2,y2,color='blue',marker='+',label='data...
>>>plt.title('title')
>>>plt.xlabel('xlabel')
>>>plt.ylabel('ylabel')
>>>plt.legend(loc='upper right') # 凡例位置
>>>plt.xticks(np.arange(0,100,10))
>>>plt.yticks(np.arange(0,100,10))
>>>plt.grid() # グリッド線を表示
>>>plt.show()
**三次関数グラフ [#s3ccebdc]
***データの生成 [#cfc43738]
コチラは関数なので、式(y=x^3+x^2x+x+1)からデータを生成...
>>>#x座標
>>>x=np.arange(-10,10,0.1) # -10 - +10まで0.1刻みの配列
>>>#x座標
>>>y=0.01*(x**3+x**2+x+1) # 三次関数:y=x^3+x^2x+x+1
***データの可視化 [#b69a4be4]
グラフのプロット
-最も単純な例
>>>plt.plot(x,y) # グラフをプロットする
>>>plt.show() # グラフを表示する
-修飾を加えた例~
[[前述の修飾項目>#n69cc525]]を除く
>>>plt.plot(x,y) # グラフをプロットする
>>>plt.hlines([0], -10, 10, linestyles='dashed', color='...
>>>plt.vlines([0], -1, 1, linestyles='dashed', color='gr...
>>>plt.xlim([-5, 5]) # x軸方向の表示範囲を指定
>>>plt.ylim([-1, 1]) # y軸方向の表示範囲を指定
>>>plt.show() # グラフを表示する
**三角関数グラフ [#o5bea06c]
***sin関数グラフ [#m6ad31a6]
sin関数からデータを生成する。
-データの生成
>>>#x座標
>>>x=np.arange(0,6,0.1) # 0 - 6まで0.1刻みの配列
>>>x
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, ...
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, ...
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, ...
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , ...
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, ...
5.5, 5.6, 5.7, 5.8, 5.9])
>>>#y座標
>>>y=np.sin(x)
>>>y
array([ 0. , 0.09983342, 0.19866933, 0.2955202...
0.47942554, 0.56464247, 0.64421769, 0.7173560...
0.84147098, 0.89120736, 0.93203909, 0.9635581...
0.99749499, 0.9995736 , 0.99166481, 0.9738476...
0.90929743, 0.86320937, 0.8084964 , 0.7457052...
0.59847214, 0.51550137, 0.42737988, 0.3349881...
0.14112001, 0.04158066, -0.05837414, -0.1577456...
-0.35078323, -0.44252044, -0.52983614, -0.6118578...
-0.7568025 , -0.81827711, -0.87157577, -0.9161659...
-0.97753012, -0.993691 , -0.99992326, -0.9961646...
-0.95892427, -0.92581468, -0.88345466, -0.8322674...
-0.70554033, -0.63126664, -0.55068554, -0.4646021...
-データの描画
>>>#グラフ描画
>>>plt.plot(x,y)
>>>plt.show()
#ref(sin_Matplotlib.png,left,nowrap,sin関数グラフ,60%)
***sin, cos関数グラフ [#m6ad31a6]
同様に、sin, cos関数からデータを生成する。
-データの生成
>>>#x座標
>>>x=np.arange(0,6,0.1) # 0 - 6まで0.1刻みの配列
>>>#y座標
>>>y1=np.sin(x)
>>>y2=np.cos(x)
-データの描画
>>>#グラフ描画
>>> plt.plot(x,y1, label="sin")
>>> plt.plot(x,y2, linestyle="--", label="cos")
>>> plt.xlabel("x")
>>> plt.ylabel("y")
>>> plt.title("sin & cos")
>>> plt.legend()
>>> plt.show()
#ref(SinAndCos_Matplotlib.png,left,nowrap,sin & cos関数グ...
*セカンド・ステップ [#v413b986]
**パレート図 [#oc3f2690]
結構手数が要る。
**散布図行列 [#a41dcf52]
matplotlibラッパであるseabornを使って、~
表の全ペアの[[ヒストグラムと散布図>統計解析#ff177fed]]を...
***データの生成 [#e4327406]
-準備~
scikit-learnのデータセットを使う
>pip install scikit-learn
-生成~
[[dfは数値表として初期化されたDataFrame>Pandas#w1afd0f6]]
from sklearn import datasets
iris = datasets.load_iris()
df_data = pd.DataFrame(iris.data, columns=iris.feature_n...
df_target = pd.DataFrame(iris.target, columns=['species'])
df = pd.concat([df_data, df_target], axis=1)
df.head()
***データの描画 [#b5eb3ea6]
-既定値
>>>sns.pairplot(df, height=2.0)
>>>plt.show()
-カテゴリ列値で色分け
>>>sns.pairplot(df, hue='カテゴリ列')
>>>plt.show()
**ヒートマップ [#eddbc4be]
***データの生成 [#ye1810a9]
[[dfは数値表として初期化されたDataFrame>Pandas#w1afd0f6]]。
# 10x12 の一様乱数を生成
np.random.seed(0)
uniform_data = np.random.rand(10, 12)
***データの描画 [#k8730ed6]
# 図表のサイズを指定
plt.figure(figsize=(10, 7))
# numpy.ndarray(行列)でもDataFrameでもOK
# annot : 数値を表示するかどうか(annotation)
# square: 四角を正方形に合わせるかどうか
# fmt : 表示する数値の形式(formatting)
sns.heatmap(uniform_data, annot=True, square=True, fmt='...
plt.show()
*サード・ステップ(その他) [#gafb135d]
**画像の表示 [#m63803cc]
>>>from matplotlib.image import imread
>>>img=imread('C:\Windows\Web\Wallpaper\Theme1\img1.jpg')
>>>plt.imshow(img)
>>>plt.show()
#ref(img_Matplotlib.png,left,nowrap,画像,60%)
**3次元のプロット [#fb63aada]
***準備 [#y0763b81]
インポート
# import
from mpl_toolkits.mplot3d import Axes3D
***追加 [#maa3a8ef]
-点
# (x, y, z)
x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.scatter(x, y, z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
-線
--直線
# (x, y, z)
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = [1, 2, 3, 4, 5]
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot(x, y, z, "o-")
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
--曲線
# (x, y, z)
x = np.linspace(0, 8 * np.pi, num=50)
y = x * np.cos(x)
z = x * np.sin(x)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot(x, y, z, "o-")
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
-面
--平面
# (x, y, z)
# 2次元のグリッド座標
x, y = np.meshgrid(np.arange(0, 40, 2.5), np.arange(1, 1...
# 平面の式
z = 2*x + 3*y + 4
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot_wireframe(x,y,z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
--曲面
# (x, y, z)
# 2次元のグリッド座標
x, y = np.meshgrid(np.arange(-3, 3, 0.25), np.arange(-3,...
# 曲面の式
z = np.sin(x)+ np.cos(y)
# 図表の定義
fig = plt.figure()
#ax = Axes3D(fig)
ax = fig.add_subplot(projection='3d')
# 3Dでプロット
ax.plot_wireframe(x,y,z)
# ラベル
ax.set_xlabel('X-label')
ax.set_ylabel('Y-label')
ax.set_zlabel('Z-label')
*参考 [#d52305ac]
-matplotlib - Wikipedia~
https://ja.wikipedia.org/wiki/Matplotlib
-DxCommon/MatplotlibTraining.ipynb at develop · OpenToury...
https://github.com/OpenTouryoProject/DxCommon/blob/develo...
ページ名: