matplotlib中图例中标签的样式 [英] Styling part of label in legend in matplotlib
问题描述
是否可以以特定样式(例如,粗体或斜体)使图例的文本一部分?>
正如silvado在他的评论中提到的那样,您可以使用LaTeX渲染来更灵活地控制文本渲染.请参阅此处以获取更多信息: http://matplotlib.org/users/usetex.html >
一个例子:
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 \textbf{line 1}")
ax.plot(x, z, label = r"This is \textit{line 2}")
ax.legend()
plt.show()
请注意标签字符串前面的"r".因此,\将被视为乳胶命令,而不是像python那样解释(因此您可以键入\textbf
而不是\\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 \textbf{line 1}")
ax.plot(x, z, label = r"This is \textit{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 \textbf
instead of \\textbf
).
这篇关于matplotlib中图例中标签的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!