简体   繁体   中英

How can I solve my dimension issue with Python pcolor?

I have a 3 lists and I would like to make a pcolor plot.

ccplot = plt.pcolor(a,b, c, vmin=np.min(c), vmax=np.max(c))

the shape of a and b are: (108,)

The problem is:

when the shape of c is (216,) I get the error:

" ValueError: not enough values to unpack (expected 2, got 1) "

and when I reshape the c to be a 2d array of the shape (10800, 2), I get the error:

" TypeError: Dimensions of C (108, 2) are incompatible with X (108) and/or Y (108); see help(pcolor) "

Please help me handle this pcolor plot. I appreciate in advance.

regards Travis h

I believe you are looking at the pcolor implementation in the wrong way. If you have c as an array of (216,) it doesn't make sense as it needs both an i, and j value to unpack to find its location in a 2D grid . pcolor needs to be able to access c[ i , j ] and for a 1D array like (216,) this is not possible.

Also when you have C (108,2) your other arrays should look like X (109,) Y(3,). Try the following example:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm

C = np.random.rand(6, 10)
X = range(11)
Y = range(7)

fig, ax0 = plt.subplots(1, 1)

c = ax0.pcolor(X, Y, C)
ax0.set_title('default: no edges')

plt.show()

So c is a 6x10 matrix that needs locations for the corners for both x and y. These then need to be of length 7, and 11 to supply all coordinates for the corners of the c matrix. For some visuals on the corners you can go to https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pcolor.html where the first image shows you how X, Y, and C are related.

Hope it helps!

The problem is that the dimensions of the arrays must be compatible. The solution could be achieved by "reshape"ing the a and b to (36,3), then making the meshgrid of those by ax.pcolormesh(). Finally the pcolor can be plotted if c is also reshaped to (36,3) ie converted into a 2D array. Pay special attention to the mesh so that it makes sense according to the application.

a = a.reshape(36,3)
b = b.reshape(36,3)
c = c.reshape(36,3)

#making the mesh
a,b = np.meshgrid(a,b)

#pcolormesh
fig, ax0 = plt.subplots(1, 1)
c = ax0.pcolor(a, b, c)
plt.show()

The pcolormesh is more suitable for larger datasets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM