Add Custom Tick With Matplotlib
I would like to add a custom tick in a matplotlib figure. Currently, I add my ticks with the following command (for instance): axis.set_yticks([0.5,0.6,0.7,0.8,0.9,1.0]) I would l
Solution 1:
The other question and its answer are still valid. You cannot use dictionaries to set the ticks.
In this case however it seems your requirement is different. While the linked question asks for leaving the ticks unchanged, here you want to set the ticks manually anyways. This will require to set the ticks and ticklabels but then allows to just replace any key from a dictionary with the respective value when setting the labels.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ticks = [0.5,0.6,0.7,0.8,0.9,1.0]
ax.set_yticks(ticks)
dic = { 1.0 : "some custom text"}
labels = [ticks[i] if t not in dic.keys() else dic[t] for i,t in enumerate(ticks)]
## or
# labels = [dic.get(t, ticks[i]) for i,t in enumerate(ticks)]
ax.set_yticklabels(labels)
plt.tight_layout()
plt.show()
Post a Comment for "Add Custom Tick With Matplotlib"