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='*')

最新更新