I am generating a density plot using three arrays:x
,y
, and z
. However, the values on the colorbar do not correspond to the arrays. For instance, when x=1
, y=0.1
, z=1
which means that the color should be dark red, not yellow according to the colorbar. How do I fix this mismatch?
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, 2, 3 , 4, 5, 6,
7, 8, 9, 10, 11, 12])
plt.imshow(z.reshape(3,4), cmap=cm.hot)
cbar=plt.colorbar()
plt.xlabel("var",size=20)
plt.ylabel("\u03B2",size=20)
y1=np.unique(y)
y1.sort()
x1=np.unique(x)
x1.sort()
yticklabs = y1
xticklabs = x1
plt.xticks(range(4), xticklabs)
plt.yticks(range(3), yticklabs)
plt.show()
The current output is
CodePudding user response:
The problem is that you assigned wrong y-ticks labels. You can fix it by using this command:
plt.yticks(range(3), reversed(yticklabs))