莫烦Matplotlib可视化第四章多图合并显示代码学习
生活随笔
收集整理的這篇文章主要介紹了
莫烦Matplotlib可视化第四章多图合并显示代码学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
4.1Subplot多合一顯示
import matplotlib.pyplot as plt import numpy as npplt.figure() """ 每個圖占一個位置 """ # plt.subplot(2,2,1) #將畫板分成兩行兩列,選取第一個位置,可以去掉逗號 # plt.plot([0,1],[0,1]) # # plt.subplot(2,2,2) # plt.plot([0,1],[0,1]) # # plt.subplot(2,2,3) # plt.plot([0,1],[0,1]) # # plt.subplot(2,2,4) # plt.plot([0,1],[0,1]) """ 第一個圖獨占一行 """ plt.subplot(2,1,1) plt.plot([0,1],[0,1])plt.subplot(2,3,4) plt.plot([0,1],[0,1])plt.subplot(2,3,5) plt.plot([0,1],[0,1])plt.subplot(2,3,6) plt.plot([0,1],[0,1])plt.show()4.2Subplot分格顯示
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec """ 三種不同的subplot顯示方法 """# #第一種:subplot2grid # plt.figure() # ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1) #3行3列起始點是0.0,跨度是一行三列 # ax1.plot([1,2],[1,2]) # ax1.set_title('ax1_title') # ax2 = plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1) #3行3列起始點是0.0,跨度是一行三列 # 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)# #第二種:gridspec # plt.figure() # gs = gridspec.GridSpec(3,3) # ax6 = plt.subplot(gs[0,:]) # ax7 = plt.subplot(gs[1,:2]) #第一行占了前兩個列 # ax8 = plt.subplot(gs[1:,2]) #第一行之后,占第二個列 # ax9 = plt.subplot(gs[-1,0]) # ax10 = plt.subplot(gs[-1,-2])#第三鐘:define structure f, ((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex = True,sharey=True) #sharex共享X軸 ax11.scatter ([1,2],[1,2])plt.tight_layout() plt.show()4.3圖中圖
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspecfig = 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 ax2 = 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()4.4 主次坐標軸
import matplotlib.pyplot as plt import numpy as npx = np.arange(0,10,0.1) y1 = 0.05*x**2 y2 = -1*y1fig,ax1 = plt.subplots() ax2 = ax1.twinx() #鏡面Y1軸 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()總結
以上是生活随笔為你收集整理的莫烦Matplotlib可视化第四章多图合并显示代码学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到前男友向自己求婚是什么意思
- 下一篇: 梦到买桔子是什么意思