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. But, I additionally want to place the 'Testing' tick on an arbitrary point, let's say on "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归因
不隶属于 StackOverflow
scroll top