为图表添加副标题
我想为我的图表指定一个 18pt 大字体的标题,然后在其下方添加一个 10pt 小字体的副标题。我怎样才能在 matplotlib 中做到这一点?看来 title()
函数只接受具有单个 fontsize
属性的单个字符串。一定有办法做到这一点,但是怎么办呢?
I want to give my graph a title in big 18pt font, then a subtitle below it in smaller 10pt font. How can I do this in matplotlib? It appears the title()
function only takes one single string with a single fontsize
attribute. There has to be a way to do this, but how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我所做的是使用
title()
函数作为副标题,使用suptitle()
作为主标题(它们可以采用不同的字体大小参数)。What I do is use the
title()
function for the subtitle and thesuptitle()
for the main title (they can take different font size arguments).虽然这不能为您提供与多种字体大小相关的灵活性,但向 pyplot.title() 字符串添加换行符可能是一个简单的解决方案;
Although this doesn't give you the flexibility associated with multiple font sizes, adding a newline character to your pyplot.title() string can be a simple solution;
这是一个实现 Floris van Vugt 答案(2010 年 12 月 20 日)的 pandas 代码示例。他说:
>我所做的是使用 title() 函数作为副标题,使用 subtitle() 函数作为>主标题(它们可以采用不同的字体大小参数)。希望有帮助!
注意:我无法对该答案发表评论,因为我是 stackoverflow 的新手。
This is a pandas code example that implements Floris van Vugt's answer (Dec 20, 2010). He said:
>What I do is use the title() function for the subtitle and the suptitle() for the >main title (they can take different fontsize arguments). Hope that helps!
Note: I could not comment on that answer because I'm new to stackoverflow.
对我有用的解决方案是:
suptitle()
作为实际标题,title()
作为副标题,并使用可选参数y
进行调整code>:两段文本之间可能存在一些令人讨厌的重叠。您可以通过调整
y
的值来解决此问题,直到得到正确的结果。The solution that worked for me is:
suptitle()
for the actual titletitle()
for the subtitle and adjust it using the optional parametery
:There can be some nasty overlap between the two pieces of text. You can fix this by fiddling with the value of
y
until you get it right.我不认为有任何内置的东西,但是您可以通过在轴上方留出更多空间并使用
figtext
:ha
是水平对齐
。I don't think there is anything built-in, but you can do it by leaving more space above your axes and using
figtext
:ha
is short forhorizontalalignment
.只需使用 TeX 即可!这有效:
编辑:要启用 TeX 处理,您需要将“usetex = True”行添加到 matplotlib 参数:
我想您的计算机上还需要一个可用的 TeX 发行版。所有详细信息均在此页面给出:
http://matplotlib.org/users/usetex.html
Just use TeX ! This works :
EDIT: To enable TeX processing, you need to add the "usetex = True" line to matplotlib parameters:
I guess you also need a working TeX distribution on your computer. All details are given at this page:
http://matplotlib.org/users/usetex.html
正如此处所述,您可以使用
matplotlib.pyplot.text
对象来实现相同的效果结果:As mentioned here, uou can use
matplotlib.pyplot.text
objects in order to achieve the same result:这是我在考虑如何使用 matplotlib 来满足我的需求时编写的 hello world。它非常全面,可以满足您所有的标题和标签需求。
快速总结
以下是制作字幕的方法:只需使用固定在正确位置的常规图形文本框:
0.5
x 位置是左侧和右侧之间的中点。0.9
y 位置将其从顶部稍微向下放置,以便它最终位于图标题下方。我们使用horizontalalignment="center"
来确保它保持左右居中。官方 matplotlib 文档:
matplotlib.figure.text()
:https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.textmatplotlib.pyplot.text()
: < a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html" rel="nofollow noreferrer">https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot .text.html其他绘图功能和标题的摘要:
完整、可运行的示例:
如何添加图形标题、图形副标题、图形页脚、图形标题、轴标签、图例标签和 (x, y ) Matplotlib 中的点标签:
来自我的 eRCaGuy_hello_world 存储库的plot_hello_world_set_all_titles_axis_labels_etc.py:
另请参阅
(x, y)
文本使用plt.text()
,并使用自定义文本格式处理第一个和最后一个点Here's a hello world I wrote as I was figuring out how to use matplotlib for my needs. It's pretty thorough, for all your title and label needs.
Quick summary
Here's how to do a subtitle: just use a regular figure text box stuck in the right place:
The
0.5
x-location is the halfway point between the left and the right. The0.9
y-location puts it a little down from the top, so that it will end up under the Figure title. We usehorizontalalignment="center"
to ensure it stays centered left and right.Official matplotlib documentation:
matplotlib.figure.text()
: https://matplotlib.org/stable/api/figure_api.html#matplotlib.figure.Figure.textmatplotlib.pyplot.text()
: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.htmlSummary of other plot features and titles:
Full, runnable example:
How to add a figure title, figure subtitle, figure footer, plot title, axis labels, legend label, and (x, y) point labels in Matplotlib:
plot_hello_world_set_all_titles_axis_labels_etc.py from my eRCaGuy_hello_world repo:
See also
(x, y)
text for each point usingplt.text()
, and handle the first and last points with custom text formatting在 matplotlib 中使用以下函数设置字幕
In matplotlib use the below function to set the subtitle