I am trying to fix how python plots my data.
Say
x = [0,5,9,10,15]
and
y = [0,1,2,3,4]
Then I would do:
matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()
and the x axis' ticks are plotted in intervals of 5. Is there a way to make it show intervals of 1?
This is a bit hacky, but by far the cleanest/easiest to understand example that I've found to do this. It's from an answer on SO here:
Cleanest way to hide every nth tick label in matplotlib colorbar?
Then you can loop over the labels setting them to visible or not depending on the density you want.
edit: note that sometimes matplotlib sets labels ==
''
, so it might look like a label is not present, when in fact it is and just isn't displaying anything. To make sure you're looping through actual visible labels, you could try:I like this solution (from the Matplotlib Plotting Cookbook):
This solution give you explicit control of the tick spacing via the number given to
ticker.MultipleLocater()
, allows automatic limit determination, and is easy to read later.Another approach is to set the axis locator:
There are several different types of locator depending upon your needs.
In case anyone is interested in a general one-liner, simply get the current ticks and use it to set the new ticks by sampling every other tick.
This is an old topic, but I stumble over this every now and then and made this function. It's very convenient:
One caveat of controlling the ticks like this is that one does no longer enjoy the interactive automagic updating of max scale after an added line. Then do
and run the resadjust function again.
I developed an inelegant solution. Consider that we have the X axis and also a list of labels for each point in X.
Example: Let's say that I want to show ticks labels only for 'feb' and 'jun' Good, now we have a fake list of labels. First, we plotted the original version. Now, the modified version.