Matplotlib学习笔记3
生活随笔
收集整理的這篇文章主要介紹了
Matplotlib学习笔记3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、盒形圖繪制:
%matplotlib inline import matplotlib.pyplot as plt import numpy as nptang_data = [np.random.normal(0,std,100) for std in range(1,4)] fig = plt.figure(figsize = (8,6)) #區域大小 plt.boxplot(tang_data,notch=False,sym='s',vert=True) #notch=False基本形狀,sym=‘s’方塊,vert=True豎著畫plt.xticks([y+1 for y in range(len(tang_data))],['x1','x2','x3']) plt.xlabel('x') plt.title('box plot')更改盒形圖顏色:
for components in bplot.keys():for line in bplot[components]:line.set_color('black')也可以橫著畫,只需要vert=False:
tang_data = [np.random.normal(0,std,100) for std in range(1,4)] fig = plt.figure(figsize = (8,6)) plt.boxplot(tang_data,notch=False,sym='s',vert=False)plt.yticks([y+1 for y in range(len(tang_data))],['x1','x2','x3']) plt.ylabel('x') plt.title('box plot')當然你要實在閑著沒事干,甚至可以把盒子形狀改一下,notch=True。
或者更改一下盒子的顏色:
2、小提琴圖:violinplot
fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(12,5)) tang_data = [np.random.normal(0,std,100) for std in range(6,10)] axes[0].violinplot(tang_data,showmeans=False,showmedians=True) axes[0].set_title('violin plot')axes[1].boxplot(tang_data) axes[1].set_title('box plot')for ax in axes:ax.yaxis.grid(True)ax.set_xticks([y+1 for y in range(len(tang_data))]) plt.setp(axes,xticks=[y+1 for y in range(len(tang_data))],xticklabels=['x1','x2','x3','x4'])3、對于軸的操作:
x = range(10) y = range(10) fig = plt.gca() #對軸操作 plt.plot(x,y) fig.axes.get_xaxis().set_visible(False) #消去軸的刻度 fig.axes.get_yaxis().set_visible(False)4、去掉上右兩軸且帶網格的直方圖:
import math x = np.random.normal(loc = 0.0,scale=1.0,size=300) width = 0.5 bins = np.arange(math.floor(x.min())-width,math.ceil(x.max())+width,width) ax = plt.subplot(111)ax.spines['top'].set_visible(False) #去除上軸 ax.spines['right'].set_visible(False) #去除右軸plt.tick_params(bottom='off',top='off',left = 'off',right='off') #消除軸上的指示線 plt.grid() #加上網格 plt.hist(x,alpha = 0.5,bins = bins) #hist直方圖5、軸標簽:
x = range(10) y = range(10)labels = ['godx' for i in range(10)] fig,ax = plt.subplots() plt.plot(x,y) plt.title('godx') ax.set_xticklabels(labels,rotation = 45,horizontalalignment='right') #rotation = 45傾斜角,horizontalalignment='right'右對齊import matplotlib as mpl mpl.rcParams['axes.titlesize'] = '10' #更改圖標題的大小6、一個圖多條曲線+圖例:
x = np.arange(10) for i in range(1,4):plt.plot(x,i*x**2,label = 'Group %d'%i) plt.legend(loc='best') #在圖里加上圖例,loc='best'放在最好的位置也可指定其他位置upper center、lower left等
ax.legend(loc='upper center',bbox_to_anchor = (0.5,1.15) ,ncol=3) #把圖例放在圖外面,上方,(0.5,1.15)表示位置,nocl=3表示橫著寫(3列) ax.legend(loc='upper center',bbox_to_anchor = (1.15,1) ,ncol=1) #ncol=1豎著寫(1列) plt.legend(loc='upper right',framealpha = 0.1) #將圖例設置為透明7、直方圖:
最簡單的:
兩個變量的直方圖:
import random data1 = [random.gauss(15,10) for i in range(500)] data2 = [random.gauss(5,5) for i in range(500)] bins = np.arange(-50,50,2.5)plt.hist(data1,bins=bins,label='class 1',alpha = 0.3) #設置alpha可以看兩組數據堆疊程度 plt.hist(data2,bins=bins,label='class 2',alpha = 0.3) plt.legend(loc='best') #圖例 plt.show()8、散點圖的繪制:
mu_vec1 = np.array([0,0]) cov_mat1 = np.array([[2,0],[0,2]]) #協方差矩陣x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100) x2_samples = np.random.multivariate_normal(mu_vec1+0.2, cov_mat1+0.2, 100) x3_samples = np.random.multivariate_normal(mu_vec1+0.4, cov_mat1+0.4, 100)plt.figure(figsize = (8,6)) plt.scatter(x1_samples[:,0],x1_samples[:,1],marker ='x',color='blue',alpha=0.6,label='x1') plt.scatter(x2_samples[:,0],x2_samples[:,1],marker ='o',color='red',alpha=0.6,label='x2') plt.scatter(x3_samples[:,0],x3_samples[:,1],marker ='^',color='green',alpha=0.6,label='x3') plt.legend(loc='best') plt.show()也可以在散點圖中加入每個點的坐標:
x_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74, 0.93] y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25, 0.55]plt.figure(figsize = (8,6)) plt.scatter(x_coords,y_coords,marker='s',s=50) #s=50點的大小for x,y in zip(x_coords,y_coords):plt.annotate('(%s,%s)'%(x,y),xy=(x,y),xytext=(0,-15),textcoords = 'offset points',ha='center') #加注釋,textcoords = 'offset points'加坐標,ha='center'對齊 plt.show()離某個點越遠點越大:
mu_vec1 = np.array([0,0]) cov_mat1 = np.array([[1,0],[0,1]]) X = np.random.multivariate_normal(mu_vec1, cov_mat1, 500) fig = plt.figure(figsize=(8,6))R=X**2 R_sum=R.sum(axis = 1)plt.scatter(X[:,0],X[:,1],color='grey',marker='o',s=20*R_sum,alpha=0.5) plt.show()總結
以上是生活随笔為你收集整理的Matplotlib学习笔记3的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《Python 数据科学实践指南》读书笔
- 下一篇: HDU 6078 Wavel Seque