-1

I'm working with python 3.4. I'm trying to plot a simple surface from a 2D array (11x13), but the colour map is not graduating properly. There's only a small range, but the colour bar looks reasonable while the plot just has stripes. Doesn't seem to matter what colourmap I use I get an equivalent result.

From another post, I tried using the rstride & cstride parameters, but that just turned the entire surface pink (with this map).

Any suggestions? I adapted the code from one of the mplot3d tutorial examples - basically just replaced X, Y, Z and the axis limits and tried different colour maps.

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()
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
MNoteboom
  • 1
  • 1

1 Answers1

0

For anyone else that finds this thread with a similar question:

After reading a question about handling of NaN cells, I tried modifying the ax.plot_surface command with nanmin and nanmax arguments and that sorted out the colour stretching:

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))
MNoteboom
  • 1
  • 1