我正在尝试相对于2个其他维度(海拔高度和时间)创建2D阵列(Ne)的3D表面图。当我运行以下程序时,其目的就是:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import pandas as pd
# read in the data held in the DataFrame using "read_table", a function built into the pandas module
Ne_DataFrame = pd.read_table('ne2011216lf.txt', sep='\s+')
# store each dimension from the DataFrame file into its own array for indexing and plotting use
Altitude = np.array(Ne_DataFrame.index, dtype = float)
Time = np.array(Ne_DataFrame.columns, dtype = float)
Ne = np.array(Ne_DataFrame).reshape(Altitude.size,Time.size)
# Plot Ne as a function of Altitude and Time
fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.plot_surface(Ne, Altitude, Time)
cset = ax.contourf(Time, Altitude, Ne, zdir = 'z', cmap = cm.coolwarm)
cset = ax.contourf(Time , Altitude, Ne, zdir = 'x', cmap = cm.coolwarm)
cset = ax.contourf(Time, Altitude, Ne, zdir = 'y', cmap = cm.coolwarm)
ax.set_xlabel('Altitude')
ax.set_xlim(59.1,687)
ax.set_ylabel('Time')
ax.set_zlabel('Ne')
ax.set_zlim(0,6)
plt.show()
我得到一条附加到第20行的ValueError消息。这是完整的Traceback:
ValueError Traceback (most recent call last)
/home/reu2018/bs2018/Ionosphere/sample_code.py in <module>()
18 fig = plt.figure()
19 ax = fig.gca(projection = '3d')
---> 20 ax.plot_surface(Ne, Altitude, Time)
21 cset = ax.contourf(Time, Altitude, Ne, zdir = 'z', cmap = cm.coolwarm)
22 cset = ax.contourf(Time , Altitude, Ne, zdir = 'x', cmap = cm.coolwarm)
/usr/lib64/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.pyc in plot_surface(self, X, Y, Z, *args, **kwargs)
1354 Z = np.atleast_2d(Z)
1355 # TODO: Support masked arrays
-> 1356 X, Y, Z = np.broadcast_arrays(X, Y, Z)
1357 rows, cols = Z.shape
1358
/usr/lib64/python2.7/site-packages/numpy/lib/stride_tricks.pyc in broadcast_arrays(*args)
92 # There must be at least two non-1 lengths for this axis.
93 raise ValueError("shape mismatch: two or more arrays have "
---> 94 "incompatible dimensions on axis %r." % (axis,))
95 elif len(unique) == 2:
96 # There is exactly one non-1 length. The common shape will take this
ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 1.
我已经确定了&#39; Time&#39;和海拔高度&#39;数组匹配&#39; Ne&#39;的形状尺寸。使用第15行中的reshape命令进行数组,我几乎可以肯定plot_surface中的参数的顺序是正确的。任何人都可以在这里找出错误吗?