I would like to display a font in Times New Roman in the legend of a matplotlib plot.
I have changed all other tick labels/axis labels/titles to Times New Roman, and have searched the documentation but I can only find how to change the font size in a legend using the prop
argument in pyplot.legend()
我想在一个matplotlib情节的图例中显示一种Times New Roman字体。我已经将所有其他记号标签/轴标签/标题更改为Times New Roman,并搜索了文档,但我只能找到如何使用pyplot.Legend()中的prop参数更改图例中的字体大小。
Of course straight after posting, I found the answer. Solution for anyone else with the same issue:
当然,在发帖后,我立即找到了答案。任何其他有相同问题的人的解决方案:
import matplotlib as mpl
mpl.rc('font',family='Times New Roman')
更多回答
add your answer as an answer so it can be accepted and taken out of the 'unanswered' questions queue. welcome to StackOverflow!
将您的答案添加为答案,以便可以接受该答案,并将其从“未回答”问题队列中删除。欢迎来到StackOverflow!
This wasn't showing up in google results so I'm going to post it as an answer. The rc parameters for font can be used to set a single default font.
这没有显示在谷歌搜索结果中,所以我会把它作为一个答案发布出来。字体的RC参数可用于设置单个默认字体。
Solution for anyone else with the same issue:
任何其他有相同问题的人的解决方案:
import matplotlib as mpl
mpl.rc('font',family='Times New Roman')
The .rc
solution given changes the default font for all drawing.
给定的.rc解决方案会更改所有工程图的默认字体。
Here is a solution for doing this when you don't want to change all the fonts, but just the font properties of the legend of this particular graph (a legend belonging to a particular axis
object):
当您不想更改所有字体,而只想更改此特定图形(属于特定轴对象的图例)的图例的字体属性时,以下是一种解决方案:
L = ax.legend()
plt.setp(L.texts, family='Consolas')
This allows you to choose a different font for the legend and the axes. I found this helpful when I needed a monospace font for my legend, but not for the axes -- allowing me to create a neat legend like this:
这允许您为图例和轴选择不同的字体。当我的图例需要等宽字体而不是轴的时候,我发现这很有帮助--允许我创建一个整洁的图例,如下所示:
Note how the title is a different font than the legend - this gives me an alignment of numbers that would otherwise be hard to achieve.
注意标题与图例的字体是如何不同的--这给了我一个数字对齐,否则很难实现。
I think this is the better way.
我认为这是更好的方式。
import matplotlib.font_manager as fm
## your font directory
font_path = '/Users/frhyme/Library/Fonts/BMDOHYEON_otf.otf'
## font_name
font_name = fm.FontProperties(fname=font_path).get_name()
plt.legend(prop={'family':font_name, 'size':20})
Here is an example from 2023
以下是2023年的一个例子
plt.legend(
labels,
bbox_to_anchor=[1, 0, 0.5, 1],
prop={'family':'monospace'}
)
prop={'family':'monospace'}
- change legend font
bbox_to_anchor=[1, 0, 0.5, 1]
- move legend to right upper conner
道具={‘Family’:‘monSpace’}-更改图例字体BBOX_TO_ANCOUNT=[1,0,0.5,1]-将图例移动到右上角
更多回答
good except that Consolas does not seem to come with standard installation or not a font family. 'monospace'
works for me
很好,只是Consolas似乎没有提供标准安装,也没有字体系列。‘MonSpace’对我很管用
@DimaLituiev - Consolas is a standard monospace font on a Mac. I guess you have a Windows machine? Anyway - this was intended as a guide, not a prescription...
@DimaLituiev-Consolas是Mac上的标准等宽字体。我猜你有一台Windows机器吧?无论如何-这是一个指南,而不是处方。
On my mac 10.10, open -a /Applications/Font\ Book.app
shows 9 monospace fonts, but only Courier
Courier New
and monospace
work in Matplotlib 2.2.3. See also font_manager
在我的Mac 10.10上,打开-a/Applications/Font\Book.app显示了9种等宽字体,但只有Courier Courier New和等宽字体可以在Matplotlib 2.2.3中使用。另请参阅Font_Manager
我是一名优秀的程序员,十分优秀!