Changing values ​​on the graph axis matplotlib imshow ()

Let's say I have some input:

data = np.random.normal(loc=100,scale=10,size=(500,1,32)) hist = np.ones((32,20)) # initialise hist for z in range(32): hist[z],edges = np.histogram(data[:,0,z],bins=np.arange(80,122,2)) 

I can build it using imshow() :

 plt.imshow(hist,cmap='Reds') 

receipt:

enter image description here

However, the x-axis values ​​do not match the input (i.e., the average value is 100, from 80 to 122). So I would like to change the x axis to show the values ​​in edges .

I tried:

 ax = plt.gca() ax.set_xlabel([80,122]) # range of values in edges ... # this shifts the plot so that nothing is visible 

and

 ax.set_xticklabels(edges) ... # this labels the axis but does not centre around the mean: 

enter image description here

Any ideas on how to change the axis values ​​to reflect the input I'm using?

+58
python numpy matplotlib
Sep 09 '13 at 10:23
source share
2 answers

I would try to avoid changing xticklabels if possible, otherwise it can become very confusing if, for example, you close your histogram with additional data.

Defining the range of your grid is probably the best, and you can do it with imshow adding the extent keyword. In this way, the axes are automatically adjusted. If you want to change the shortcuts, I would use set_xticks , perhaps with some formatting. Incorrect labeling should be the last.

 fig, ax = plt.subplots(figsize=(6,6)) ax.imshow(hist, cmap=plt.cm.Reds, interpolation='none', extent=[80,120,32,0]) ax.set_aspect(2) # you may also use am.imshow(..., aspect="auto") to restore the aspect ratio 

enter image description here

+95
Sep 09 '13 at 10:38
source share
β€” -

I had a similar problem and Google sent me to this post. My solution was a little different and less compact, but hopefully this might come in handy for someone.

Displaying your image with matplotlib.pyplot.imshow is usually a quick way to display 2D data. However, this by default marks axes with the number of pixels. If the 2D data you output matches some uniform grid defined by x and y arrays, then you can use matplotlib.pyplot.xticks and matplotlib.pyplot.yticks to mark the x and y axes using the values ​​in these arrays. They will associate some labels corresponding to the actual grid data with the number of pixels on the axes. And to do this is much faster than, for example, using something like pcolor.

Here is an attempt to do this with your data:

 import matplotlib.pyplot as plt # ... define 2D array hist as you did plt.imshow(hist, cmap='Reds') x = np.arange(80,122,2) # the grid to which your data corresponds nx = x.shape[0] no_labels = 7 # how many labels to see on axis x step_x = int(nx / (no_labels - 1)) # step between consecutive labels x_positions = np.arange(0,nx,step_x) # pixel count at label position x_labels = x[::step_x] # labels you want to see plt.xticks(x_positions, x_labels) # in principle you can do the same for y, but it is not necessary in your case 
0
Dec 12 '18 at 15:59
source share



All Articles