如何使附加的子图与现有的固定长宽子图宽度相等?

2024-09-19 23:42:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个子地块,第一个子地块的比例是固定的。下面的子图我不关心比例,但宽度应该与上面的子图对齐。你知道吗

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(211, aspect='equal', autoscale_on=False,   xlim=(0, 80), ylim=(0, 40))

ax.plot([0,10,20,40,60,70], [1,4,3,2,6,5], 'bo')

ax1 = fig.add_subplot(212, xlim=(0, 8000),  ylim=(-200, 0))
ax1.plot([0,8000],[-200,0], '-')

plt.show()

enter image description here

如何使两个子地块具有相同的宽度?你知道吗

更新:

我成功了!你知道吗

import matplotlib.pyplot as plt
import numpy as np

gkw = {'height_ratios':[1, 2] } 
fig, (ax1, ax2) = plt.subplots(2, 1,  gridspec_kw = gkw )

ax1.set_aspect('equal')
ax1.set_autoscale_on(False)
ax1.set_xlim(left=0,   right=80)
ax1.set_ylim(bottom=0, top=40)

ax1.plot([0,10,20,40,60,70], [1,4,3,2,6,5], 'bo')

ax2.set_xlim(left=0,   right=8000)
ax2.set_ylim(bottom=-200, top=0)

ya = np.diff(np.array(ax2.get_ylim()))[0] 
xa = np.diff(np.array(ax2.get_xlim()))[0] 
wa = gkw['height_ratios'][0]/float(gkw['height_ratios'][1]) 
ia = 40/80

ax2.set_aspect(float(1/wa*ia/(ya/xa)))
ax2.plot([0,8000],[-200,0], '-')
plt.show()

Tags: importplotasnpfigpltheightset
1条回答
网友
1楼 · 发布于 2024-09-19 23:42:06

这就是你要找的吗? 两个子地块的宽度相同

import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.set_xlim((0,80))
ax1.set_ylim((0,40))
ax1.plot([0,10,20,40,60,70], [1,4,3,2,6,5], 'bo')
ax2.set_xlim((0,8000))
ax2.set_ylim((-200,0))
ax2.plot([0,8000],[-200,0], '-')

plt.show()

相关问题 更多 >