Skip to content Skip to sidebar Skip to footer

Matplotlib Runtimewarning Displaying A 3d Plot

I have a script which analyses a dataset and then outputs xyz data. In order to understand the distribution of the data, I want to visualize it in a 3d plot. As I have no experienc

Solution 1:

The problem is that griddata cannot produce data for the edges of the grid. This is circumvented internally by masking the output array. However, for a masked array, a comparison xa < 0, which is needed to determine the colors, is not possible.

The solution here would be to exclude the edges from plotting.

ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1])

Complete example:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
import numpy as np

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

data = np.genfromtxt('plot.txt')
x = data[:,0]
y = data[:,1]
z = data[:,2]

xi = np.linspace(-1, 1)
yi = np.linspace(-1, 1)

X, Y = np.meshgrid(xi, yi)
Z = griddata(x, y, z, xi, yi, interp='linear')

surf = ax.plot_surface(X[1:-1,1:-1], Y[1:-1,1:-1], Z[1:-1,1:-1], 
                       rstride=5, cstride=5, cmap=cm.jet,
                       linewidth=1, antialiased=True)

ax.set_zlim3d(np.min(Z), np.max(Z))

fig.colorbar(surf)

plt.show()

enter image description here

Post a Comment for "Matplotlib Runtimewarning Displaying A 3d Plot"