Issue
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?
Solution
The following code plots a 3D scatter plot with a 3D surface plot:
import matplotlib.pyplot as plt
import numpy as np
from random import random, seed
from matplotlib import cm
import matplotlib as mpl
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(projection='3d') # to work in 3d
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, ec='k') # 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()
You can see some other examples with 3d plots here.
I've changed the colors of the surface plot from the default to a colormap hot
, in order to distinguish the colors of the two plots - now, it's seen that the surface plot overrides the scatter plot, independently of the order.
To fix that issue, it should be used transparency in the colormap of the surface plot; adding the code from Transparent colormap
and changing the line:
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot) # plot a 3d surface plot
to
theCM = mpl.colormaps.get_cmap('bwr')
theCM._init()
alphas = np.abs(np.linspace(-1.0, 1.0, theCM.N))
theCM._lut[:-3,-1] = alphas
# other code
ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM)
Answered By - sissi_luaty
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.