如何在Python中创建一个3D散点图?

时间:2022-09-10 22:22:20

I am currently have a nx3 matrix array. I want plot the three columns as three axis's. How can I do that?

我现在有一个nx3矩阵数组。我想把这三列画成三轴。我怎么做呢?

I have googled and people suggested using Matlab, but I am really having a hard time with understanding it. I also need it be a scatter plot.

我在谷歌上搜索过,有人建议用Matlab,但我真的很难理解它。我还需要一个散点图。

Can someone teach me?

有人可以教我吗?

4 个解决方案

#1


73  

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want.

你可以用matplotlib来做这个。matplotlib有一个mplot3d模块,它将执行您想要的操作。

from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
import random


fig = pyplot.figure()
ax = Axes3D(fig)

sequence_containing_x_vals = list(range(0, 100))
sequence_containing_y_vals = list(range(0, 100))
sequence_containing_z_vals = list(range(0, 100))

random.shuffle(sequence_containing_x_vals)
random.shuffle(sequence_containing_y_vals)
random.shuffle(sequence_containing_z_vals)

ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)
pyplot.show()

The code above generates a figure like:

上面的代码生成如下图:

如何在Python中创建一个3D散点图?

#2


2  

Use asymptote instead!

使用渐近线相反!

This is what it can look like:

这就是它的样子:

http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.pdf

http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.pdf

This is the code: http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.asy

这是代码:http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.asy

Asymptote can also read in data files.

渐近线也可以在数据文件中读取。

And the full gallery: http://asymptote.sourceforge.net/gallery/

以及完整的图库:http://asymptote.sourceforge.net/gallery/。

To use asymptote from within Python:

在Python中使用渐近线:

http://www.tex.ac.uk/tex-archive/graphics/asymptote/base/asymptote.py

http://www.tex.ac.uk/tex-archive/graphics/asymptote/base/asymptote.py

#3


2  

Use the following code it worked for me:

使用以下对我有用的代码:

# Create the figure
   fig = plt.figure()
   ax = fig.add_subplot(111, projection='3d')

   # Generate the values
   x_vals = X_iso[:, 0:1]
   y_vals = X_iso[:, 1:2]
   z_vals = X_iso[:, 2:3]

   # Plot the values
   ax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')
   ax.set_xlabel('X-axis')
   ax.set_ylabel('Y-axis')
   ax.set_zlabel('Z-axis')

   plt.show()

while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.

而X_iso是我的3-D数组,对于X_vals, Y_vals, Z_vals,我从该数组中复制/使用1列/轴,并分别分配给这些变量/数组。

#4


-3  

from enthought.mayavi import mlab

从enthought。mayavi进口mlab

#1


73  

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want.

你可以用matplotlib来做这个。matplotlib有一个mplot3d模块,它将执行您想要的操作。

from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
import random


fig = pyplot.figure()
ax = Axes3D(fig)

sequence_containing_x_vals = list(range(0, 100))
sequence_containing_y_vals = list(range(0, 100))
sequence_containing_z_vals = list(range(0, 100))

random.shuffle(sequence_containing_x_vals)
random.shuffle(sequence_containing_y_vals)
random.shuffle(sequence_containing_z_vals)

ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)
pyplot.show()

The code above generates a figure like:

上面的代码生成如下图:

如何在Python中创建一个3D散点图?

#2


2  

Use asymptote instead!

使用渐近线相反!

This is what it can look like:

这就是它的样子:

http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.pdf

http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.pdf

This is the code: http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.asy

这是代码:http://asymptote.sourceforge.net/gallery/3D%20graphs/helix.asy

Asymptote can also read in data files.

渐近线也可以在数据文件中读取。

And the full gallery: http://asymptote.sourceforge.net/gallery/

以及完整的图库:http://asymptote.sourceforge.net/gallery/。

To use asymptote from within Python:

在Python中使用渐近线:

http://www.tex.ac.uk/tex-archive/graphics/asymptote/base/asymptote.py

http://www.tex.ac.uk/tex-archive/graphics/asymptote/base/asymptote.py

#3


2  

Use the following code it worked for me:

使用以下对我有用的代码:

# Create the figure
   fig = plt.figure()
   ax = fig.add_subplot(111, projection='3d')

   # Generate the values
   x_vals = X_iso[:, 0:1]
   y_vals = X_iso[:, 1:2]
   z_vals = X_iso[:, 2:3]

   # Plot the values
   ax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')
   ax.set_xlabel('X-axis')
   ax.set_ylabel('Y-axis')
   ax.set_zlabel('Z-axis')

   plt.show()

while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.

而X_iso是我的3-D数组,对于X_vals, Y_vals, Z_vals,我从该数组中复制/使用1列/轴,并分别分配给这些变量/数组。

#4


-3  

from enthought.mayavi import mlab

从enthought。mayavi进口mlab