matplotlib图表区与p

2024-05-17 04:03:56 发布

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

在matplotlib中,如何控制绘图区域的大小与图表的总面积?

我使用以下代码设置图表区域的大小:

fig = plt.gcf()
fig.set_size_inches(8.11, 5.24)

但是,我不知道如何设置绘图区域的大小,因此当我输出图表时,x轴上的图例会被切成两半。


Tags: 代码区域绘图sizematplotlib图表figplt
1条回答
网友
1楼 · 发布于 2024-05-17 04:03:56

我想举个例子可以帮助你。图形大小figsize可以设置绘图将驻留的窗口的大小。轴列表参数[left, bottom, width, height]确定图中的位置以及将覆盖多少区域。

因此,如果运行下面的代码,您将看到窗口大小为8x6英寸。在这个窗口中将有一个主图big_ax,占总面积的0.8x0.8。第二个图的大小为总面积的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()

enter image description here

相关问题 更多 >