日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Matplotlib笔记(莫烦Python)

發布時間:2023/12/20 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Matplotlib笔记(莫烦Python) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

matplotlib學習筆記

跟著莫煩老師的B站課程記的matplotlib學習筆記,分享給大家^ ^

# 導入要使用的庫 import matplotlib.pyplot as plt import numpy as np

基本用法

x = np.linspace(-1,1,50) y = x**2 plt.plot(x,y) #繪制點線圖 plt.show()

figure圖像

#畫在兩個figure里 x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure() plt.plot(x,y1)plt.figure() plt.plot(x,y2)plt.show()

#畫在一個figure里 x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure() plt.plot(x,y1,color = 'blue',linewidth = 1.0,linestyle = '-') plt.plot(x,y2,color = 'red',linewidth = 3.0,linestyle = '--') plt.show()

設置坐標軸

x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure() plt.plot(x,y1,color = 'blue',linewidth = 1.0,linestyle = '-') plt.plot(x,y2,color = 'red',linewidth = 2.0,linestyle = '--') ##設置坐標軸取值范圍 plt.xlim((-1,2)) plt.ylim((-2,3)) ##設置軸名稱 plt.xlabel("I am x") plt.ylabel("I am y") ##設置坐標軸刻度 new_ticks = np.linspace(-1,2,5) print('new_ticks:',new_ticks) plt.xticks(new_ticks) plt.yticks([-2,-1.8,-1,1.22,3],['$really\ bad$',r'$bad\ \alpha$','$normal$','really good'])# gca = 'get cuurent axis' ax = plt.gca() #獲取當前figure的軸(共4個:'bottom','left','top','right') #設置軸的顏色 ax.spines['right'].set_color('none') #隱藏'right'軸 ax.spines['top'].set_color('none') #隱藏'top'軸 #設置軸上標尺的位置 ax.xaxis.set_ticks_position('bottom') #設置x軸上標尺顯示的位置 ax.yaxis.set_ticks_position('left') #設置y軸上標尺顯示的位置 #設置軸的位置 ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0))plt.show() new_ticks: [-1. -0.25 0.5 1.25 2. ]

圖例

x = np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 plt.figure() l1,=plt.plot(x,y1,label = 'up',color = 'blue',linewidth = 1.0,linestyle = '-') l2,=plt.plot(x,y2,label = 'down',color = 'red',linewidth = 2.0,linestyle = '--') plt.legend(handles=[l1,],labels=['aaa',],loc='best') plt.show()

注解

x = np.linspace(-3,3,50) y = 2*x+1 plt.figure() plt.plot(x,y,color = 'blue',linewidth = 1.0,linestyle = '-')ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0))x0=1 y0=2*x0 + 1 plt.scatter(x0,y0) #繪制散點圖 plt.plot([x0,x0],[y0,0],'k--',lw=2.5)# method 1 ############################## plt.annotate(r'$2x+1=%s$' % y0,xy=(x0,y0),xycoords='data',xytext=(+30,-30),textcoords='offset points',fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))#method 2 ############################## plt.text(-3.7,3,r'$This\ is\ the\ some\ text. \mu\ \sigma_i\ \alpha_t$',fontdict={'size':16,'color':'r'})plt.show()

坐標軸刻度能見度

x = np.linspace(-3,3,50) y = 0.1*xplt.figure() plt.plot(x,y,color = 'blue',linewidth = 10.0,linestyle = '-') plt.ylim(-2,2) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data',0)) ax.spines['left'].set_position(('data',0))for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(12)label.set_bbox(dict(facecolor='white',edgecolor='None',alpha=0.7)) ## 以上操作并未達到預想效果plt.show()

scatter散點圖

n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) T = np.arctan2(Y,X) #for color valueplt.scatter(X,Y,s=75,c=T,alpha=0.5)plt.xlim((-1.5,1.5)) plt.ylim((-1.5,1.5))##隱藏刻度 plt.xticks(()) plt.yticks(())plt.show()

Bar 柱狀圖

n = 12 X = np.arange(n) Y1 = (1-X/float(n))*np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n))*np.random.uniform(0.5,1.0,n)plt.bar(X,+Y1,facecolor='#9999ff',edgecolor='white') plt.bar(X,-Y2,facecolor='#ff9999',edgecolor='white')for x,y in zip(X,Y1):# ha = horizontal alignmentplt.text(x,y+0.05,'%.2f' % y,ha='center',va='bottom')for x,y in zip(X,Y2):# ha = horizontal alignmentplt.text(x,-y-0.05,'%.2f' % y,ha='center',va='top')plt.xlim(-.5,n) plt.xticks(()) plt.ylim(-1.25,1.25) plt.yticks(())plt.show()

contour 等高線圖

def f(x,y):return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)n = 256 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) X,Y = np.meshgrid(x,y)#填充顏色 plt.contourf(X,Y,f(X,Y),8,alpha = 0.75,cmap=plt.cm.hot) #a添加等高線 C = plt.contour(X,Y,f(X,Y),8,colors='black',linewidth=.5) #adding label plt.clabel(C,inline=True,fontsize=10)plt.xticks(()) plt.yticks(()) plt.show() /home/guoych/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:12: UserWarning: The following kwargs were not used by contour: 'linewidth'if sys.path[0] == '':

Image 圖片

a = np.array([0.31,0.36,0.42,0.36,0.43,0.52,0.42,0.52,0.65]).reshape(3,3)plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower') plt.colorbar()plt.xticks(()) plt.yticks(()) plt.show()

3D數據

# 3D繪圖需要額外導入這個庫 from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = Axes3D(fig) # X,Y value X = np.arange(-4,4,0.25) Y = np.arange(-4,4,0.25) X,Y = np.meshgrid(X,Y) R = np.sqrt(X**2 + Y**2) # height value Z = np.sin(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow')) ax.contourf(X,Y,Z,zdir='z',offset = -2,cmap='rainbow') ax.set_zlim(-2,2)plt.show()

subplot 多個顯示

plt.figure()plt.subplot(2,2,1) plt.plot([0,1],[0,1])plt.subplot(2,2,2) plt.plot([0,1],[0,2])plt.subplot(2,2,3) plt.plot([0,1],[0,3])plt.subplot(224) plt.plot([0,1],[0,4])plt.show()

subplot in grid 分格顯示

import matplotlib.gridspec as gridspec #method 1: ###################################################### plt.figure() ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1) ax1.plot([1,2],[1,2]) ax1.set_title('ax1_title') ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1) ax3 = plt.subplot2grid((3,3),(1,2),colspan=1,rowspan=2) ax4 = plt.subplot2grid((3,3),(2,0),colspan=1,rowspan=1) ax5 = plt.subplot2grid((3,3),(2,1),colspan=1,rowspan=1)plt.show()

#method 2: ###################################################### plt.figure() gs = gridspec.GridSpec(3,3) ax1 = plt.subplot(gs[0,:]) ax2 = plt.subplot(gs[1,:2]) ax3 = plt.subplot(gs[1:,2]) ax4 = plt.subplot(gs[-1,0]) ax5 = plt.subplot(gs[-1,-2])plt.show()

#method 3: ###################################################### f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True) ax11.scatter([1,2],[1,2])plt.tight_layout() plt.show()

plot in plot 圖中圖

fig = plt.figure() x = [1,2,3,4,5,6,7] y = [1,3,4,2,5,8,6]left, bottom, width, height = 0.1,0.1,0.8,0.8 ax1 = fig.add_axes([left,bottom,width,height]) ax1.plot(x,y,'r') ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('title')left, bottom, width, height = 0.2,0.6,0.25,0.25 ax1 = fig.add_axes([left,bottom,width,height]) ax1.plot(x,y,'b') ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('title inside 1')plt.axes([0.6,0.2,0.25,0.25]) plt.plot(y[::-1],x,'g') plt.xlabel('x') plt.ylabel('y') plt.title('title inside 2')plt.show()

次坐標

x = np.arange(0,10,0.1) y1 = 0.05 * x**2 y2 = -1 * y1fig,ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x,y1,'g-') ax2.plot(x,y2,'b--')ax1.set_xlabel('X data') ax1.set_ylabel('Y1',color='g') ax2.set_ylabel('Y2',color='b')plt.show()

animation 動畫

from matplotlib import animation fig,ax=plt.subplots()x = np.arange(0,2*np.pi,0.01) line,=ax.plot(x,np.sin(x))def animate(i):line.set_ydata(np.sin(x+i/10))return line,def init():line.set_ydata(np.sin(x))return line,ani = animation.FuncAnimation(fig=fig,func=animate,frames=100,init_func=init,interval=20,blit=True) plt.show()

總結

以上是生活随笔為你收集整理的Matplotlib笔记(莫烦Python)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。