【发布时间】:2015-12-01 18:14:58
【问题描述】:
我有数据已用 python 读入 numpy 数组,每个数组的长度为 22,500 个数据点。我正在尝试使用下面的代码创建一个颜色图,但它不起作用。如果有人能给我一些关于我做错了什么的建议,我将不胜感激。
# - The code starts out with lines which read data from a text file into three separate
# python lists. However, these lines are not necessary for this example so they are
# being omitted.
# These lines transform the lists into numpy arrays. Each array contains 22,500 data points
XVal = np.array(Array1)
YVal = np.array(Array2)
ZVal = np.array(Array3)
# - These section attempts to create a 2D color map of the data with the XVal and ZVal arrays
# being represented as x and y coordinates on the color map and the ZVal array represented
# as a color on the X and Y coordinates
import pylab
# Create the colormap
pylab.pcolor(XVal,YVal,ZVal)
# Create colorbar
pylab.colorbar()
# Show plot on screen
pylab.show()
当我运行此代码时,我收到以下冗长的错误消息
ValueError Traceback (most recent call last)
C:\Users\jawebb\Desktop\MCNP\Mesh_Tal.py in <module>()
124 # pylab.linspace(YVal.min(),YVal.max(),200))
125
--> 126 pylab.pcolor(XVal,YVal,Tally)
127
128 pylab.colorbar()
C:\Users\jawebb\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.pyc in pcolor(*args, **kwargs)
3016 ax.hold(hold)
3017 try:
-> 3018 ret = ax.pcolor(*args, **kwargs)
3019 draw_if_interactive()
3020 finally:
C:\Users\jawebb\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.pyc in pcolor(self, *args, **kwargs)
4887 shading = kwargs.pop('shading', 'flat')
4888
-> 4889 X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
4890 Ny, Nx = X.shape
4891
C:\Users\jawebb\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\axes\_axes.pyc in _pcolorargs(funcname, *args, **kw)
4690 if len(args) == 3:
4691 X, Y, C = args
-> 4692 numRows, numCols = C.shape
4693 else:
4694 raise TypeError(
ValueError: need more than 1 value to unpac
【问题讨论】:
-
您阅读过
plt.pcolor的文档吗?特别是,阅读有关输入数组形状的部分 -ZVal需要是二维的。 -
另外,
pcolor对于这么多数据点可能会很慢。考虑改用pcolormesh或imshow。 -
你说得对,用imshow写这段代码比用pcolor写的好,谢谢。
-
您应该发布您的新代码作为答案,然后接受它以便可以关闭问题。
标签: python arrays numpy matplotlib