matplotlib 如何制作3D散点图

3mpgtkmj  于 2023-05-01  发布在  其他
关注(0)|答案(4)|浏览(140)

我目前有一个nx3矩阵数组。我想把这三根柱子画成三个轴。我该怎么做?
我在谷歌上搜索过,人们建议使用Matlab,但我真的很难理解它。我还需要它是一个散点图。

0vvn1miw

0vvn1miw1#

你可以使用matplotlib来实现。matplotlib有一个mplot3d模块,可以完全按照你的要求执行。

import matplotlib.pyplot as plt
import random

fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection='3d')

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)
plt.show()

上面的代码生成了一个类似这样的图:

aelbi1ox

aelbi1ox2#

使用以下代码,它为我工作:

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

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

oaxa6hgo

oaxa6hgo3#

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection='3d')

**散点图 *

zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata);

Colab笔记本

vxf3dgd4

vxf3dgd44#

使用plotly -最简单,最实用,最好的情节

import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width',
              color='species')
fig.show()

https://plotly.com/python/3d-scatter-plots/

相关问题