【问题标题】:matplotlib: Setting single title for all figures (not subplots)matplotlib:为所有图形设置单个标题(不是子图)
【发布时间】:2021-08-09 11:04:54
【问题描述】:

我想给我的代码中的所有数字一个单独的标题#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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    我不相信 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='*')
       
    

    【讨论】:

    • 您好,我们如何通过此功能将所有数字保存在一个文件夹中
    猜你喜欢
    • 2018-05-26
    • 2021-11-13
    • 2017-06-15
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多