简体   繁体   English

刻度线标签修改; 如何在标签上添加任意刻度:

[英]Tick label modification; How to add an arbitrary tick on the label:

I have a plot of which I want to modify the text of the tick label. 我想要修改刻度标签的文本。 In the "Matplotlib - Modify tick label text" topic, this issue was solved. 在“ Matplotlib-修改刻度标签文本”主题中,解决了此问题。 But, I additionally want to place the 'Testing' tick on an arbitrary point, let's say on "0.7". 但是,我还想将“测试”刻度放置在任意点上,比如说“ 0.7”。 I was wondering, if there is any way to do that, preferably without the use of annotations. 我想知道是否有办法做到这一点,最好不要使用注释。

The code suggested on that topic is as follows: 关于该主题的建议代码如下:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.canvas.draw()
labels = [item.get_text() for item in ax.get_xticklabels()]
labels[1] = 'Testing'
ax.set_xticklabels(labels)
plt.show()

In this case you have to set the tick positions and then set the labels: 在这种情况下,您必须设置刻度位置,然后设置标签:

ticks = ax.get_xticks()
ticks = np.union1d(ticks, [0.7])
ax.set_xticks(ticks)
ax.set_xticklabels(newlabels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM