How to change the amount of increments in pyplot axis


Question

Hi probably quite a simple question but..
When plotting a graph using matplotlib.pyplot my Y axis goes from -0.04 to 0.03 which is fine but there are 8 labels for increments (eg 0.03,0.02,0.01 etc.). I need more maybe 16 or so.

Thanks for your help

1
4
2/27/2015 8:31:10 AM

Accepted Answer

Use set_yticks() to change the tick locations. For example:

import scipy, pylab
fig = pylab.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(scipy.randn(8))
ax.set_yticks(scipy.arange(-1.5,1.5,0.25))
fig.show()

pylab.yticks() is another option. More ticks
(source: stevetjoa.com)

2
7/29/2019 7:05:43 AM

Matplotlib has several different algorithms for choosing tick locations automatically, and e.g. LinearLocator or MaxNLocator may suit your purpose. See the major_minor demo for how to use Locators in general, and the ticker api documentation for the various Locators available. The documentation for the individual classes is somewhat sparse, but guessing based on the argument names tends to work fine.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon