在Matplotlib 3D表面图中只能看到单一颜色

时间:2018-03-31 01:51:43

标签: python matplotlib

我正在尝试使用matplotlib中的3D曲面绘图功能。即使颜色条具有正确的颜色分布,我也总是在主3D图中获得单一颜色。我试过调整vmin和vmax值。它会改变显示的颜色,但它仍然是单一颜色。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import matplotlib.colors as colors


plt.rc('font', size=20)          # controls default text sizes
plt.rc('axes', titlesize=20)     # fontsize of the axes title
plt.rc('axes', labelsize=20)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=20)    # fontsize of the tick labels
plt.rc('ytick', labelsize=20)    # fontsize of the tick labels
plt.rc('legend', fontsize=20)    # legend fontsize
plt.rc('figure', titlesize=20)  # fontsize of the figure title


fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')

X = np.linspace(1, 9, 9)
Y = np.linspace(0.1, 0.9, 9)
X, Y = np.meshgrid(X, Y)


y14=[0.4452739,0.30183285,0.25813195,0.22200696,0.19410882,0.17159065,0.15946234,0.1454833,0.13729666]
y24=[0.36556191,0.26622398,0.20779134,0.17347617,0.15454879,0.13959851,0.12783846,0.1125244,0.0991787]
y34=[0.28298545,0.20158779,0.15011216,0.13871456,0.12187855,0.1108228,0.09260828,0.09191485,0.08405019]
y44=[0.23466043,0.1570963,0.12579934,0.11918668,0.0973392,0.08328725,0.08287371,0.07223769,0.06703042]
y54=[0.20135427,0.14908527,0.11954249,0.09954416,0.08083851,0.07613027,0.06216689,0.06148425,0.05450287]
y64=[0.18790923,0.13138967,0.09762079,0.08155587,0.06951088,0.06225487,0.05563515,0.05375833,0.04966462]
y74=[0.1646632,0.11620533,0.08697924,0.06734929,0.06212874,0.05564317,0.05146634,0.05027161,0.04692116]
y84=[0.15172702,0.09854979,0.07125092,0.06229793,0.05398641,0.05226622,0.05084956,0.0496555,0.05421488]
y94=[0.13190896,0.07993282,0.06037859,0.05867347,0.0576491,0.05695472,0.05695472,0.05876029,0.05545337]
Z=[y14,y24,y34,y44,y54,y64,y74,y84,y94]
Z=np.array(Z)


# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(0, 0.45)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

请查看附件中我得到的结果:output image

1 个答案:

答案 0 :(得分:0)

您似乎正在使用旧版本的matplotlib。在这种情况下,rstridecstride参数设置为10。但是,在绘图中每个维度的点数少于10个点,因此只有一个点用于颜色信息。

使用

ax.plot_surface(..., rstride=1, cstride=1, ...)

让每个点的颜色信息在图中可视化。

通常,请始终参考matplotlib版本的示例。例如。如果您使用的是matplotlib 1.4.1,请查看

https://matplotlib.org/1.4.1/examples/mplot3d/surface3d_demo.html