[英]Matplotlib - how to underline the x-axis ticklabels?
I was trying to underline xticklabels using plot.rc
but 我试图使用
plot.rc
下划线,但是
plt.rc('text', usetex=True)
xtext = plt.xticks()
xtext[0] = tuple(r'\underline{}'.format(x) for x in xtext[0])
But the above code gives me an error `'tuple' object does not support item assignment' 但是上面的代码给我一个错误的“'元组'对象不支持项目分配”
Same error with: 同样的错误:
xtext[1] = plt.text(0,0,r'\underline254')
Here, xtext[0]
is an ndarray
of x-axis label values
and xtext[1]
is an silent_list
of Text xticklabel objects
这里,
xtext[0]
是一个ndarray
的x-axis label values
和xtext[1]
是一种silent_list
的Text xticklabel objects
How can I underline the xtick labels? 如何在xtick标签上加下划线?
Consider the following solution. 考虑以下解决方案。 Here I am creating a new list of modified tick labels with underscores.
在这里,我正在创建带下划线的修改后的刻度标签的新列表。 Then I assign them as the new tick labels using the axis object
ax
and set_xticklabels
. 然后,使用轴对象
ax
和set_xticklabels
将它们分配为新的刻度标签。 I have replaced the format
with the old style of string formatting using %
as for some reason, the format
method underlines only the first character. 由于某种原因,我已将
format
替换为使用%
的旧format
字符串格式化,因为format
方法仅在第一个字符下划线。
import matplotlib.pyplot as plt
from matplotlib import rc
rc('text', usetex=True)
fig, ax = plt.subplots()
plt.plot(range(5))
xtext = plt.xticks()
xtext_new = [r'\underline{%s}' %x for x in xtext[0]]
ax.set_xticklabels(xtext_new)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.