Matplotlib - 共享 x 轴图形的垂直高度
Matplotlib - vertical height of a shared x-axis figure
我想让两个图表的底部高度小得多。我试过 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()
你可以做到这一点,例如通过使用 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)
我想让两个图表的底部高度小得多。我试过 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()
你可以做到这一点,例如通过使用 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)