Changing the axes in the graph of the matrix matplotlib two-dimensional matrix

I have a numpy 2D array that I want to build in a color panel. I am having problems changing the axis so that they display my dataset. The vertical axis goes โ€œdownโ€ from 0 to 100, while I want it to be โ€œupโ€ from 0.0 to 0.1. Therefore, I need to do two things:

  • Flip the array using np.flipud () and then flip the axis too
  • Change the labels to go from 0.0 to 0.1, not from 0 to 100

Here is an example of what my color plot plan looks like: Example of Colorbar plot

And here is the code:

data = np.load('scorr.npy') (x,y) = np.unravel_index(data.argmax(), data.shape) max=data[x][y] fig = plt.figure() ax = fig.add_subplot(111) cax = ax.imshow(data, interpolation='nearest') cbar = fig.colorbar(cax, ticks=[-max, 0, max]) cbar.ax.set_yticklabels([str(-max), '0', str(max)]) plt.show() 

Does anyone have any suggestions? Thanks in advance!

+7
source share
2 answers

You want to look at the imshow โ€œoriginโ€ and โ€œdegreeโ€ options, I think.

 import matplotlib.pyplot as plt import numpy as np x,y = np.mgrid[-2:2:0.1, -2:2:0.1] data = np.sin(x)*(y+1.05**(x*np.floor(y))) + 1/(abs(xy)+0.01)*0.03 fig = plt.figure() ax = fig.add_subplot(111) ticks_at = [-abs(data).max(), 0, abs(data).max()] cax = ax.imshow(data, interpolation='nearest', origin='lower', extent=[0.0, 0.1, 0.0, 0.1], vmin=ticks_at[0], vmax=ticks_at[-1]) cbar = fig.colorbar(cax,ticks=ticks_at,format='%1.2g') fig.savefig('out.png') 

extent and origin

+9
source

The only way I know to change the axis labels on the image graph is by manual labeling ... If someone has a cleaner method, I would like to study it.

 ax.yaxis.set_ticks(np.arange(0,100,10)) ax.yaxis.set_ticklabels(['%.2f' % 0.1/100*i for i in np.arange(0,100,10)]) 
0
source

All Articles