あなたが表示されるビジュアルオブジェクトのPython言語のmatplotlibのをseabornライブラリや選択を使用することができ、データ分析チャートに使用されます。
、matplotlibの
matplotlibのPythonはmatplotlibのことで、開発者は図面、ヒストグラム、パワースペクトル、バー、エラー、散布などを生成することができ、わずか数行のコードを必要とすることができ、2D描画ライブラリです。
-
出版品質のグラフを作成するためのツールライブラリを描きます
-
目的は、MatlabのインターフェースはPythonを描く式を構築することです
-
import matplotlib.pyplot as plt
-
pyployモジュールは、共通のmatplotlibのAPI関数が含まれています
フィギュア
-
matplotlibのフィギュアの画像オブジェクトが配置されています
-
図を作成します。
fig = plt.figure()
サンプルコード:
# 引入matplotlib包
import matplotlib.pyplot as plt
import numpy as np %matplotlib inline #在jupyter notebook 里需要使用这一句命令 # 创建figure对象 fig = plt.figure()
結果:
<matplotlib.figure.Figure at 0x11a2dd7b8>
サブプロット
fig.add_subplot(A、B、C)
-
A、Bは、領域* Bに分割された図を示しています
-
Cは、操作される現在選択されている領域を表します
-
注:1(0ではない)からの番号
-
サブプロットの最後の位置指定された領域を描画プロット(jupyterのノートブックが正しく表示されません)
サンプルコード:
# 指定切分区域的位置
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) # 在subplot上作图 random_arr = np.random.randn(100) #print random_arr # 默认是在最后一次使用subplot的位置上作图,但是在jupyter notebook 里可能显示有误 plt.plot(random_arr) # 可以指定在某个或多个subplot位置上作图 # ax1 = fig.plot(random_arr) # ax2 = fig.plot(random_arr) # ax3 = fig.plot(random_arr) # 显示绘图结果 plt.show()
結果:
ヒストグラム:HIST
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3) plt.show()
結果:
散布:散布
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
# 绘制散点图 x = np.arange(50) y = x + 5 * np.random.rand(50) plt.scatter(x, y) plt.show()
結果:
ヒストグラム:バー
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
# 柱状图 x = np.arange(5) y1, y2 = np.random.randint(1, 25, size=(2, 5)) width = 0.25 ax = plt.subplot(1,1,1) ax.bar(x, y1, width, color='r') ax.bar(x+width, y2, width, color='g') ax.set_xticks(x+width) ax.set_xticklabels(['a', 'b', 'c', 'd', 'e']) plt.show()
結果:
描画マトリックス:plt.imshow()
- 混同マトリックスの三次元との関係
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
# 矩阵绘图 m = np.random.rand(10,10) print(m) plt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean) plt.colorbar() plt.show()
結果:
[[ 0.92859942 0.84162134 0.37814667 0.46401549 0.93935737 0.0344159 0.56358375 0.75977745 0.87983192 0.22818774] [ 0.88216959 0.43369207 0.1303902 0.98446182 0.59474031 0.04414217 0.86534444 0.34919228 0.53950028 0.89165269] [ 0.52919761 0.87408715 0.097871 0.78348534 0.09354791 0.3186 0.25978432 0.48340623 0.1107699 0.14065592] [ 0.90834516 0.42377475 0.73042695 0.51596826 0.14154431 0.22165693 0.64705882 0.78062873 0.55036304 0.40874584] [ 0.98853697 0.46762114 0.69973423 0.7910757 0.63700306 0.68793919 0.28685306 0.3473426 0.17011744 0.18812329] [ 0.73688943 0.58004874 0.03146167 0.08875797 0.32930191 0.87314734 0.50757536 0.8667078 0.8423364 0.99079049] [ 0.37660356 0.63667774 0.78111565 0.25598593 0.38437628 0.95771051 0.01922366 0.37020219 0.51020305 0.05365718] [ 0.87588452 0.56494761 0.67320078 0.46870376 0.66139913 0.55072149 0.51328222 0.64817353 0.198525 0.18105368] [ 0.86038137 0.55914088 0.55240021 0.15260395 0.4681218 0.28863395 0.6614597 0.69015592 0.46583629 0.15086562] [ 0.01373772 0.30514083 0.69804049 0.5014782 0.56855904 0.14889117 0.87596848 0.29757133 0.76062891 0.03678431]]
plt.subplots()
-
そして、新しく作成された返す
figure
と、subplot
オブジェクトの配列を -
2行2列のサブプロットを生成します:
fig, subplot_arr = plt.subplots(2,2)
-
複数のグラフを作成するためにこれを使用するために適切jupyterでは、推奨される方法
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
fig, subplot_arr = plt.subplots(2,2) # bins 为显示个数,一般小于等于数值个数 subplot_arr[1,0].hist(np.random.randn(100), bins=10, color='b', alpha=0.3) plt.show()
結果:
色、マーキング、リニア
- ax.plot(X、Y、 'r--の')
ax.plotに相当する( ' - X'、Y、ラインスタイル=色= 'R')
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(2) axes[0].plot(np.random.randint(0, 100, 50), 'ro--') # 等价 axes[1].plot(np.random.randint(0, 100, 50), color='r', linestyle='dashed', marker='o')
結果:
[<matplotlib.lines.Line2D at 0x11a901e80>]
- 共通の色、マーク、リニア
マーク、ラベル、伝説
-
セットスケール範囲
plt.xlim()、plt.ylim()
ax.set_xlim()、ax.set_ylim()
-
スケール表示を設定します。
plt.xticks()、plt.yticks()
ax.set_xticks()、ax.set_yticks()
-
設定スケールのラベル
ax.set_xticklabels()、ax.set_yticklabels()
-
設定し軸ラベル
ax.set_xlabel()、ax.set_ylabel()
-
設定されたタイトル
ax.set_title()
-
伝説
ax.plot(ラベル= '伝説')
ax.legend()plt.legend()
LOC =「最高」:自動的に最適な配置位置凡例を選択
サンプルコード:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1) ax.plot(np.random.randn(1000).cumsum(), label='line0') # 设置刻度 #plt.xlim([0,500]) ax.set_xlim([0, 800]) # 设置显示的刻度 #plt.xticks([0,500]) ax.set_xticks(range(0,500,100)) # 设置刻度标签 ax.set_yticklabels(['Jan', 'Feb', 'Mar']) # 设置坐标轴标签 ax.set_xlabel('Number') ax.set_ylabel('Month') # 设置标题 ax.set_title('Example') # 图例 ax.plot(np.random.randn(1000).cumsum(), label='line1') ax.plot(np.random.randn(1000).cumsum(), label='line2') ax.legend() ax.legend(loc='best') #plt.legend()
結果: <matplotlib.legend.Legend at 0x11a4061d0>
二、Seaborn
http://seaborn.pydata.org/index.html
Seabornは実際にほとんどの場合、使用seabornは非常に魅力的なグラフを作ることができるようになります、それは簡単にマッピングすること、より高度なAPIパッケージに基づいて作られたmatplotlibの、あなたはより多くを持っているmatplotlibの使用をすることができます図特性。Seabornのmatplotlibのは補完的ではなく、代替手段として見られるべきです。
-
Pythonはマッピングツールのライブラリである、あなたは魅力的で有益なグラフを作成することができます
-
matplotlibの上に構築し、numpyのパンダのデータ構造の可視化をサポートしています。
-
その他の組み込みのテーマやカラーテーマ
-
単一変数の視覚化、データセット変数の二次元分布を比較するために使用される変数
-
可視化線形回帰モデルの独立変数が独立変数とではありません
import numpy as np
import pandas as pd
# from scipy import stats import matplotlib.pyplot as plt import seaborn as sns # %matplotlib inline
データの可視化の分散セット
一変量分布sns.distplot()
サンプルコード:
# 单变量分布
x1 = np.random.normal(size=1000)
sns.distplot(x1);
x2 = np.random.randint(0, 100, 500) sns.distplot(x2);
結果:
ヒストグラムsns.distplot(KDE =偽)
サンプルコード:
# 直方图
sns.distplot(x1, bins=20, kde=False, rug=True)
結果:
カーネル密度推定sns.distplot(HIST =偽)またはsns.kdeplot()
サンプルコード:
# 核密度估计
sns.distplot(x2, hist=False, rug=True)
結果:
二変量分布
サンプルコード:
# 双变量分布
df_obj1 = pd.DataFrame({"x": np.random.randn(500),
"y": np.random.randn(500)}) df_obj2 = pd.DataFrame({"x": np.random.randn(500), "y": np.random.randint(0, 100, 500)})
図散乱sns.jointplot()
サンプルコード:
# 散布图
sns.jointplot(x="x", y="y", data=df_obj1)
結果:
二次元のヒストグラムHexbinのsns.jointplot(種類=「進」)
サンプルコード:
# 二维直方图
sns.jointplot(x="x", y="y", data=df_obj1, kind="hex");
結果:
カーネル密度推定sns.jointplot(種類= 'KDE')
サンプルコード:
# 核密度估计
sns.jointplot(x="x", y="y", data=df_obj1, kind="kde");
結果:
変数のデータセット間の関係は)(sns.pairplotを可視化
サンプルコード:
# 数据集中变量间关系可视化
dataset = sns.load_dataset("tips")
#dataset = sns.load_dataset("iris")
sns.pairplot(dataset);
結果:
カテゴリーデータの可視化
#titanic = sns.load_dataset('titanic')
#planets = sns.load_dataset('planets')
#flights = sns.load_dataset('flights')
#iris = sns.load_dataset('iris')
exercise = sns.load_dataset('exercise')
カテゴリー散布図
sns.stripplot()データ点の重複
サンプルコード:
sns.stripplot(x="diet", y="pulse", data=exercise)
結果:
、色相指定サブカテゴリをデータポイントの重複を避けるためにsns.swarmplot()
サンプルコード:
sns.swarmplot(x="diet", y="pulse", data=exercise, hue='kind')
結果:
カテゴリ内のデータ配信
図ボックスsns.boxplot()、色相指定のサブカテゴリ
サンプルコード:
# 盒子图
sns.boxplot(x="diet", y="pulse", data=exercise)
#sns.boxplot(x="diet", y="pulse", data=exercise, hue='kind')
結果:
バイオリン図sns.violinplot()、色相指定サブカテゴリ
サンプルコード:
# 小提琴图
#sns.violinplot(x="diet", y="pulse", data=exercise)
sns.violinplot(x="diet", y="pulse", data=exercise, hue='kind')
結果:
カテゴリチャート内
ヒストグラムsns.barplot()
サンプルコード:
# 柱状图
sns.barplot(x="diet", y="pulse", data=exercise, hue='kind')
結果:
図ポイントsns.pointplot()
サンプルコード:
# 点图
sns.pointplot(x="diet", y="pulse", data=exercise, hue='kind');
結果: