【问题标题】:Change Matplotlib font without impacting global在不影响全局的情况下更改 Matplotlib 字体
【发布时间】:2016-02-16 04:45:59
【问题描述】:

我有一个函数可以创建、显示并保存它:

def make_a_plot():
    plt.plot(x,y)
    plt.show()
    plt.safefig('./my_plot.png')

我希望情节不受当前任何设置的影响,而不进行影响他们正在使用的设置的全局更改:

# User can set whatever plotting settings they want
plt.style.use('ggplot') 

def make_a_plot():

    #clear settings and modify font family
    rcdefaults()
    rcParams['font.family'] = 'sans-serif'

    plt.plot(x,y)
    plt.show()
    plt.safefig('./my_plot.png')

# Use the function
make_a_plot(x,y)

# Create a second plot with their ORIGINAL settings
plt.plot(x2,y2)
plt.show()

如何确保函数创建的绘图不受用户设置的影响,以及函数后创建的绘图不受使用函数的影响?

【问题讨论】:

    标签: python matplotlib plot settings


    【解决方案1】:

    您的问题在范围上有些不清楚。

    是您想要控制/覆盖的字体属性(仅),而不考虑全局设置,还是您想要忽略所有现有设置。

    如果你只想控制字体属性,你可以使用fontproperties参数:

    import matplotlib.pyplot as plt
    import matplotlib.font_manager as fm
    
    # Plt with customized font settings
    def make_plot(ax, x, y):
        # Set the font dictionaries (for plot title and axis titles)
        title_font = {'fontname':'Arial', 'size':'16', 'color':'blue', 'weight':'normal'}   # This can be a long dictionary
    
        # Set the font properties for other places
        font_path = 'C:\Windows\Fonts\comic.ttf'
        font = fm.FontProperties(fname=font_path, size=14)
        axis_font = fm.FontProperties(fname=font_path, size=13)
    
        # Set the tick labels font
        for label in (ax.get_xticklabels() + ax.get_yticklabels()):
            # label.set_fontname('Arial')
            # label.set_fontsize(13)
            label.set_font_properties(font)
    
        ax.plot(x, y, 'r-', label=u'Thin Red Line')
        ax.set_xlabel(u"X axis", fontproperties=axis_font)
        ax.set_ylabel(u"Y axis", fontproperties=axis_font)
        ax.set_title(u"This is the Title", **title_font)
        ax.legend(loc='lower right', prop=font, numpoints=1)
        ax.text(0.1, 5, u"Some text", fontproperties=font)
    
    # A regular plot with global fong settings:
    def regular_plot(ax, x, y):
        ax.plot(x,y, 'b-', label=u'Blue Line')
        ax.set_xlabel(u"X axis")
        ax.set_ylabel(u"Y axis")
        ax.set_title(u"This is the Title", axes=ax1)
        ax.legend(loc='lower right', numpoints=1)
    
    fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(9,3))
    x = y = range(0, 10) # Some data
    
    # 1st make a regular plot:
    regular_plot(ax1, x, y)
    # then make customized plot:
    make_plot(ax2, x, y)
    # then agains a regular plot:
    regular_plot(ax3, x, y)
    plt.show()
    

    从生成的图形中可以看出,自定义绘图没有改变,也不受全局字体设置的影响。

    如果您正在寻找更完整的控件,您可能想看看如何使用style sheets

    【讨论】:

    • 这真的很有帮助!我只是以字体为例,我想确保没有其他设置影响图表的外观。我会接受它(尤其是因为您包含了样式表链接),但如果有人发布一个完全控制的示例(可能使用样式表),我将切换到该答案。
    • 另外,这个函数会被用户在很多不同的环境中运行,有没有没有font_path引用的方法呢?
    • font_path 只是您可以做到的严格的一个例子。如果您不需要它,只需使用title_font 的示例,您只需提供fontname。当然,这假设 OS(或 matplotlib)知道这个字体名称。
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多