【发布时间】:2013-02-20 05:07:57
【问题描述】:
如何将 3D 散点图与 3D 曲面图结合起来,同时保持曲面图透明,以便我仍然可以看到所有点?
【问题讨论】:
标签: python matplotlib scatter geometry-surface
如何将 3D 散点图与 3D 曲面图结合起来,同时保持曲面图透明,以便我仍然可以看到所有点?
【问题讨论】:
标签: python matplotlib scatter geometry-surface
要在同一张图中组合各种类型的图,您应该使用该函数
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
我已将曲面图的颜色从默认更改为颜色图“热”,以便区分两个图的颜色 - 现在,可以看到曲面图独立地覆盖了散点图顺序...
编辑:要解决这个问题,应该在曲面图的颜色图中使用透明度;将代码添加到: Transparent colormap 并换行:
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);
我们得到:
【讨论】:
zorder 参数仍然适用于 3d 线。这有点混乱,但我通过迭代点并将它们单独绘制为从点到自身的线,在曲面图的顶部绘制了散点图。
使用siluaty的例子;您可以调整 alpha 值,而不是通过 cmap=theCM 命令使用透明度。这可能会得到你想要的?
ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)
【讨论】: