簡體   English   中英

Python,Matplotlib自定義軸共享Y軸

[英]Python, Matplotlib custom axes share Y axis

我有一個簡單的代碼來創建一個7軸/自定義子圖的圖形(我的理解是子圖是相等大小和等間距的,在我的特殊情況下,我需要一個比其余的大)。

fig = plt.figure(figsize = (16,12))
# row 1
ax1 = plt.axes([0.1,0.7,0.2,0.2])
ax2 = plt.axes([0.4,0.7,0.2,0.2])
ax3 = plt.axes([0.7,0.7,0.2,0.2])
# big row 2
ax4 = plt.axes([0.1, 0.4, 0.5, 0.2])
#row 3
ax5 = plt.axes([0.1,0.1,0.2,0.2])
ax6 = plt.axes([0.4,0.1,0.2,0.2])
ax7 = plt.axes([0.7,0.1,0.2,0.2])

我的問題是,我如何讓所有這些軸共享相同的y軸。 我在谷歌/堆棧上找到的所有內容都是針對子圖,例如:

ax = plt.subplot(blah, sharey=True)

但是為軸創建調用相同的東西不起作用:

ax = plt.axes([blah], sharey=True) # throws error

無論如何要做到這一點? 我正在與之合作的是:

所需的無花果布局

使用matplotlib.gridspec.GridSpec非常簡單

gs=GridSpec(3,3)創建一個3x3網格來放置子圖

對於您的頂行和底行,我們只需要在該3x3網格上索引一個單元格(例如, gs[0,0]位於左上角)。

對於中間行,您需要跨越兩列,因此我們使用gs[1,0:2]

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig=plt.figure(figsize=(16,12))

gs = GridSpec(3,3)

# Top row
ax1=fig.add_subplot(gs[0,0])
ax2=fig.add_subplot(gs[0,1],sharey=ax1)
ax3=fig.add_subplot(gs[0,2],sharey=ax1)

# Middle row
ax4=fig.add_subplot(gs[1,0:2],sharey=ax1)

# Bottom row
ax5=fig.add_subplot(gs[2,0],sharey=ax1)
ax6=fig.add_subplot(gs[2,1],sharey=ax1)
ax7=fig.add_subplot(gs[2,2],sharey=ax1)

ax1.set_ylim(-15,10)

plt.show()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM