使用matplotlib實現在同一個窗口繪制多個圖形
matplotlib在同一個窗口繪制多個圖形
代碼如下:
import numpy as np import matplotlib.pyplot as plt #創(chuàng)建自變量數組 x= np.linspace(0,2*np.pi,500) #創(chuàng)建函數值數組 y1 = np.sin(x) y2 = np.cos(x) y3 = np.sin(x*x) #創(chuàng)建圖形 plt.figure(1) ''' 意思是在一個2行2列共4個子圖的圖中,定位第1個圖來進行操作(畫圖)。 最后面那個1表示第1個子圖。那個數字的變化來定位不同的子圖 ''' #第一行第一列圖形 ax1 = plt.subplot(2,2,1) #第一行第二列圖形 ax2 = plt.subplot(2,2,2) #第二行 ax3 = plt.subplot(2,1,2) #選擇ax1 plt.sca(ax1) #繪制紅色曲線 plt.plot(x,y1,color='red') #限制y坐標軸范圍 plt.ylim(-1.2,1.2) #選擇ax2 plt.sca(ax2) #繪制藍色曲線 plt.plot(x,y2,'b--') plt.ylim(-1.2,1.2) #選擇ax3 plt.sca(ax3) plt.plot(x,y3,'g--') plt.ylim(-1.2,1.2) plt.show()
matplotlib——多圖合并
1.多圖合一(subplot)
matplotlib 是可以組合許多的小圖, 放在一張大圖里面顯示的. 使用到的方法叫作 subplot.
使用import導入matplotlib.pyplot模塊, 并簡寫成plt. 使用plt.figure創(chuàng)建一個圖像窗口.
使用plt.subplot來創(chuàng)建小圖. plt.subplot(2,2,1)表示將整個圖像窗口分為2行2列, 當前位置為1. 使用plt.plot([-2,3],[1,5])在第1個位置創(chuàng)建一個小圖.依此類推后面三個子圖。
import matplotlib.pyplot as plt plt.figure(figsize=(5,4)) plt.subplot(2,2,1) #221亦可 plt.plot([-2,3],[1,5]) plt.subplot(2,2,2) plt.plot([4,9],[3,-5]) plt.subplot(2,2,3) plt.plot([-6,-9],[3,-7]) plt.subplot(2,2,4) plt.plot([1,-1],[0,4]) plt.tight_layout() #自動調整子批次參數,使子批次適合圖形區(qū)域
2.分格顯示
2.1.subplot2grid方法
使用plt.subplot2grid來創(chuàng)建第1個小圖, (3,3)表示將整個圖像窗口分成3行3列, (0,0)表示從第0行第0列開始作圖,colspan=3表示列的跨度為3, rowspan=1表示行的跨度為1. colspan和rowspan缺省, 默認跨度為1.后面子圖與此方法一樣。
import matplotlib.pyplot as plt plt.figure() ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) #plt.subplot2grid(shape, location, rowspan, colspan) ax1.plot([1.6, 2.3], [0.8, 1.2]) #畫直線 ax1.set_title('this is ax1') ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=1) #起始(1,0)橫向跨度為1 ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2) #起始(1,2)縱向跨度為2 ax4 = plt.subplot2grid((3, 3), (2, 0)) ax4.scatter([0.5, 1], [1.8, 1.5]) ax4.set_xlabel('X') ax4.set_ylabel('Y') ax5 = plt.subplot2grid((3, 3), (2, 1)) plt.tight_layout() plt.show()
2.2.gridspec方法
使用import導入matplotlib.pyplot模塊, 并簡寫成plt. 使用import導入matplotlib.gridspec, 并簡寫成gridspec.
使用plt.figure()創(chuàng)建一個圖像窗口, 使用gridspec.GridSpec將整個圖像窗口分成3行3列.
使用plt.subplot來作圖, gs[0, :]表示這個圖占第0行和所有列, gs[1, :2]表示這個圖占第1行和第2列前的所有列, gs[1:, 2]表示這個圖占第1行后的所有行和第2列, gs[-1, 0]表示這個圖占倒數第1行和第0列, gs[-1, -2]表示這個圖占倒數第1行和倒數第2列.
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec plt.figure() gs = gridspec.GridSpec(3, 3) # use index from 0 ax6 = plt.subplot(gs[0, :]) ax7 = plt.subplot(gs[1, :2]) ax8 = plt.subplot(gs[1:, 2]) #第一行第二行的第二列(起始為0) ax9 = plt.subplot(gs[-1, 0]) #最后一行第一個位置 ax10 = plt.subplot(gs[-1, -2]) #最后一行倒數第二個位置 plt.tight_layout() plt.show()
2.3.subplots方法
使用plt.subplots建立一個2行2列的圖像窗口,sharex=True表示共享x軸坐標, sharey=True表示共享y軸坐標. ((ax11, ax12), (ax13, ax14))表示第1行從左至右依次放ax11和ax12, 第2行從左至右依次放ax13和ax14.
使用ax11.scatter創(chuàng)建一個散點圖.
import matplotlib.pyplot as plt f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True) ax11.scatter([1.3,1.75], [1.8,1]) plt.tight_layout() #表示緊湊顯示圖像 plt.show()
3.圖中有圖(plot in plot)
首先確定大圖左下角的位置以及寬高:left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
注意,4個值都是占整個figure坐標系的百分比。在這里,假設figure的大小是10x10,那么大圖就被包含在由(1, 1)開始,寬8,高8的坐標系內。
將大圖坐標系添加到figure中,顏色為r(red),取名為title。
接著,我們來繪制右上角的小圖,步驟和繪制大圖一樣,注意坐標系位置和大小的改變:
import matplotlib.pyplot as plt fig = plt.figure() #定義一個圖像窗口 x = [2, 3, 2, 5, 4, 8, 7] y = [2, 5, 6, 8, 2, 3, 1] left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 #left、bottom為原點坐標(按比例),width, height為圖的長寬(按比例) ax1 = fig.add_axes([left, bottom, width, height]) #在fig中添加一個圖 ax1.plot(x, y, 'blue') ax1.set_xlabel('x') ax1.set_ylabel('y') ax1.set_title('title') left, bottom, width, height = 0.6, 0.6, 0.25, 0.25 ax2 = fig.add_axes([left, bottom, width, height]) #在fig中再添加一個圖 ax2.plot(y, x, 'r') ax2.set_xlabel('x') ax2.set_ylabel('y') ax2.set_title('small fig in large ') plt.show()
效果如下:
4.設置雙坐標軸
有時候我們會用到次坐標軸,即在同個圖上有第2個y軸存在。同樣可以用matplotlib做到,而且很簡單。
對ax1調用twinx()方法,生成如同鏡面效果后的ax2
import matplotlib.pyplot as plt import numpy as np x = np.arange(0,10,0.1) y1 = 2*(x**2) y2 = -0.5*(x**2) fig, ax1 = plt.subplots() ax2 = ax1.twinx() #使用twinx添加y軸的坐標軸 ax1.plot(x, y1, 'g-') ax2.plot(x, y2, 'r+') #設置標簽(共用X軸) ax1.set_xlabel('X') ax1.set_ylabel('Y1',color='g') ax2.set_ylabel('Y2',color='r') plt.show()
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。