出于某種原因,我找不到這方面的資訊(我很確定它存在于某處),但在下面的通用示例中,我想減少 ax1 和 ax2 之間的 hspace,同時保持 ax2-ax3 和ax3-ax4。
我也很感激任何指向此類示例的鏈接!
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i 1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure()
gs1 = GridSpec(6, 1, hspace=0.2)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])
annotate_axes(fig)
plt.show()

uj5u.com熱心網友回復:
一種可能適合您需要的方法是創建一個子網格(在本例中,將 hspace 設定為 0):
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def annotate_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i 1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure()
gs1 = GridSpec(6, 1, hspace=0.2)
# subgrid for the first two slots
# in this example with no space
subg = gs1[0:2].subgridspec(2, 1, hspace = 0)
# note the ax1 and ax2 being created from the subgrid
ax1 = fig.add_subplot(subg[0])
ax2 = fig.add_subplot(subg[1])
ax3 = fig.add_subplot(gs1[2:4])
ax4 = fig.add_subplot(gs1[4:6])
annotate_axes(fig)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424617.html
標籤:Python matplotlib 多轴
上一篇:將字串轉換為浮點數時出現值錯誤
