1

I was trying to plot a 3D diagram with manual input data (x,y,z) using ax.plot_surface. Even though I used a similar code I found online, I still got some errors.

"Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 29
    return umr_minimum(a, axis, None, out, keepdims)
RuntimeWarning: invalid value encountered in reduce

Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 26
    return umr_maximum(a, axis, None, out, keepdims)
RuntimeWarning: invalid value encountered in reduce

Warning (from warnings module):
  File "D:\Program Files (x86)\Python\Python36\lib\site-packages\matplotlib\colors.py", line 489
    np.copyto(xa, -1, where=xa < 0.0)
RuntimeWarning: invalid value encountered in less"

Even with this errors, the diagram could be plotted. But it's all black. And somehow, the colorbar does not match the z values.

Can anyone help me with this problem? I appreciate your help.

This is the code I used (the exact code is shown below):

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np
import scipy.interpolate
from matplotlib.ticker import LinearLocator, FormatStrFormatter

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

x = [1043.797,621.694,203.275,-213.783,-627.143,-1045.474,-1045.474,-628.403,-213.783,0.42,203.278,621.697,1043.801,1042.545,621.701,203.282,0.426,-213.778,-628.397,-1045.467,-0.834,1043.804,621.701,203.292,0.434,-213.77,-628.393,-1045.462,-1045.464,-628.395,-213.772,-0.829,203.29,621.707,1043.812,1043.807,621.706,203.287,-213.775,-628.398,-1045.466]

y = [-1210.936,-1211.146,-1210.931,-1210.819,-1210.916,-1210.916,-727.082,-726.768,-726.776,-726.883,-726.887,-727.101,-726.68,-242.741,-243.059,-242.846,-242.841,-242.732,-242.723,-243.037,19.801,241.133,241.025,241.248,241.148,241.154,241.167,241.07,725.216,725.208,724.565,725.401,724.976,724.97,724.975,1209.226,1209.324,1209.328,1209.338,1209.559,1209.254]

z = [3753.086,4054.802,4101.778,4064.706,3844.414,3614.887,4156.525,4184.521,4284.536,4269.797,4273.816,4298.024,4264.16,4224.935,4188.664,4200.863,4210.243,4164.851,4143.223,4148.073,3980.13,4094.025,4203.862,4260.099,4238.935,4233.248,4186.161,4072.293,4021.05,4311.022,4351.636,4359.61,4385.24,4382.892,4169.055,3927.979,4226.974,4237.096,4180.779,4082.677,3739.785]

x=np.asarray(x)
y=np.asarray(y)
N = 100
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), 
method='cubic')
xi, yi = np.meshgrid(xi,yi)
surf = ax.plot_surface(xi, yi, zi, cmap=plt.cm.hot)
plt.show()
4
  • 1
    Please read How to create a Minimal, Complete, and Verifiable example. How are we supposed find the problem, if we can't reproduce your problem with the information given? And you can format your code with the {} button. Commented Feb 5, 2018 at 16:48
  • 1
    What do we need to place in the code instead of [...] to reproduce this? Commented Feb 5, 2018 at 17:30
  • @Piinthesky Sorry for the incomplete information. I have updated all the information to reproduce the problem there. Could you please take another look? Commented Feb 6, 2018 at 1:01
  • If you remove cmap, it looks ok (if you increase N). But I don't know, why cmap is not applied. Paging Dr @ImportanceOfBeingErnest - he probably knows, how to deal with it. Commented Feb 6, 2018 at 1:41

1 Answer 1

1

Due to the interpolation on the grid, the outmost points of the resulting array are nan (i.e. first and last column & first and last row). While nan values can be ignored for plotting, they are unfortunately not for producing the colorization. In order to be able to use a colormap, an array without nan values should be provided (this is strictly only true for 3D plots).

While there are in general several options like replacing values and masking, here the easiest is to leave out the rows and columns from plotting. I.e. instead of ax.plot_surface(xi, yi, zi, cmap="hot") you can use

ax.plot_surface(xi[1:-1,1:-1], yi[1:-1,1:-1], zi[1:-1,1:-1], cmap="hot")

Complete example:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate


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

x = [1043.797,621.694,203.275,-213.783,-627.143,-1045.474,-1045.474,-628.403,-213.783,0.42,203.278,621.697,1043.801,1042.545,621.701,203.282,0.426,-213.778,-628.397,-1045.467,-0.834,1043.804,621.701,203.292,0.434,-213.77,-628.393,-1045.462,-1045.464,-628.395,-213.772,-0.829,203.29,621.707,1043.812,1043.807,621.706,203.287,-213.775,-628.398,-1045.466]

y = [-1210.936,-1211.146,-1210.931,-1210.819,-1210.916,-1210.916,-727.082,-726.768,-726.776,-726.883,-726.887,-727.101,-726.68,-242.741,-243.059,-242.846,-242.841,-242.732,-242.723,-243.037,19.801,241.133,241.025,241.248,241.148,241.154,241.167,241.07,725.216,725.208,724.565,725.401,724.976,724.97,724.975,1209.226,1209.324,1209.328,1209.338,1209.559,1209.254]

z = [3753.086,4054.802,4101.778,4064.706,3844.414,3614.887,4156.525,4184.521,4284.536,4269.797,4273.816,4298.024,4264.16,4224.935,4188.664,4200.863,4210.243,4164.851,4143.223,4148.073,3980.13,4094.025,4203.862,4260.099,4238.935,4233.248,4186.161,4072.293,4021.05,4311.022,4351.636,4359.61,4385.24,4382.892,4169.055,3927.979,4226.974,4237.096,4180.779,4082.677,3739.785]

x=np.asarray(x)
y=np.asarray(y)
N = 100
xi = np.linspace(x.min(), x.max(), N)
yi = np.linspace(y.min(), y.max(), N)
zi = scipy.interpolate.griddata((x, y), z, (xi[None,:], yi[:,None]), 
                                        method='cubic')

xi, yi = np.meshgrid(xi,yi)

surf = ax.plot_surface(xi[1:-1,1:-1], yi[1:-1,1:-1], zi[1:-1,1:-1], cmap=plt.cm.hot)
plt.show()

enter code here

Sign up to request clarification or add additional context in comments.

3 Comments

Great answer as always. I was surprised that with N=100 the borders looked so serrated. This was also caused by NaN values then?
I guess so. Using nan values would always require to crop something, as e.g. the mean of nan and 3800 is not defined.
Thank you so much. I am kind of new to python. I will look into how those NAN rows and columns are left out in the plotting then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.