我正在尝试绘制3D表面。我每个1D有3个数组。值是从Excel工作表中提取的。
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib.pyplot as plt
import itertools
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
X = [0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1,1.05,1.1,1.15,1.2]
Y = [10000000000000000000
,20000000000000000000
,30000000000000000000
,40000000000000000000
,50000000000000000000
,60000000000000000000
,70000000000000000000
,80000000000000000000
,90000000000000000000
,100000000000000000000
,110000000000000000000
,120000000000000000000
,130000000000000000000
,140000000000000000000
,150000000000000000000
,160000000000000000000
,170000000000000000000
,180000000000000000000
,190000000000000000000
,200000000000000000000]
Z = [5.90751,6.01571,6.19643,6.26732,6.32022,6.36293,6.42566,6.43954,6.47582,6.50517,6.52934,6.54958,6.56663,6.5811,6.63215,6.62565,6.61779,6.60883,6.59896,6.5883,6.57693,6.56489,6.44006,6.53824,6.68457,6.74257,6.78405,6.816,6.8617,6.84799,6.83235,6.81546,6.81916,6.81924,6.81536,6.80798,6.8242,6.79263,6.75856,6.72239,6.68439,6.64468,6.60344,6.56073,6.7541,6.88597,6.977,6.97738,6.98815,7.02256,7.06207,7.0375,7.00731,6.97282,6.93465,6.89311,6.84843,6.80066,6.75272,6.68075,6.60604,6.52904,6.45007,6.3693,6.28693,6.20308,7.02396,7.08376,7.17063,7.18306,7.18244,7.17303,7.14963,7.08718,7.01978,6.94831,6.873,6.79399,6.71136,6.62525,6.51435,6.40319,6.28903,6.17258,6.05432,5.93462,5.81381,5.69213,7.17041,7.27721,7.32903,7.29928,7.26225,7.21911,7.14226,7.04571,6.94349,6.83643,6.72483,6.60891,6.48896,6.36522,6.19277,6.04472,5.8941,5.74196,5.58903,5.43591,5.28305,5.13086,7.37347,7.42535,7.41202,7.34988,7.28249,7.20921,7.08148,6.95175,6.81514,6.67287,6.52555,6.3737,6.21792,6.05878,5.8292,5.64887,5.46718,5.28562,5.10517,4.92652,4.7502,4.57673,7.50456,7.5156,7.45192,7.36194,7.26661,7.16445,6.9862,6.82358,6.653,6.47636,6.29473,6.10908,5.92039,5.72962,5.45008,5.2436,5.03772,4.83421,4.63421,4.43842,4.24741,4.06158,7.59027,7.57076,7.46464,7.36256,7.24748,7.11851,6.88461,6.68277,6.46961,6.25906,6.04556,5.82934,5.6118,5.39422,5.07334,4.84745,4.62447,4.40641,4.19441,3.98912,3.79098,3.60023,7.64709,7.60481,7.49907,7.36629,7.21803,7.05517,6.76344,6.52663,6.27886,6.02983,5.78768,5.54493,5.30326,5.06415,4.71086,4.47207,4.23853,4.01246,3.79484,3.58623,3.38687,3.19676,7.69465,7.66694,7.50986,7.34632,7.16629,6.97074,6.62319,6.35377,6.07431,5.79536,5.52839,5.26349,5.00257,4.7472,4.3698,4.12397,3.8855,3.65669,3.43849,3.2312,3.03486,2.84921,7.76301,7.70565,7.5019,7.30898,7.09811,6.87086,6.46977,6.17042,5.86259,5.56088,5.27309,4.99049,4.71504,4.44825,4.05424,3.80609,3.56714,3.3397,3.12458,2.92188,2.73135,2.5526,7.81029,7.72748,7.47989,7.25847,7.01752,6.75968,6.30765,5.98133,5.64863,5.33031,5.0257,4.72963,4.44398,4.17004,3.76549,3.51896,3.28293,3.05979,2.85029,2.65428,2.47133,2.30081,7.84228,7.73682,7.4471,7.19778,6.92754,6.64026,6.14031,5.79016,5.43615,5.10655,4.78893,4.48326,4.19115,3.91363,3.5034,3.26149,3.03086,2.8141,2.61181,2.42376,2.24931,2.09417,7.86291,7.73666,7.40582,7.12914,6.83041,6.51509,5.97045,5.59975,5.23184,4.89163,4.56449,4.25261,3.95726,3.67906,3.26687,3.03182,2.8083,2.5992,2.40511,2.22566,2.06564,1.92312,7.87495,7.7291,7.35774,7.05425,6.72799,6.38616,5.80027,5.41226,5.03708,4.68693,4.35335,4.03815,3.74218,3.46562,3.05411,2.82753,2.61229,2.41163,2.2262,2.05918,1.91276,1.77842,7.88034,7.71567,7.30418,6.97446,6.62173,6.25505,5.63148,5.22933,4.85045,4.49324,4.15588,3.8398,3.54531,3.27215,2.8631,2.64618,2.43992,2.24805,2.07221,1.92082,1.78238,1.65584,7.88052,7.69751,7.24614,6.89087,6.51289,6.12313,5.46538,5.05433,4.67267,4.31092,3.97209,3.65707,3.36569,3.09725,2.69174,2.48528,2.28833,2.10534,1.94548,1.8019,1.67103,1.55181,7.87655,7.67546,7.18444,6.80442,6.40247,5.99148,5.30302,4.89137,4.50415,4.14003,3.80161,3.48923,3.20224,2.93941,2.53792,2.34245,2.155,1.98527,1.83567,1.69951,1.57575,1.46331,7.86919,7.65019,7.11977,6.71588,6.29135,5.86103,5.1451,4.73565,4.34505,3.98039,3.64391,3.33541,3.05369,2.79715,2.39965,2.21556,2.03839,1.8826,1.74037,1.61118,1.49403,1.3878,7.85905,7.6222,7.05269,6.62591,6.18029,5.7325,4.99583,4.58742,4.19527,3.83164,3.49835,3.19464,2.91882,2.66895,2.2751,2.10262,1.94104,1.79278,1.65751,1.53481,1.42374,1.3232]
X2,Y2=zip(*list(itertools.product(X,Y)))
x = np.reshape(X2, (20, 22))
y = np.reshape(Y2, (20, 22))
z = np.reshape(Z, (20, 22))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
运行此代码时,出现以下错误
C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\function_base.py in
average(a, axis, weights, returned)
355 if weights is None:
356 avg = a.mean(axis)
--> 357 scl = avg.dtype.type(a.size/avg.size)
358 else:
359 wgt = np.asanyarray(weights)
AttributeError: 'float' object has no attribute 'dtype'
我之前尝试过使用不同的值集来编写此代码,并且效果很好。但由于某种原因,这组代码无法正常工作。
答案 0 :(得分:0)
Y
数组中的数字似乎太大。虽然不确定为什么它与matplotlib 2.2一起使用。
您可以将它们转换为浮点数以使其起作用,
Y = np.array(Y).astype(float)