在 matplotlib 中,如何控制绘图区域的大小与图表的总面积?
我使用以下代码设置图表区域的大小:
fig = plt.gcf()
fig.set_size_inches(8.11, 5.24)
但是,我不知道如何设置绘图区域的大小,结果当我输出图表时,x 轴上的图例被切成两半。
请您参考如下方法:
我想一个例子可以帮助你。图形大小 figsize 可以设置绘图所在的窗口大小。坐标轴列表参数 [left, bottom, width, height] 确定绘图在图中的位置以及将覆盖多少区域。
因此,如果您运行下面的代码,您将看到窗口的大小为 8x6 英寸。在该窗口内将是一个占总面积 0.8x0.8 的主图 big_ax。第二个地 block small_ax 的总面积为 0.3x0.3。
import matplotlib.pyplot as plt
import numpy as np
x1 = np.random.randint(-5, 5, 50)
x2 = np.random.randn(20)
fig = plt.figure(figsize=(8,6)) # sets the window to 8 x 6 inches
# left, bottom, width, height (range 0 to 1)
# so think of width and height as a percentage of your window size
big_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
small_ax = fig.add_axes([0.52, 0.15, 0.3, 0.3]) # left, bottom, width, height (range 0 to 1)
big_ax.fill_between(np.arange(len(x1)), x1, color='green', alpha=0.3)
small_ax.stem(x2)
plt.setp(small_ax.get_yticklabels(), visible=False)
plt.setp(small_ax.get_xticklabels(), visible=False)
plt.show()
