结合散点图和表面图 [英] Combining scatter plot with surface plot

查看:115
本文介绍了结合散点图和表面图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

plt.hold(True).

以下代码绘制了3D散点图和3D表面图:

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()

结果:

您可以在此处看到其他一些带有3d绘图的示例:
http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

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...

编辑:要解决该问题,应在表面图的颜色图中使用透明度;在以下位置添加代码: 透明色图 并更改行:

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

ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM);

我们得到:

这篇关于结合散点图和表面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆