Skip to content Skip to sidebar Skip to footer

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 notin dic.keys() else dic[t] for i,t inenumerate(ticks)]
## or 
# labels = [dic.get(t, ticks[i]) for i,t inenumerate(ticks)]

ax.set_yticklabels(labels)

plt.tight_layout()
plt.show()

enter image description here

Post a Comment for "Add Custom Tick With Matplotlib"