python - 使用 matplotlib,是否可以一次为图形上的所有子图设置属性?

标签 python graph numpy matplotlib

使用 matplotlib(与 Python),是否可以一次为图形上的所有子图设置属性?

我创建了一个包含多个子图的图形,目前我有这样的东西:

import numpy as np
import matplotlib.pyplot as plt

listItems1 = np.arange(0, 100)
listItems8 = np.arange(0, 100)
listItems11 = np.arange(0, 100)
figure1 = plt.figure(1)

# First graph on Figure 1
graphA = figure1.add_subplot(2, 1, 1)
graphA.plot(listItems1, listItems8, label='Legend Title')
graphA.legend(loc='upper right', fontsize='10')
graphA.grid(True)
plt.xticks(range(0, len(listItems1) + 1, 36000), rotation='20', fontsize='7', color='white', ha='right')
plt.xlabel('Time')
plt.ylabel('Title Text')

# Second Graph on Figure 1
graphB = figure1.add_subplot(2, 1, 2)
graphB.plot(listItems1, listItems11, label='Legend Title')
graphB.legend(loc='upper right', fontsize='10')
graphB.grid(True)
plt.xticks(range(0, len(listItems1) + 1, 36000), rotation='20', fontsize='7', color='white', ha='right')
plt.xlabel('Time')
plt.ylabel('Title Text 2')

plt.show()

问题,有没有办法一次设置任何或所有这些属性?我将在一个图中有 6 个不同的子图,一遍又一遍地复制/粘贴相同的“xticks”设置和“图例”设置有点乏味。

是否有某种“figure1.legend(...”之类的东西?

谢谢。第一次为我发帖。 Hello World ! ;)

最佳答案

如果您的子图实际上共享一个轴/一些轴,您可能有兴趣指定 sharex=True 和/或 sharey=True 参数到 子图

参见 John Hunter 在 this video 中的更多解释.它可以让您的图形看起来更清晰,并减少代码重复。

关于python - 使用 matplotlib,是否可以一次为图形上的所有子图设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17753398/

相关文章:

python - 如何 reshape 数据框以保留唯一信息?

python - 在 python 中使用 lambda 函数添​​加教堂数字

python - 在 Python 中的字符前添加零

algorithm - 具有具有流动能力的节点的图的 Edmonds-Karp 算法

python - Pandas OneHotEncoder.fit(dataframe) 返回 ValueError : invalid literal for long() with base 10

python - 如何覆盖Django登录

java - 过滤 HashMap 的条目并迭代值

c++ - 在至少访问 X 节点一次的图中查找最短路

python - 确定颜色是否在轮廓 OpenCV 内

python - Pandas:将特定的 Excel 单元格值读入变量