摘要:
matplotlib 是可以組合許多的小圖, 放在一張大圖里面顯示的. 使用到的方法叫作 subplot.
1.定義引用
import matplotlib.pyplot as plt
import numpy as np
#matplotlib 是可以組合許多的小圖, 放在一張大圖里面顯示的. 使用到的方法叫作 subplot.
#使用import導(dǎo)入matplotlib.pyplot模塊, 并簡寫成plt
2.編寫函數(shù)
def multiGraph():
"""
圖中圖
"""
plt.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(223)
plt.plot([0, 1], [0, 1])
plt.subplot(224)
plt.plot([0, 1], [0, 1])
"""
plt.subplot(224)表示將整個圖像窗口分為2行2列,
當前位置為4. 使用plt.plot([0,1],[0,1])在第4個位置創(chuàng)建一個小圖.
"""
plt.show() # 展示
def multiGraph2():
plt.subplot(2, 1, 1)
plt.plot([0, 1], [0, 1])
"""
使用plt.subplot(2,1,1)將整個圖像窗口分為2行1列, 當前位置為1.
使用plt.plot([0,1],[0,1])在第1個位置創(chuàng)建一個小圖.
"""
plt.subplot(2, 3, 4)
plt.plot([0, 1], [0, 2])
"""
使用plt.subplot(2,3,4)將整個圖像窗口分為2行3列, 當前位置為4.
使用plt.plot([0,1],[0,2])在第4個位置創(chuàng)建一個小圖.
這里需要解釋一下為什么第4個位置放第2個小圖.
上一步中使用plt.subplot(2,1,1)將整個圖像窗口分為2行1列,
第1個小圖占用了第1個位置, 也就是整個第1行.
這一步中使用plt.subplot(2,3,4)將整個圖像窗口分為2行3列,
于是整個圖像窗口的第1行就變成了3列, 也就是成了3個位置,
于是第2行的第1個位置是整個圖像窗口的第4個位置.
使用plt.subplot(235)將整個圖像窗口分為2行3列,當前位置為5.
使用plt.plot([0,1],[0,3])在第5個位置創(chuàng)建一個小圖.
同上, 再創(chuàng)建plt.subplot(236).
"""
plt.subplot(235)
plt.plot([0, 1], [0, 3])
plt.subplot(236)
plt.plot([0, 1], [0, 4])
plt.show() # 展示
def multiGraph3():
"""subplot2grid 方法分格顯示"""
plt.figure() #創(chuàng)建一個圖片
"""
使用plt.subplot2grid來創(chuàng)建第1個小圖, (3,3)表示將整個圖像窗口分成3行3列,
(0,0)表示從第0行第0列開始作圖,colspan=3表示列的跨度為3,
rowspan=1表示行的跨度為1. colspan和rowspan缺省, 默認跨度為1.
"""
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax1.plot([1, 2], [1, 2]) # 畫小圖
ax1.set_title('ax1_title') # 設(shè)置小圖的標題
"""
使用plt.subplot2grid來創(chuàng)建第2個小圖, (3,3)表示將整個圖像窗口分成3行3列,
(1,0)表示從第1行第0列開始作圖,colspan=2表示列的跨度為2. 同上畫出 ax3,
(1,2)表示從第1行第2列開始作圖,rowspan=2表示行的跨度為2.
再畫一個 ax4 和 ax5, 使用默認 colspan, rowspan.
"""
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
"""
使用ax4.scatter創(chuàng)建一個散點圖, 使用ax4.set_xlabel和ax4.set_ylabel來對x軸和y軸命名.
"""
ax4.scatter([1, 2], [2, 2])
ax4.set_xlabel('ax4_x')
ax4.set_ylabel('ax4_y')
plt.show()
def multiGraph4():
"""
gridspec實現(xiàn)分割圖
使用import導(dǎo)入matplotlib.gridspec, 并簡寫成gridspec.
"""
import matplotlib.gridspec as gridspec
"""
使用plt.figure()創(chuàng)建一個圖像窗口, 使用gridspec.GridSpec將整個圖像窗口分成3行3列.
"""
plt.figure()
gs = gridspec.GridSpec(3, 3)
"""
使用plt.subplot來作圖, gs[0, :]表示這個圖占第0行和所有列,
gs[1, :2]表示這個圖占第1行和第2列前的所有列,
gs[1:, 2]表示這個圖占第1行后的所有行和第2列,
gs[-1, 0]表示這個圖占倒數(shù)第1行和第0列,
gs[-1, -2]表示這個圖占倒數(shù)第1行和倒數(shù)第2列.
"""
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])
plt.show()
def multiGraph5():
"""
subplots
"""
"""
使用plt.subplots建立一個2行2列的圖像窗口,sharex=True表示共享x軸坐標,
sharey=True表示共享y軸坐標.
((ax11, ax12), (ax13, ax14))表示第1行從左至右依次放ax11和ax12,
第2行從左至右依次放ax13和ax14.
"""
f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)
"""使用ax11.scatter創(chuàng)建一個散點圖."""
ax11.scatter([1, 2], [1, 2])
"""
plt.tight_layout()表示緊湊顯示圖像, plt.show()表示顯示圖像.
"""
plt.tight_layout()
plt.show()
def multiGraph6():
"""
圖中圖
"""
# 初始化figure
fig = plt.figure()
# 創(chuàng)建數(shù)據(jù)
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
"""
4個值都是占整個figure坐標系的百分比。
在這里,假設(shè)figure的大小是10x10,
那么大圖就被包含在由(1, 1)開始,寬8,高8的坐標系內(nèi)。
將大圖坐標系添加到figure中,顏色為r(red),取名為title:
"""
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])
ax2.plot(y, x, 'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')
"""
這里我們采用一種更簡單方法,即直接往plt里添加新的坐標系:
"""
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g') # 注意對y進行了逆序處理
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')
plt.show()
def multiGraph7():
"""
有時候我們會用到次坐標軸,即在同個圖上有第2個y軸存在。同樣可以用matplotlib做到,而且很簡單。
"""
import numpy as np
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x ** 2
y2 = -1 * y1
"""
可以看到,y2和y1是互相倒置的。接著,獲取figure默認的坐標系 ax1:
"""
fig, ax1 = plt.subplots()
"""
對ax1調(diào)用twinx()方法,生成如同鏡面效果后的ax2:
"""
ax2 = ax1.twinx()
"""
接著進行繪圖, 將 y1, y2 分別畫在 ax1, ax2 上:
"""
ax1.plot(x, y1, 'g-') # green, solid line
ax1.set_xlabel('X data')
ax1.set_ylabel('Y1 data', color='g')
ax2.plot(x, y2, 'b-') # blue
ax2.set_ylabel('Y2 data', color='b')
plt.show()
3.執(zhí)行
選擇一個函數(shù)執(zhí)行
if __name__ == "__main__":
multiGraph8()
4.執(zhí)行效果圖
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
