Python matplotlib 3d surface plot

2020-08-02 11:20发布

问题:

I am trying to convert an equation for a surface plot written for mathematica (image and script below) to a python script using matplotlib. There are only a handful of examples of surface plots on the web.

Help would be appreciated for my non functioning one of many attempts

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

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

x = np.linspace(2,-2)
y = np.linspace(2,-2)
z = np.linspace(2,-2)


surfx = -1 * (pow(y, 10) + pow(z, 10) - 100)
surfy = -1 * (pow(x, 10) + pow(z, 10) - 100)
surfz = -1 * (pow(x, 10) + pow(y, 10) - 100)


ax.plot_surface(surfx,surfy,surfz,  rstride=4, cstride=4, color='b')

plt.show()

回答1:

I don't think that matplotlib has an equivalent function at this point. If you are not restricted to using matplotlib, you might want to take a look at mayavi and its contour3d()function.

The following code produces a similar plot to your example using mayavi. I am not sure if it's possible to add the wireframe outline however.

import numpy as np
from mayavi import mlab

x, y, z = np.ogrid[-2:2:25j, -2:2:25j, -2:2:25j]
s = np.power(x, 10) + np.power(y, 10) + np.power(z, 10) - 100

mlab.figure(bgcolor=(1,1,1))
mlab.contour3d(s, contours=[2], color=(.5,.5,.5), transparent=True, opacity=.5)

ax = mlab.axes(nb_labels=5, ranges=(-2,2,-2,2,-2,2))
ax.axes.property.color = (0,0,0)
ax.axes.axis_title_text_property.color = (0,0,0)
ax.axes.axis_label_text_property.color = (0,0,0)
ax.axes.label_format='%.0f'

mlab.show()