使用matplotlib實(shí)現(xiàn)在同一個(gè)窗口繪制多個(gè)圖形
matplotlib在同一個(gè)窗口繪制多個(gè)圖形

代碼如下:
import numpy as np import matplotlib.pyplot as plt #創(chuàng)建自變量數(shù)組 x= np.linspace(0,2*np.pi,500) #創(chuàng)建函數(shù)值數(shù)組 y1 = np.sin(x) y2 = np.cos(x) y3 = np.sin(x*x) #創(chuàng)建圖形 plt.figure(1) ''' 意思是在一個(gè)2行2列共4個(gè)子圖的圖中,定位第1個(gè)圖來進(jìn)行操作(畫圖)。 最后面那個(gè)1表示第1個(gè)子圖。那個(gè)數(shù)字的變化來定位不同的子圖 ''' #第一行第一列圖形 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坐標(biāo)軸范圍 plt.ylim(-1.2,1.2) #選擇ax2 plt.sca(ax2) #繪制藍(lán)色曲線 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導(dǎo)入matplotlib.pyplot模塊, 并簡寫成plt. 使用plt.figure創(chuàng)建一個(gè)圖像窗口.
使用plt.subplot來創(chuàng)建小圖. plt.subplot(2,2,1)表示將整個(gè)圖像窗口分為2行2列, 當(dāng)前位置為1. 使用plt.plot([-2,3],[1,5])在第1個(gè)位置創(chuàng)建一個(gè)小圖.依此類推后面三個(gè)子圖。
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() #自動(dòng)調(diào)整子批次參數(shù),使子批次適合圖形區(qū)域

2.分格顯示
2.1.subplot2grid方法
使用plt.subplot2grid來創(chuàng)建第1個(gè)小圖, (3,3)表示將整個(gè)圖像窗口分成3行3列, (0,0)表示從第0行第0列開始作圖,colspan=3表示列的跨度為3, rowspan=1表示行的跨度為1. colspan和rowspan缺省, 默認(rèn)跨度為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導(dǎo)入matplotlib.pyplot模塊, 并簡寫成plt. 使用import導(dǎo)入matplotlib.gridspec, 并簡寫成gridspec.
使用plt.figure()創(chuàng)建一個(gè)圖像窗口, 使用gridspec.GridSpec將整個(gè)圖像窗口分成3行3列.
使用plt.subplot來作圖, gs[0, :]表示這個(gè)圖占第0行和所有列, gs[1, :2]表示這個(gè)圖占第1行和第2列前的所有列, gs[1:, 2]表示這個(gè)圖占第1行后的所有行和第2列, gs[-1, 0]表示這個(gè)圖占倒數(shù)第1行和第0列, gs[-1, -2]表示這個(gè)圖占倒數(shù)第1行和倒數(shù)第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]) #最后一行第一個(gè)位置 ax10 = plt.subplot(gs[-1, -2]) #最后一行倒數(shù)第二個(gè)位置 plt.tight_layout() plt.show()

2.3.subplots方法
使用plt.subplots建立一個(gè)2行2列的圖像窗口,sharex=True表示共享x軸坐標(biāo), sharey=True表示共享y軸坐標(biāo). ((ax11, ax12), (ax13, ax14))表示第1行從左至右依次放ax11和ax12, 第2行從左至右依次放ax13和ax14.
使用ax11.scatter創(chuàng)建一個(gè)散點(diǎn)圖.
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個(gè)值都是占整個(gè)figure坐標(biāo)系的百分比。在這里,假設(shè)figure的大小是10x10,那么大圖就被包含在由(1, 1)開始,寬8,高8的坐標(biāo)系內(nèi)。
將大圖坐標(biāo)系添加到figure中,顏色為r(red),取名為title。
接著,我們來繪制右上角的小圖,步驟和繪制大圖一樣,注意坐標(biāo)系位置和大小的改變:
import matplotlib.pyplot as plt
fig = plt.figure() #定義一個(gè)圖像窗口
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為原點(diǎn)坐標(biāo)(按比例),width, height為圖的長寬(按比例)
ax1 = fig.add_axes([left, bottom, width, height]) #在fig中添加一個(gè)圖
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中再添加一個(gè)圖
ax2.plot(y, x, 'r')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('small fig in large ')
plt.show()效果如下:

4.設(shè)置雙坐標(biāo)軸
有時(shí)候我們會(huì)用到次坐標(biāo)軸,即在同個(gè)圖上有第2個(gè)y軸存在。同樣可以用matplotlib做到,而且很簡單。
對(duì)ax1調(diào)用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軸的坐標(biāo)軸
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'r+')
#設(shè)置標(biāo)簽(共用X軸)
ax1.set_xlabel('X')
ax1.set_ylabel('Y1',color='g')
ax2.set_ylabel('Y2',color='r')
plt.show()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python編寫自動(dòng)化郵件發(fā)送程序(進(jìn)階版)
在數(shù)字化時(shí)代,自動(dòng)化郵件發(fā)送功能已成為企業(yè)和個(gè)人提升工作效率的重要工具,本文將使用Python編寫一個(gè)簡單的自動(dòng)化郵件發(fā)送程序,希望對(duì)大家有所幫助2025-08-08
基于Tensorflow的MNIST手寫數(shù)字識(shí)別分類
這篇文章主要為大家詳細(xì)介紹了基于Tensorflow的MNIST手寫數(shù)字識(shí)別分類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
python實(shí)現(xiàn)將多個(gè)文件分配到多個(gè)文件夾的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)將多個(gè)文件分配到多個(gè)文件夾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01

