Trouble plotting data, the y-axis values are cramped for some reason

I encountered a problem plotting data as a color bar map using matplotlib and numpy.

I have a data array "b" with the shape (48, 21600) that I’m trying to plot as a color map, when I do that I run into the issue of cramped numbers on the y-axis and no useful visualization.

fig, ax = plt.subplots(1, dpi=200)
img = ax.imshow(b)
plt.grid(False)
cbar = plt.colorbar(img)
cbar.set_label('Heat')

This is how it looks like

I have tried to use figsize=() to increase the figure size on one axis but the cramped numbers remained.

fig, ax = plt.subplots(figsize=(4, 15))
img = ax.imshow(b)
plt.grid(False)
cbar = plt.colorbar(img)
cbar.set_label('Heat')

How the plot looks like after using figsize()

The data that I’m trying to plot is a numpy array and looks like this:

[[ 3.  0.  3. ...  1.  2.  5.]
 [ 0.  9.  4. ...  1.  1.  3.]
 [ 2.  3.  3. ...  0.  1.  6.]
 ...
 [ 5.  8.  8. ... 16. 14.  7.]
 [ 5.  3.  7. ...  8. 16. 10.]
 [ 9.  4.  6. ...  6. 25.  3.]]

>Solution :

It seems like imshow won’t work with non-square grids. In those cases, pcolormesh is the way to go. I tried it with a smaller array:

fig, ax = plt.subplots(1, dpi=200)
b = np.random.randn(6, 10)
mesh = ax.pcolormesh(b)
cbar = plt.colorbar(mesh)
cbar.set_label('Heat')

which gives this result:
enter image description here

I hope it helps.

Leave a Reply