有什么简单的方法可以在 Python 中绘制我可以旋转的 3d 散点图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4739360/
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 17:16:38  来源:igfitidea点击:

Any easy way to plot a 3d scatter in Python that I can rotate around?

pythonchartsmatplotlibscatter-plot

提问by pnodbnda

Currently I'm using matplotlib to plot a 3d scatter and while it gets the job done, I can't seem to find a way to rotate it to see my data better.

目前我正在使用 matplotlib 绘制 3d 散点图,虽然它完成了工作,但我似乎无法找到一种方法来旋转它以更好地查看我的数据。

Here's an example:

下面是一个例子:

import pylab as p
import mpl_toolkits.mplot3d.axes3d as p3

#data is an ndarray with the necessary data and colors is an ndarray with
#'b', 'g' and 'r' to paint each point according to its class

...

fig=p.figure()
ax = p3.Axes3D(fig)
ax.scatter(data[:,0], data[:,2], data[:,3], c=colors)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.add_axes(ax)
p.show()

I'd like a solution that lets me do it during execution time but as long as I can rotate it and it's short/quick I'm fine with it.

我想要一个解决方案,让我在执行期间完成它,但只要我可以旋转它并且它很短/很快我就可以了。

Here's a comparison of the plots produced after applying a PCA to the iris dataset:
1. mayavi
alt text
2. matplotlib
alt text

以下是对 iris 数据集应用 PCA 后生成的图的比较:
1. mayavi
替代文字
2. matplotlib
替代文字

Mayavi makes it easier to visualize the data, but MatPlotLib looks more professional. Matplotlib is also lighter.

Mayavi 使数据可视化更容易,但 MatPlotLib 看起来更专业。Matplotlib 也更轻。

采纳答案by unutbu

Using mayavi, you can create such a plot with

使用mayavi,您可以创建这样的图

import enthought.mayavi.mlab as mylab
import numpy as np
x, y, z, value = np.random.random((4, 40))
mylab.points3d(x, y, z, value)
mylab.show()

The GUI allows rotation via clicking-and-dragging, and zooming in/out via right-clicking-and-dragging.

GUI 允许通过单击和拖动进行旋转,并通过右键单击和拖动进行放大/缩小。

alt text

替代文字

回答by Joe Kington

Well, first you need to define what you mean by "see my data better"...

好吧,首先您需要定义“更好地查看我的数据”的含义......

You can rotate and zoom in on the plot using the mouse, if you're wanting to work interactively.

如果您想以交互方式工作,您可以使用鼠标旋转和放大绘图。

If you're just wanting to rotate the axes programatically, then use ax.view_init(elev, azim)where elevand azimare the elevation and azimuth angles (in degrees) that you want to view your plot from.

如果您只想以编程方式旋转轴,请使用ax.view_init(elev, azim)whereelevazim是您想要查看绘图的仰角和方位角(以度为单位)。

Alternatively, you can use the ax.elev, ax.azim, and ax.distproperties to get/set the elevation, azimuth, and distance of the current view point.

另外,您也可以使用ax.elevax.azimax.dist属性来获取/设置仰角,方位角,以及当前视图点的距离。

Borrowing the sourcefrom this example:

借用该示例中

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

We get a nice scatterplot: alt text

我们得到一个很好的散点图: 替代文字

You can rotate the axes programatically as shown:

您可以以编程方式旋转轴,如图所示:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def randrange(n, vmin, vmax):
    return (vmax-vmin)*np.random.rand(n) + vmin

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

ax.azim = 200
ax.elev = -45

plt.show()

alt text

替代文字

Hope that helps a bit!

希望那有所帮助!