python - matplotlib:为所有图形设置单个标题(不是子图)
问题描述
我想给我的代码中的所有数字一个单独的标题#Variable_cycles
。数字不是子图,而是单独绘制的。我正在使用 %matplotlib 在单独的窗口中显示绘图。据我所知 plt.rcParams 没有这样的密钥
import matplotlib.pyplot as plt
%matplotlib
plt.figure(1), plt.scatter(x,y,marker='o'),
plt.title("Variable_cycles"),
plt.show
plt.figure(2),
plt.scatter(x,y,marker='*'),
plt.title("Variable_cycles"),
plt.show
解决方案
我不相信 rcParams 或类似设置中有这样的设置,但如果您为所有图形设置了选项,您可以创建一个简单的辅助函数来创建图形,应用这些设置(例如标题、轴标签、等),并返回图形对象,那么您只需要为每个新图形调用一次该函数。一个简单的例子是:
import matplotlib.pyplot as plt
%matplotlib
def makefigure():
# Create figure and axes
fig, ax = plt.subplots()
# Set title
fig.suptitle('Variable cycles')
# Set axes labels
ax.set_xlabel('My xlabel')
ax.set_ylabel('My ylabel')
# Put any other common settings here...
return fig, ax
fig1, ax1 = makefigure()
ax1.scatter(x, y, marker='o')
fig2, ax2 = makefigure()
ax2.scatter(x, y, marker='*')
推荐阅读
- mongodb - MongoDB查询-展开并匹配保留null或不同的值/根据条件添加新字段
- python - 为什么 reverse(string[0:]) 在 python 中显示错误,为什么我不能使用它而不是 reverse(string[1:]) + string[0]
- typescript - 打字稿泛型返回类型
- java - Spring WebClient - 如何在 HTTP 错误(4xx、5xx)的情况下访问响应正文?
- less-unix - 退出后,如何在终端上保持较少的输出?
- python - 如何从主 python 文件中正确运行单独的 python 脚本?
- python - “python setup.py test”与“pytest {package}”有什么区别?
- python - Numba 并行 prange 无法扩大 - 非常慢
- python - python - 为什么在Python中需要类`Logger`的方法`warning`中的`args`?
- c# - 如何从数组中随机选择两个字符串而不重复