Python 结合散点图和曲面图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15229896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:34:21  来源:igfitidea点击:

Combining scatter plot with surface plot

pythonmatplotlibscattergeometry-surface

提问by user2136765

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?

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

采纳答案by sissi_luaty

To combine various types of plots in the same graph you should use the function

要在同一图形中组合各种类型的图,您应该使用该函数

plt.hold(True).

plt.hold(真)。

The following code plots a 3D scatter plot with a 3D surface plot:

以下代码使用 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()

result:

结果:

you can see some other examples with 3d plots here:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

您可以在此处查看其他一些带有 3d 绘图的示例: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 colormapand 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:

我们得到:

回答by StevilP

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?

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

ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)