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

发布于 2024-10-12 20:08:04 字数 797 浏览 3 评论 0原文

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

这是一个例子:

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

我想要一个可以让我在执行期间执行此操作的解决方案,但只要我可以旋转它并且它很短/很快,我就可以接受。

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

Mayavi 可以更轻松地可视化数据,但 MatPlotLib 看起来更专业。 Matplotlib 也更轻。

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.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-10-19 20:08:04

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

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

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

或者,您可以使用 ax.elevax.azimax.dist 属性来获取/设置仰角、方位角和当前视点的距离。

源代码借用sourceforge.net/mpl_toolkits/mplot3d/tutorial.html#scatter-plots" rel="noreferrer">这个例子:

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

我们得到了一个很好的散点图:
alt text

您可以通过编程方式旋转轴,如下所示:

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

希望有所帮助!

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 elev and azim are the elevation and azimuth angles (in degrees) that you want to view your plot from.

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

Borrowing the source from 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!

遗弃M 2024-10-19 20:08:04

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

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

GUI 允许通过单击和拖动进行旋转以及放大/out 通过右键单击并拖动。

替代文本

Using mayavi, you can create such a plot with

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.

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文