Invalid shape for plotting using Matplotlib

Viewed 30

I am trying to make a density plot using arrays x, y, and z but there is an error with the shape. I present the expected output.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np

x=np.array([1., 3., 4., 5., 1., 3., 4., 5., 1., 3., 4., 5.])
y=np.array([0.1  , 0.1  , 0.1  , 0.1  , 0.01 , 0.01 , 0.01 , 0.01 , 0.001,
       0.001, 0.001, 0.001])

z=np.array([-1.18976, -0.95538, -1.1647 , -1.11199, -1.02719, -0.83643,
       -0.94146, -0.97814, -1.27374, -1.58571, -1.65026, -1.62557])

plt.imshow(z,cmap=cm.hot,label="R0")
cbar=plt.colorbar()

plt.xlabel("var",size=20)
plt.ylabel("\u03B2",size=20)

plt.show()

The error is

in <module>
    plt.imshow(z,cmap=cm.hot,label="R0")

  File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2903, in imshow
    __ret = gca().imshow(

  File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 5609, in imshow
    im.set_data(X)

  File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\image.py", line 709, in set_data
    raise TypeError("Invalid shape {} for image data"

TypeError: Invalid shape (12,) for image data

The expected output is

enter image description here

1 Answers

first of all check if you're dividing your pixel's with 255

then reshape using np.reshape(image(values))

hope this will work for you

Related