Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading