please have a look at the following issue.
Why is this working:
y=[1,2]; x= [0,1,2,3]
z = [6, 2, 3, 5, 2, 4, 4, 5]
z = np.reshape(z, (2, 4))
plt.pcolormesh(x, y, z)
But this is not:
y=[1]; x= [0,1,2,3]
z = [6, 2, 3, 5]
z = np.reshape(z, (1, 4))
plt.pcolormesh(x, y, z)
Or in other words, how can I use "pcolormesh" for a 1D array?
CodePudding user response:
pcolormesh
is expecting x
and y
to be the boundaries of the mesh. So, there should be one x
value and one y
value more to set the necessary boundaries. You'll need 2
y
boundaries and 5
x
boundaries for a 1
by 4
mesh.
When working with 2D meshes, pcolormesh
also allows x
and y
to indicate the center of the mesh. This is your first example. Note, matplotlib calculates boundaries in between the given values, which is quite noticeable when the x
or the y
values aren't equidistant. If only one y value is given, matplotlib doesn't have a simple way to calculate in between boundaries.
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))
y = [1, 2]
x = [0, 1, 2, 3]
z = [6, 2, 3, 5, 2, 4, 4, 5]
z = np.reshape(z, (2, 4))
ax1.pcolormesh(x, y, z)
y = [1, 2]
x = [0, 1, 2, 3, 4]
z = [6, 2, 3, 5]
z = np.reshape(z, (1, 4))
ax2.pcolormesh(x, y, z)
plt.tight_layout()
plt.show()