我有2个子图,第一个具有固定比率。下部子图我不在乎比率,但宽度应与上部子图对齐。
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()
如何使两个子图具有相同的宽度?
更新:
我成功了!
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()
答案 0 :(得分:1)
这是您要找的东西吗? 两个子图具有相同的宽度
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()