Matplotlib 如何设置图例的字体类型 [英] Matplotlib how to set legend's font type

查看:33
本文介绍了Matplotlib 如何设置图例的字体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改Matplotlib中图例文本的字体类型.我知道我可以做这样的事情:

I want to change the font type of the legend texts in Matplotlib. I know I can do something like this:

plt.legend(prop={'family': 'Arial'})

但是我想使用中文字体类型,我不知道应该在上面的行中输入什么姓氏.但是我确实有一个针对该中文字体类型的 fontproperties 对象.但是,我还没有找到一种设置图例的fontproperties的方法.

But I want to use a Chinese font type and I have no idea what is the family name I should put in the line above. But I do have a fontproperties object to that Chinese font type. However, I haven't found a way to set the fontproperties of the legend.

有两个问题:

  1. 如何查找特定字体的姓氏名称
  2. 如何设置图例的字体属性

推荐答案

通过将FontProperties对象(例如下面的 font )传递到 ax.legend prop 参数:

Pass the FontProperties object (such as font below) to ax.legend via the prop parameter:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.font_manager as font_manager

fig, ax = plt.subplots()
x = np.linspace(-10, 10, 100)
ax.plot(np.sin(x)/x, label='Mexican hat')

font = font_manager.FontProperties(family='Comic Sans MS',
                                   weight='bold',
                                   style='normal', size=16)
ax.legend(prop=font)
plt.show()

在 Ubuntu 上,您可以通过运行使新字体可用于您的系统

On Ubuntu, you can make new fonts available to your system by running

fc-cache -f -v /path/to/fonts/directory

我不确定在其他操作系统上是如何完成的,或者不确定其他版本的Unix上的 fc-cache 是否通用.

I'm not sure how it is done on other OSes, or how universal fc-cache is on other flavors of Unix.

一旦安装了字体以使操作系统知道它们,就可以使matplotlib 重新生成其fontList ,方法是删除〜/.cache/fontconfig 〜/.cache/matplotlib 中的文件.

Once you've installed your font(s) so that your OS knows about them, you can cause matplotlib to regenerate its fontList by deleting the files in ~/.cache/fontconfig and ~/.cache/matplotlib.

〜/.cache/matplotlib/fontList.json 文件为您提供了matplotlib知道的所有字体的可读列表.在那里,您将找到如下所示的条目:

The ~/.cache/matplotlib/fontList.json file gives you a humanly-readable list of all the fonts matplotlib knows about. There, you'll find entries that look like this:

{
  "weight": "bold",
  "stretch": "normal",
  "fname": "/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf",
  "_class": "FontEntry",
  "name": "Comic Sans MS",
  "style": "normal",
  "size": "scalable",
  "variant": "normal"
},

请注意,fname 是底层 ttf 文件的路径,并且还有一个 name 属性.您可以通过 ttf 文件的路径指定 FontProperties 对象:

Notice that the fname is the path to the underlying ttf file, and that there is a name property as well. You can specify the FontProperties object by the path to the ttf file:

font = font_manager.FontProperties(fname="/usr/share/fonts/truetype/msttcorefonts/Comic_Sans_MS_Bold.ttf")

或按名称:

font = font_manager.FontProperties(family='Comic Sans MS',
                                   weight='bold',
                                   style='normal', size=16)

<小时>

如果您不希望在系统范围内安装字体,则可以通过 fname 路径指定 FontProperties 对象,从而无需调用fc-cache 并弄乱 ~/.cache.


If you do not wish to install your font system-wide, you can specify the FontProperties object by fname path, thus by-passing the need to call fc-cache and messing with ~/.cache.

这篇关于Matplotlib 如何设置图例的字体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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