Python畫圖總結
import numpy as npnimport matplotlib.pyplot as pltnnx = np.arange(10000)ny = np.sin(2*0.01*x)nnnfig=plt.figure(figsize=(6,2.5))nax = fig.add_subplot(111)nplt.plot(x,y)nax.set_xlim(0,10000)nax.set_ylim(-2,2)nax.set_xticks(np.linspace(0,10000,5)) nax.set_yticks(np.linspace(-2,2,5))nnax.set_xlabel(Times)nax.set_ylabel(Amplitude)nnplt.tight_layout()nfig.set_size_inches(6,2.5)nfig.savefig(first.png,dpi=1200)nnplt.show()n
2、直方圖
import numpy as npnfrom numpy import arangenimport matplotlib.pyplot as pltnnimport scipy.io as sionncorr_m = [[0.8,0.4,0.7,0.65],[0.75,0.42,0.51,0.65],[0.45,0.91,0.34,0.51],[0.41,0.32,0.51,0.28],[0.12,0.32,0.25,0.41]]ncorr_m = np.array(corr_m)nmean_corr_m = np.mean(corr_m,axis=1)nnnnN1,N2 = corr_m.shapenindex = np.arange(N1)+1nn################################### Plot ##############################nmean_corr_m_sort_location1 = np.argsort(mean_corr_m)nmean_corr_m_sort1 = np.sort(mean_corr_m)nmean_corr_m_sort_location = mean_corr_m_sort_location1[::-1]nmean_corr_m_sort = mean_corr_m_sort1[::-1]nncorr_m_sort = np.zeros([5,4])nfor i in range(N1):n corr_m_sort[i,:] = corr_m[mean_corr_m_sort_location[i],:]nnfig = plt.figure(figsize=(6,2.5))nax = fig.add_subplot(111)nbar_width = 0.13nopacity = 0.8nrects1 = plt.bar(index, corr_m_sort[:,0], bar_width, alpha=opacity, color = b,label= Men) nrects2 = plt.bar(index+bar_width*1, corr_m_sort[:,1], bar_width, alpha=opacity, color = m)nrects3 = plt.bar(index+bar_width*2, corr_m_sort[:,2], bar_width, alpha=opacity, color = g)nrects4 = plt.bar(index+bar_width*3, corr_m_sort[:,3], bar_width, alpha=opacity, color = k)nnplt.plot(index+bar_width*3,mean_corr_m_sort[:],ro-)nnax.set_xlim(1,6)nax.set_ylim(0,1)nnax.set_xticks(np.linspace(1,6,6)) nax.set_yticks(np.linspace(0,1,6))nnplt.xticks(index + 2*bar_width, (1, 2, 3, 4, 5)) nplt.tight_layout()nfig.set_size_inches(6,2.5)nfig.savefig(fig19_a.png,dpi=1200)n
3、去掉畫布邊框、添加曲線說明
import numpy as npnimport matplotlib.pyplot as pltnnx = np.arange(10000)ny = np.sin(2*0.01*x)nnnfig=plt.figure(figsize=(6,2.5))nax = fig.add_subplot(111)nplt.plot(x,y,label="cosine")nax.set_xlim(0,10000)nax.set_ylim(-2,2)nax.set_xticks(np.linspace(0,10000,5)) nax.set_yticks(np.linspace(-2,2,5))nnax.set_xlabel(Times)nax.set_ylabel(Amplitude)nnax.spines[right].set_color(none)nax.spines[top].set_color(none)nax.legend(loc=upper left)nnplt.tight_layout()nfig.set_size_inches(6,2.5)nfig.savefig(plot3.png,dpi=1200)nnplt.show()n
4、三維畫圖、變換背景顏色、方向轉換
import numpy as npnimport matplotlib.pyplot as pltnfrom mpl_toolkits.mplot3d import Axes3Dnfrom matplotlib.collections import PolyCollectionnfrom matplotlib.colors import colorConverternnimport scipy.io as sionnt = np.linspace(0,10,2000)nnz = np.zeros([5,2000])nnfor i in range(5):n z[i,:] = np.sin(2*np.pi*t*(i+1)*0.2)n nx = [np.linspace(0,9,5)]*2000ny = [np.linspace(0,1999,2000)]*5nx = np.array(x).Tny = np.array(y)nnfig = plt.figure(figsize=(6,2.5))nnax = fig.gca(projection=3d,axisbg=white)nplt.gca().patch.set_facecolor(white)nnnax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))nax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))nax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))nnfor i in range(5):n ax.plot(x[i,:],y[i,:],z[i,:])nnax.view_init(20,60) nfig.set_size_inches(6, 2.5)nfig.savefig(fig6.png,dpi=1200)nplt.show()n
推薦閱讀:
TAG:Python | Matplotlib |