更改Matplotlib字体而不影响全局 [英] Change Matplotlib font without impacting global

查看:289
本文介绍了更改Matplotlib字体而不影响全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建绘图,显示并保存的函数:

I have a function that creates a plot, displays it, and saves it:

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

我希望该图不受当前任何设置的影响,而不会进行会影响其使用设置的全局更改:

I want to the plot to be un-impacted by any settings currently in place without making global changes that impact the settings they are using:

# 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()

如何确保该功能创建的绘图不受用户设置的影响并且,并且确保该功能创建后的绘图不受使用该功能的影响?

How can I ensure that the plot created by the function is not impacted by the user's settings and that plots created after the function are not impacted by the use of the function?

推荐答案

您的问题在范围上尚不清楚.

Your question is somewhat unclear on the scope.

是要控制/覆盖的字体属性(仅),而不管全局设置是什么,还是要忽略所有现有设置?

Is it the font properties (only) that you want to control / override regardless of the global settings or are do you want ignore ALL existing settings.

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

If you want to control font properties only you could use fontproperties parameter:

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()

从结果图中可以看到,自定义图没有变化,也不受全局字体设置的影响.

As you can see from the resulting graph customized plot did not change and was not affected by the global font setting.

如果您正在寻找更完整的控件,则可能需要查看如何使用样式表

If you are looking for a more complete control you might want to look at how to use style sheets

这篇关于更改Matplotlib字体而不影响全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆