将散点图与曲面图结合起来

use*_*765 12 python scatter matplotlib geometry-surface

如何将3D散点图与3D曲面图结合起来,同时保持曲面图透明,这样我仍然可以看到所有点?

sis*_*aty 17

要在同一图表中组合各种类型的图表,您应该使用该功能

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()
Run Code Online (Sandbox Code Playgroud)

结果:

http://s9.postimage.org/ge0wb8kof/3d_scatter_surface_plt.gif

你可以在这里看到一些带有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
Run Code Online (Sandbox Code Playgroud)

ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM);
Run Code Online (Sandbox Code Playgroud)

我们得到:

http://s16.postimage.org/5qiqn0p5h/3d_scatter_surface_plt_transparent.gif


小智 13

使用siluaty的例子; 而不是通过cmap = theCM命令使用透明度,您可以调整alpha值.这可能会得到你想要的?

ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)
Run Code Online (Sandbox Code Playgroud)