散點圖
本節開始介紹用plt.plot和ax.plot畫散點圖
# In[*] import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline # In[*] x = np.linspace(0,10,30) y = np.sin(x) plt.plot(x,y,'o',color='black')
第三個參數‘o'是代表散點圖中散點的形狀,可以修改。
rng = np.random.RandomState(1234) for marker in ['o','.',',','x','+','v','^','<','>','s','d']: plt.plot(rng.rand(5),rng.rand(5),marker, label="marker'='{0}'".format(marker)) plt.legend(numpoints=1) plt.xlim(0,1.8)
可以擬合成一條曲線,其中’-ok‘中直線(-),圓圈(o),黑色(k)
rng = np.random.RandomState(1234) for marker in ['o','.',',','x','+','v','^','<','>','s','d']: plt.plot(rng.rand(5),rng.rand(5),marker, label="marker'='{0}'".format(marker)) plt.legend(numpoints=1) plt.xlim(0,1.8) plt.plot(x,y,'-ok')
用plt.scatter畫散點圖
plt.scatter相對於plt.plot的主要優勢在於,前者在創建散點圖時具有更高的靈活性,可以單獨控制每個散點與數據匹配,也可以讓每個散點具有不同的屬性(大小,表面顏色,邊框顏色等)
rng = np.random.RandomState(1234) x = rng.randn(100) y = rng.randn(100) colors = rng.rand(100) sizes = 1000*rng.rand(100) plt.scatter(x,y,c=colors,s=sizes,alpha=0.3,cmap='viridis') plt.colorbar()
# In[*] from sklearn.datasets import load_iris iris = load_iris() features = iris.data.T plt.scatter(features[0],features[1],alpha=0.2, s=100*features[3],c=iris.target,cmap='viridis') plt.xlabel(iris.feature_names[0]) plt.ylab(iris.feature_names[1])
當面對大型數據集時,plt.plot會在效率方面優於plt.scatter,這是因爲plt.scatter會對每一個單獨的散點進行大小或者顏色的設置,而plt.plot是一次性複製所有的設置。
繪製基本誤差線
%reset -f %clear # In[*] import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline # In[*] plt.style.use('seaborn-whitegrid') x = np.linspace(0,10,50) dy = np.linspace(0,1,50) y = np.sin(x) + dy*np.random.randn(50) plt.errorbar(x,y,yerr=dy,fmt='.k')
一般來說,誤差棒的顏色最好比數據點的顏色稍淺一點比較好,尤其當你的數據點非常多時。
# In[*] plt.style.use('seaborn-whitegrid') x = np.linspace(0,10,50) dy = np.linspace(0,1,50) y = np.sin(x) + dy*np.random.randn(50) plt.errorbar(x,y,yerr=dy,fmt='o', color='black',ecolor='gray',elinewidth=3,capsize=0)
誤差棒還有一些比較少用的參數,例如水平方向的誤差線(xerr),單側誤差線(one-sidederrorbar)。