I want to plot some data stored in a Pandas Dataframe using matplotlib. I want to put specific labels on x axis ticks. So, I set them with:
ax.xaxis.set_ticklabels(data_frame['labels'])
That works well, but it sets a tick label for each data point, making the plot unreadable, so I tried:
ax.locator_params(axis='x', nbins=3)
which reduces the number of ticks to 3, but the labels are not corresponding to correct data points (if labels are a,b,c,d,e ..., x,y,z I get labels a,b,c instead of a,m,z or something like that). My next idea was to set tick labels positions:
ax.xaxis.set_ticks(data_frame.index.values)
but it does not work.
What works is:
ax.xaxis.set_ticklabels(data_frame['labels'][::step])
ax.xaxis.set_ticks(data_frame.index.values[::step])
without setting any locator_params
.
This is almost perfect. It fixes the ticks and labels, but when I zoom the plot (using the matplotlib interactive window) new labels are obviously not appearing. And what I need are readable ticks that adjust themselves depending on plot zoom (this is what ax.locator_params(axis='x', nbins=3)
does correctly without any custom labels).
In other words: I need to set specific label for each data point but show only few of them on the plot axis ticks without losing the correct assignment.