I would like to set y-ticker labels, but still have them bound to the data. Via this question, matplotlib: change yaxis tick labels, I am trying to do this with matplotlib.ticker
.
I currently just want tick marks at three positions: [.25, .5, .75]
. The graph generates correctly, but with the print statements, I can see it iterates over each label twice and some other random values that are not in my dictionary and thus generate key errors on this line: return label_lookup[x]
. The values do not appear to be the same it's run each time. What is causing this issue?
import matplotlib as mpl
import matplotlib.pyplot as plt
label_lookup = {.25: 'Label 1',
.5: 'Label 2',
.75: 'Label 3'}
def mjrFormatter(x, pos):
print x, pos
return label_lookup[x]
ax = plt.gca()
ax.set_yticks([.25,.5,.75], minor=False)
ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))
plt.draw()
plt.show()