matplotlib中图例中标签的样式部分 [英] Styling part of label in legend in matplotlib

查看:28
本文介绍了matplotlib中图例中标签的样式部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将图例文本的部分设为特定样式,例如粗体斜体?

解决方案

正如 silvado 在他的评论中提到的,您可以使用 LaTeX 渲染来更灵活地控制文本渲染.请参阅此处了解更多信息:http://matplotlib.org/users/usetex.html>

示例:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt从 matplotlib 导入 rc# 激活乳胶文本渲染rc('text', usetex=True)x = np.arange(10)y = np.random.random(10)z = np.random.random(10)fig = plt.figure()ax = fig.add_subplot(111)ax.plot(x, y, label = r"This is 	extbf{line 1}")ax.plot(x, z, label = r"This is 	extit{line 2}")ax.legend()plt.show()

注意标签字符串前的r".因此, 将被视为乳胶命令,而不是像 python 那样解释(因此您可以键入 extbf 而不是 \textbf).

Is it possible to have part of the text of a legend in a particular style, let's say, bold or italic?

解决方案

As silvado mentions in his comment, you can use LaTeX rendering for more flexible control of the text rendering. See here for more information: http://matplotlib.org/users/usetex.html

An example:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc

# activate latex text rendering
rc('text', usetex=True)

x = np.arange(10)
y = np.random.random(10)
z = np.random.random(10)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, label = r"This is 	extbf{line 1}")
ax.plot(x, z, label = r"This is 	extit{line 2}")
ax.legend()
plt.show()

Note the 'r' before the strings of the labels. Because of this the will be treated as a latex command and not interpreted as python would do (so you can type extbf instead of \textbf).

这篇关于matplotlib中图例中标签的样式部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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