简体   繁体   English

如何使用Python pcolor解决尺寸问题?

[英]How can I solve my dimension issue with Python pcolor?

I have a 3 lists and I would like to make a pcolor plot. 我有3个清单,我想作一个pcolor图。

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

the shape of a and b are: (108,) a和b的形状为:(108,)

The problem is: 问题是:

when the shape of c is (216,) I get the error: 当c的形状为(216,)时出现错误:

" ValueError: not enough values to unpack (expected 2, got 1) " “ ValueError:没有足够的值可解包(预期2,得到1)”

and when I reshape the c to be a 2d array of the shape (10800, 2), I get the error: 当我将c重塑为形状为(10800,2)的2d数组时,出现错误:

" TypeError: Dimensions of C (108, 2) are incompatible with X (108) and/or Y (108); see help(pcolor) " “ TypeError:C(108,2)的尺寸与X(108)和/或Y(108)不兼容;请参阅help(pcolor)”

Please help me handle this pcolor plot. 请帮助我处理这个pcolor图。 I appreciate in advance. 我预先感谢。

regards Travis h 问候特拉维斯

I believe you are looking at the pcolor implementation in the wrong way. 我相信您正在错误地看待pcolor实现。 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 . 如果您将c作为(216,)的数组,则没有任何意义,因为它需要i和j值来解包才能在2D网格中找到其位置。 pcolor needs to be able to access c[ i , j ] and for a 1D array like (216,) this is not possible. pcolor需要能够访问c [i,j],对于像(216,)这样的一维数组,这是不可能的。

Also when you have C (108,2) your other arrays should look like X (109,) Y(3,). 同样,当您有C(108,2)时,其他数组应看起来像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. 因此c是一个6x10矩阵,需要x和y的拐角位置。 These then need to be of length 7, and 11 to supply all coordinates for the corners of the c matrix. 然后,它们的长度必须为7,长度为11,以提供c矩阵角的所有坐标。 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. 对于角落的一些视觉效果,您可以转到https://matplotlib.org/api/_as_gen/matplotlib.pyplot.pcolor.html ,其中第一幅图像向您展示X,Y和C的关系。

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(). 可以通过将a和b“重塑”为(36,3),然后通过ax.pcolormesh()制作那些网格,从而实现该解决方案。 Finally the pcolor can be plotted if c is also reshaped to (36,3) ie converted into a 2D array. 最后,如果c也重塑为(36,3),即转换为2D数组,则可以绘制pcolor。 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. pcolormesh更适合较大的数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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