Python matplotlib imshow () user marks

I am trying to set custom marks on my imshow () output, but could not find the correct combination.

The following is a description of the script. In this script, I am trying to mark all the even numbers on each axis instead of the default value (-10, -5,0,5,10)

#!/usr/bin/env python import matplotlib.pyplot as plt import numpy as np #Generate random histogram N=25 middle=N/2 hist=np.random.random_sample((N,N)) #Ticks at even numbers, data centered at 0 ticks=np.arange(-middle,middle+2,2) extent=(-middle,middle,-middle,middle) plt.imshow(hist, interpolation='nearest', extent=extent, origin='lower') plt.colorbar() # #These are my attempts to set the tick marks # #plt.gcf().gca().set_ticks(ticks) #plt.gca().set_ticks(ticks) #ax=plt.axes() #ax.set_ticks(ticks) plt.show() 

I get the feeling that set_ticks () might not be able to do this, but I'm not sure what else to try.

Thanks!

+8
python matplotlib plot histogram
source share
1 answer

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.xticks

 plt.xticks(ticks) 

Edit: as Yann mentions in a comment, you might also be interested in plt.yticks()

Result (using plt.xticks(ticks, fontsize=9) ): enter image description here

+12
source share

All Articles