问题
How can I combine a 3D scatter plot with a 3D surface plot while keeping the surface plot transparent so that I can still see all the points?
回答1:
To combine various types of plots in the same graph you should use the function
plt.hold(True).
The following code plots a 3D scatter plot with a 3D surface plot:
from mpl_toolkits.mplot3d import *
import matplotlib.pyplot as plt
import numpy as np
from random import random, seed
from matplotlib import cm
fig = plt.figure()
ax = fig.gca(projection='3d') # to work in 3d
plt.hold(True)
x_surf=np.arange(0, 1, 0.01) # generate a mesh
y_surf=np.arange(0, 1, 0.01)
x_surf, y_surf = np.meshgrid(x_surf, y_surf)
z_surf = np.sqrt(x_surf+y_surf) # ex. function, which depends on x and y
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot); # plot a 3d surface plot
n = 100
seed(0) # seed let us to have a reproducible set of random numbers
x=[random() for i in range(n)] # generate n random points
y=[random() for i in range(n)]
z=[random() for i in range(n)]
ax.scatter(x, y, z); # plot a 3d scatter plot
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_zlabel('z label')
plt.show()
result:
you can see some other examples with 3d plots here:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
I've changed the colours of the surface plot from the default to a colormap "hot" in order to distinguish the colours of the two plots - now, it's seen that the surface plot overrides the scatter plot, independently of the order...
EDIT: To fix that issue, it should be used transparency in the colormap of the surface plot; adding the code in: Transparent colormap and changing the line:
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot); # plot a 3d surface plot
to
ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM);
we get:
回答2:
Using siluaty's example; instead of using transparency through the cmap=theCM command, you can adjust the alpha value. This may get you what you want?
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)
来源:https://stackoverflow.com/questions/15229896/combining-scatter-plot-with-surface-plot