Combining scatter plot with surface plot
Question:
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?
Answers:
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:
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)
As others mentioned, if you want transparent 3d plot, just provide alpha argument:
# elve, azim are custom viewpoint/angle
ax = plt.axes(projection='3d', elev=35, azim=-125)
ax.plot_surface(x, y, z, alpha=0.7)
ax.plot(scatter_x, scatter_y, scatter_z, 'b.', markersize=10, label='top')
If you want to plot points on top of the 3d plot, use zorder. For example, the following code will generate:
ax = plt.axes(projection='3d', elev=35, azim=-125)
ax.plot_surface(x, y, z, cmap=plt.cm.coolwarm, linewidth=0.1, zorder=1)
ax.plot(scatter_x, scatter_y, scatter_z, 'b.', markersize=10, label='top', zorder=4)
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?
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:
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)
As others mentioned, if you want transparent 3d plot, just provide alpha argument:
# elve, azim are custom viewpoint/angle
ax = plt.axes(projection='3d', elev=35, azim=-125)
ax.plot_surface(x, y, z, alpha=0.7)
ax.plot(scatter_x, scatter_y, scatter_z, 'b.', markersize=10, label='top')
If you want to plot points on top of the 3d plot, use zorder. For example, the following code will generate:
ax = plt.axes(projection='3d', elev=35, azim=-125)
ax.plot_surface(x, y, z, cmap=plt.cm.coolwarm, linewidth=0.1, zorder=1)
ax.plot(scatter_x, scatter_y, scatter_z, 'b.', markersize=10, label='top', zorder=4)