Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用样式表时自定义字体_Python_Matplotlib - Fatal编程技术网

Python 使用样式表时自定义字体

Python 使用样式表时自定义字体,python,matplotlib,Python,Matplotlib,我使用的是'ggplot'样式表。现在,样式很好,只是我想特别更改字体样式可能吗? plt.imshow(ori, vmin = 0, vmax = 300) plt.style.use('ggplot') plt.show() 如果找到有关自定义样式的。但是,我只想更改字体,同时保留样式的其余部分 还有,是否有人知道在哪里可以看到每个样式的设置细节(如字体、figsize等)? plt.imshow(ori, vmin = 0, vmax = 300) plt.style.use('ggp

我使用的是
'ggplot'
样式表。现在,样式很好,只是我想特别更改字体样式可能吗?

plt.imshow(ori, vmin = 0, vmax = 300)
plt.style.use('ggplot')
plt.show() 
如果找到有关自定义样式的。但是,我只想更改字体,同时保留样式的其余部分

还有,是否有人知道在哪里可以看到每个
样式的设置细节(如字体、figsize等)?

plt.imshow(ori, vmin = 0, vmax = 300)
plt.style.use('ggplot')
plt.show() 
尝试rcParams

matplotlib.rcParams.update({'font.size': 12})

阅读更多

是的,这是可能的。您可以在本地通过将字体传递给各个标签来实现

font = {'fontname':'your font'}
plt.xlabel('xlabel', **hfont)
plt.ylabel('xlabel', **hfont)
还是全球

 import matplotlib.pyplot as plt
 plt.rcParams['font.family'] = 'your font'
组合风格 我觉得最优雅的就是去

例如,您可以在
mystyle.mplstyle
中定义自己的字体设置(请参见下面的保存位置和外观)。要使用自己的字体设置获得
ggplot
样式,您只需指定:

plt.style.use(['ggplot', 'mystyle'])
此解决方案非常优雅,因为它允许在所有绘图中使用一致的应用程序,并允许您进行混合和匹配

在哪里保存自定义样式? 取自我自己的一种风格
mystyle.mplstyle
可以有以下条目(您应该根据需要定制):

您应该将其保存在matplotlib的配置目录中。对我来说,这是
~/.matplotlib/stylelib/
,但是使用

import matplotlib
matplotlib.get_configdir()
了解在操作系统上使用什么。看见您还可以编写一个Python函数安装在正确的位置

在哪里可以找到现有样式? 然后是问题的最后一部分。首先,知道您可以使用以下方法获得具有可用样式的列表是很有用的

import matplotlib.pyplot as plt
plt.style.available
有关图形表示,请参见

如何检查例如
ggplot.mplstyle
?我认为这是最好的参考。您还可以在系统上找到
*.mplstyle
文件。但是,具体位置取决于您的操作系统和安装。对我来说

find / -iname 'ggplot.mplstyle' 2>/dev/null
给予

或者更一般地说,您可以搜索所有样式:

find / -iname '*.mplstyle' 2>/dev/null
对于Windows,我并不是一个真正的专家,但上面列出的文件路径可能会给你一个线索,让你去哪里寻找

将样式安装到正确位置的Python脚本 要在正确的位置安装自定义样式,可以构建如下脚本:

def copy_style():

    import os
    import matplotlib

    # style definition(s)

    styles = {}

    styles['mystyle.mplstyle'] = '''
font.family          : serif
'''

    # write style definitions

    # directory name where the styles are stored
    dirname = os.path.abspath(os.path.join(matplotlib.get_configdir(), 'stylelib'))

    # make directory if it does not yet exist
    if not os.path.isdir(dirname): os.makedirs(dirname)

    # write all styles
    for fname, style in styles.items():
        open(os.path.join(dirname, fname),'w').write(style)

非常感谢。它真的很有用而且系统!
def copy_style():

    import os
    import matplotlib

    # style definition(s)

    styles = {}

    styles['mystyle.mplstyle'] = '''
font.family          : serif
'''

    # write style definitions

    # directory name where the styles are stored
    dirname = os.path.abspath(os.path.join(matplotlib.get_configdir(), 'stylelib'))

    # make directory if it does not yet exist
    if not os.path.isdir(dirname): os.makedirs(dirname)

    # write all styles
    for fname, style in styles.items():
        open(os.path.join(dirname, fname),'w').write(style)