Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set xticks and yticks with my imshow plot?

My code

import numpy as np
import matplotlib.pyplot as plt

with open('nm.dat','r') as f:
    vst = map(float, f)

print vst    

a=np.asarray(vst)
print len(a)

a11=a.reshape(4,22)

plt.imshow(a11, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()

My image enter image description here

I would like my x axis to be marked with equidistant 0,8,16,24,32,40,48,56,64,72,80,88 ticks.For y axis 0,2,4,6,8. How to solve this?

like image 778
Richard Rublev Avatar asked Apr 05 '16 21:04

Richard Rublev


People also ask

How do I show all Xticks in Matplotlib?

Use xticks() method to show all the X-coordinates in the plot. Use yticks() method to show all the Y-coordinates in the plot. To display the figure, use show() method.

What is Xticks and Yticks in Python?

The xticks() and yticks() function takes a list object as argument. The elements in the list denote the positions on corresponding action where ticks will be displayed. ax. set_xticks([2,4,6,8,10]) This method will mark the data points at the given positions with ticks.


1 Answers

You are missing the extent argument in imshow. imshow assumes that there is a linear relation between pixels and your "physical" unit. You could just use:

plt.imshow(a11, cmap='hot', interpolation='nearest', extent=[0,88,0,8], origin='lower')

The extent variable has to be given such that extent=[xmin,xmax,ymin,ymax]. The origin='lower' argument is to specify that your [0,0] coordinate has to be placed in the bottom left of the axis. Otherwise, it is placed in the top left of the axis.

Finally, for showing only some particular ticks, you may want to use:

ax = plt.gca()
xticks = [0,8,16,24,32,40,48,56,64,72,80,88]
yticks = [0,2,4,6,8]
ax.xaxis.set_xticks(xticks)
ax.xaxis.set_yticks(yticks)
like image 101
Alejandro Avatar answered Nov 11 '22 01:11

Alejandro