【问题标题】:How to create a colorplot in python with matplotlib from existing data arrays如何在 python 中使用 matplotlib 从现有数据数组中创建颜色图
【发布时间】: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 对于这么多数据点可能会很慢。考虑改用pcolormeshimshow
  • 你说得对,用imshow写这段代码比用pcolor写的好,谢谢。
  • 您应该发布您的新代码作为答案,然后接受它以便可以关闭问题。

标签: python arrays numpy matplotlib


【解决方案1】:

为了完成,我添加了正确的代码行来解决我的问题。这么大的数据集需要 imshow 和一个网格来覆盖数据,这需要在原始 X、Y、Z 坐标和网格网格 X、Y、Z 坐标中的值之间进行插值。

XVal = np.array(XVal); YVal = np.array(YVal); ZVal = np.array(ZVal)
Tally = np.array(Tally); Error = np.array(Error)

import pylab as plt
from matplotlib.colors import LogNorm
import matplotlib.cm as cm
import scipy.interpolate

# Sets up a mesh grid over which data points will be plotted
xi, yi = np.linspace(XVal.min(),XVal.max(),100), np.linspace(YVal.min(),YVal.max(),100)
xi, yi = np.meshgrid(xi,yi)

# Function to interpolate data points on mesh grid
doseinterp = scipy.interpolate.Rbf(XVal, YVal, Tally, function='linear')

# - Uses the above function to interpolate data points at mesh grid locations
#   from MCNP Mesh Tally data
zi  = doseinterp(xi,yi)

# Plots a heat map of Dose Rate over the mesh grid
fig,plt = plt.subplots()
plt.set_title('Z = 0 cm',fontsize=20)
plt.set_xlabel('X (cm)',fontsize=18)
plt.set_ylabel('Y (cm)',fontsize=18)
img1 = plt.imshow(zi,norm=LogNorm(vmin=Tally.min(),vmax=Tally.max()),extent=[XVal.min(),XVal.max(), \
                YVal.min(),YVal.max()])
cb = fig.colorbar(img1)
cb.set_label('Magnitude')
fig.savefig('C:\Users\jawebb\Desktop\Dose.png')

plt.clear()

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 2017-04-04
    • 2019-09-23
    • 2016-04-06
    • 1970-01-01
    • 2015-03-24
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多