How do I organize data for a pcolormesh heatmap?
All of the examples I've used use random data. My problem is getting my actual data to match the solution.
I am trying to create a Heatmap / scatterplot chart from x, y, z: x and y is the position and z is the color. They are in three arrays of the same length.
X = [-0.11, -0.06, -0.07, -0.12, ...]
Y = [0.09, 0.13, 0.17, 0.09, ...]
Z = [0.38, 0.37, 0.44, 0.33, ...]
the pcolormesh documentation doesn't seem to describe what "C" is, other than to say it might be a "masked array". Unfortunately, I don't know what it is (yet).
How do I turn my three arrays into whatever they are looking for? I tried to insert them into a numpy array and pass this, which calmed down the "shapeless" error, but the 3D array doesn't seem to be what it's looking for.
source to share
pcolormesh
requires three 2D arrays.
yours X
and Y
are exact, but they need to be run with a numpy function meshgrid
, i.e .:
import numpy as np X = [-0.11, -0.06, -0.07, -0.12, ...] Y = [0.09, 0.13, 0.17, 0.09, ...] xx, yy = np.meshgrid(X, Y)
you just need to get Z
in a form that matches xx
and yy
and everything will be installed.
For a scatter plot X
, Y
and Z
are accurate:
import matplotlib.pyplot as plt X = [-0.11, -0.06, -0.07, -0.12, ...] Y = [0.09, 0.13, 0.17, 0.09, ...] Z = [0.38, 0.37, 0.44, 0.33, ...] fig, ax = plt.subplots() ax.scatter(X, Y, c=Z) plt.show()
source to share