matplotlib,python:标题的一部分以粗体显示 [英] matplotlib, python: part of title in bold font

查看:89
本文介绍了matplotlib,python:标题的一部分以粗体显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个轴标题,其中一部分为粗体.比如我比较两个数据集,计算R^2,如果有统计意义,我想在标题中加粗R^2.到目前为止,我只能将整个标题加粗.

I would like to create an axis title that has part of it in bold font. For example, I am comparing two data sets and computing R^2, and if there is statistical significance, I would like to print R^2 bold into the title. So far I can only make the entire title bold.

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = plt.subplot()
# example R^2 
r1 = np.random.random()
ax1.set_title('part of title that should not be bold, $R^2$: {0}'.format(np.around(r1, 3)), weight='bold')

我只希望"R ^ 2"后的数字加粗.

I only want the number after "R^2" to be bold.

谢谢!

下面的答案建议使用粗体 MathText,例如:r"$\bf{0.333}$".这样会生成乳胶样式的数字,但是它仍然不是粗体.这是从下面粘贴到新的ipython会话中的代码,以及结果:

The answer below suggests to use bold MathText, such as: r"$\bf{0.333}$". This results in Latex-style numbers, but it's still not bold. Here is the code from below pasted into a new ipython session, and the result:

推荐答案

以下内容似乎仅适用于matplotlib版本2或更高版本.

您可以在标题内使用粗体MathText来使文本的一部分变为粗体,例如 r"$ \ bf {0.333} $" .请注意,这是原始字符串( r" ).如果我们想用括号格式化字符串,它们必须被双重转义,

One can use bold MathText inside the title to make part of the text bold, e.g. r"$\bf{0.333}$". Note that this is a raw string (r""). If we want to format the string with brackets, they have to be double escaped,

`r"$\bf{{{x}}}$".format(x=0.333)`

完整示例:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = plt.subplot()
# example R^2 
r1 = np.random.random()
ax1.set_title(r'non-bold part of title, $R^2$: $\bf{{{a}}}$'.format(a=np.around(r1, 3) ) )

plt.show()

这篇关于matplotlib,python:标题的一部分以粗体显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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