I have a function returnValuesAtTime
that returns three lists-x_vals
,y_vals
and swe_vals
. All three lists are of the same length and each element in swe_vals
corresponds to a x-value
from x_vals
and a y-value
from y_vals
. I wish to generate a heatmap with a legend that uses the (x,y)
coordinates and the swe_vals
as the intensity.
I wrote the following code:
def plotValuesAtTimeMap(t):
x_vals,y_vals,swe_vals=returnValuesAtTime(t)
x_points=len(x_vals)
y_points=len(y_vals)
xx=np.linspace(x_vals[0],x_vals[-1],x_points)
yy=np.linspace(y_vals[0],y_vals[-1],y_points)
fig,ax=plt.subplots()
im=ax.pcolormesh(xx,yy,z)
fig.colorbar(im)
ax.axis('tight')
plt.show()
Once the three lists are obtained using returnValuesAtTime(t)
, I take the lengths of x_vals
and y_vals
. I then use these to generate the spacing for x and y-directions, with the limits being the first and last elements of x_vals
and y_vals
. I then try to generate the colormesh
. But this gives me an empty colormesh
with no values.
What might be going wrong? Will using a 3D numpy
array instead of the three lists solve the problem?
The first 10 elements of each of the lists are:
x_vals[0:10]
[439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893,
439936.86573189893]
y_vals[0:10]
[4514018.2797159087,
4513898.2797159087,
4513778.2797159087,
4513658.2797159087,
4513538.2797159087,
4513418.2797159087,
4513298.2797159087,
4513178.2797159087,
4513058.2797159087,
4512938.2797159087]
swe_vals[0:10]
[2.7520323,
2.7456229,
2.7456021,
2.745455,
2.7478349,
2.7445269,
2.7484877,
2.7524617,
2.75491,
2.7509627]
Edit:
I have added a scatter plot showing the (x,y) grid range below:
The x and y values are in lists, each 6804 in length. Each (x,y) point has a corresponding z-value in a separate list, which is also 6804 in length. To clarify what I hope to achieve, I want to generate a heatmap like plot with the magnitude of z-axis represented by the color of each grid on the plot. Something like what's shown below:
In the example plot, all z-values are the same. So the color is same across the entire grid space.
Edited with new resultant plot (based on member CT Zhu's suggestions):
It looks like if reshape x
, y
, z
to square matrix, you can do a contourf
plot:
In [7]:X
Out[7]:
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
In [8]:Y
Out[8]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])
plt.contourf(X,Y,np.random.random((10,10))) #reshape Z too!
plt.colorbar()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With