Matplotlib - 共享x轴图形的垂直高度

时间:2015-01-16 16:59:14

标签: python python-2.7 matplotlib

我想让两个图表的底部有一个更小的高度。我试过set_yscale(1,.5),但它没有成功,正在寻找如何做到这一点。无法在文档中找到任何内容。

import matplotlib.pyplot as plt
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)



# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(2, sharex=True)
axarr[0].plot(x, y)
axarr[0].set_title('Sharing X axis')
axarr[1].scatter(x, y)
axarr[1].set_yscale(1,.5)

plt.show()

1 个答案:

答案 0 :(得分:2)

你可以实现这一点,例如通过使用GridSpec来定位图中的子图。这会给您的代码增加一些开销,但会为您提供绘图位置及其相对宽度和高度的完全灵活性。

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# create subplots' axes
fig = plt.figure()
top_pos, bot_pos = gridspec.GridSpec(2, 1, height_ratios=[4, 1])
top_ax = fig.add_subplot(top_pos)
bot_ax = fig.add_subplot(bot_pos, sharex=top_ax)

# do the plotting
top_ax.set_title('Sharing X axis')
top_ax.plot(x, y)
bot_ax.scatter(x, y)

result