[英]Combining scatter plot with surface plot
如何将 3D 散点图与 3D 曲面图结合起来,同时保持曲面图透明,以便我仍然可以看到所有点?
要在同一图表中组合各种类型的图表,您应该使用该功能
plt.hold(真)。
以下代码绘制了具有3D曲面图的3D散点图:
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()
结果:
你可以在这里看到一些带有3d图的其他例子:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html
为了区分两个图的颜色,我已经将曲面图的颜色从默认颜色更改为“热图” - 现在,可以看到曲面图覆盖了散点图,与顺序无关 ...
编辑:要解决这个问题,应该在曲面图的色彩图中使用透明度; 添加代码: 透明色图和更改行:
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot); # plot a 3d surface plot
至
ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM);
我们得到:
使用siluaty的例子; 而不是通过cmap = theCM命令使用透明度,您可以调整alpha值。 这可能会得到你想要的?
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)
正如其他人提到的,如果你想要透明的 3d 图,只需提供 alpha 参数:
# 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')
如果要在 3d 图上绘制点,请使用zorder 。 例如,将生成以下代码:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.