有什么简单的方法可以在 Python 中绘制可以旋转的 3d 散点图吗?
目前我正在使用 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
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
2. matplotlib
Mayavi makes it easier to visualize the data, but MatPlotLib looks more professional. Matplotlib is also lighter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先您需要定义“更好地查看我的数据”的含义...
如果您想交互式工作,您可以使用鼠标旋转和放大绘图。
如果您只想以编程方式旋转轴,请使用 ax.view_init(elev, azim) ,其中 elev 和 azim 是海拔以及您想要查看绘图的方位角(以度为单位)。
或者,您可以使用
ax.elev
、ax.azim
和ax.dist
属性来获取/设置仰角、方位角和当前视点的距离。从源代码借用sourceforge.net/mpl_toolkits/mplot3d/tutorial.html#scatter-plots" rel="noreferrer">这个例子:
我们得到了一个很好的散点图:
您可以通过编程方式旋转轴,如下所示:
希望有所帮助!
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)
whereelev
andazim
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
, andax.dist
properties to get/set the elevation, azimuth, and distance of the current view point.Borrowing the source from this example:
We get a nice scatterplot:
You can rotate the axes programatically as shown:
Hope that helps a bit!
使用 mayavi,您可以使用 GUI 创建这样的图
GUI 允许通过单击和拖动进行旋转以及放大/out 通过右键单击并拖动。
Using mayavi, you can create such a plot with
The GUI allows rotation via clicking-and-dragging, and zooming in/out via right-clicking-and-dragging.