表面图不是渐变的颜色

时间:2017-10-17 16:44:58

标签: python python-3.x colors 3d mplot3d

我正在使用python 3.4。我正在尝试从2D数组(11x13)绘制一个简单的曲面,但颜色贴图没有正确刻度。只有很小的范围,但颜色条看起来合理,而情节只有条纹。我使用的colourmap似乎并不重要,我得到了相同的结果。

从另一篇文章中,我尝试使用rstride& cstride参数,但只是将整个表面变为粉红色(使用此贴图)。

有什么建议吗?我修改了一个mplot3d教程示例中的代码 - 基本上只是替换了X,Y,Z和轴限制,并尝试了不同的颜色映射。

Step_colours

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(0,550,50)
Y = np.arange(-12,1,1)
X, Y = np.meshgrid(X, Y)
# Array calculated with a process in excel...I'll learn to python it later.
Z = np.array([[0,0,0,0,0,0,0,0,9.318546297,9.32278014,9.324432509],
     [0,0,0,0,0,0,9.270465014,9.281098393,9.287418343,9.291620087,9.293257704],
     [0,0,0,9.183895053,9.20724459,9.226419172,9.239450746,9.25002849,9.257383697,9.261920422,9.263632585],
     [9,9.086332608,9.126397936,9.153091476,9.176369804,9.195477721,9.210108238,9.22128034,9.229169896,9.233975347,9.235758722],
     [9,9.0556775,9.095671635,9.125345711,9.149164982,9.168378335,9.183464661,9.194945002,9.203079816,9.208024884,9.209844315],
     [9,9.040637647,9.075058225,9.10310915,9.126085,9.144796641,9.15970021,9.171125191,9.179264934,9.184223248,9.186039585],
     [9,9.031750626,9.060617885,9.085619791,9.106813693,9.12444679,9.13872781,9.149807723,9.157769849,9.162644172,9.164426795],
     [9,9.025687536,9.049859786,9.071632721,9.09067842,9.106911657,9.120316269,9.130877982,9.138559174,9.14329971,9.145034568],
     [9,9.021085904,9.041333766,9.060092969,9.076965929,9.091711302,9.104158214,9.114155647,9.121547513,9.126169075,9.127867857],
     [9,9.017274923,9.034147314,9.050188629,9.065030255,9.07836292,9.08991453,9.099425441,9.106628029,9.111235402,9.112955947],
     [9,9.01382656,9.027663537,9.041265696,9.054295453,9.066400007,9.077233581,9.086451516,9.09368943,9.098527457,9.100440892],
     [9,9.010336943,9.021310255,9.032733661,9.044224745,9.055367181,9.065750219,9.074968252,9.082599449,9.088144367,9.090799207],
     [9,9.00619218,9.014431776,9.023994936,9.034293996,9.04481216,9.055077078,9.064646767,9.073106766,9.080108346,9.085588416]])

# replacing the zeroes with NaN
for i in range(np.shape(Z)[0]):
    for j in range(np.shape(Z)[1]):
        if Z[i,j] == 0:
            Z[i,j] = float('nan')

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

# Customize the z axis.
ax.set_zlim(9, 9.5)
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()

1 个答案:

答案 0 :(得分:0)

对于发现此主题的其他人也有类似的问题:

在阅读了有关处理NaN细胞的问题后,我尝试使用nanmin和nanmax参数修改ax.plot_surface命令并整理出颜色拉伸:

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False, rstride=1, cstride=1,
                   vmin=np.nanmin(Z), vmax=np.nanmax(Z))