Hoe maak je een 3D-spreidingsdiagram in Python?

Ik heb momenteel een nx3-matrixarray. Ik wil de drie kolommen plotten als drie assen.
Hoe kan ik dat doen?

Ik heb gegoogled en mensen stelden voor om Matlabte gebruiken, maar ik heb er echt moeite mee om het te begrijpen. Ik wil ook dat het een spreidingsdiagram is.

Kan iemand het me leren?


Antwoord 1, autoriteit 100%

U kunt hiervoor matplotlibgebruiken. matplotlib heeft een mplot3dmodule die precies doet wat je wilt.

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

De bovenstaande code genereert een figuur als:


Antwoord 2, autoriteit 6%

Gebruik de volgende code die voor mij werkte:

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

Terwijl X_Iso mijn 3D-array is en voor X_VALS, Y_VALS, Z_VALS Ik heb 1 kolom / as van die array gekopieerd / gebruikt en respectievelijk aan die variabelen / arrays toegewezen.


3

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

Scatter Plot

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 Notebook

Other episodes