[python] How to make a 3D scatter plot in Python?

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

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.

Can someone teach me?

This question is related to python 3d matplotlib plot scatter-plot

The answer is


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.


Use asymptote instead!

This is what it can look like:

https://asymptote.sourceforge.io/gallery/3Dgraphs/helix.html

This is the code: https://asymptote.sourceforge.io/gallery/3Dgraphs/helix.asy

Asymptote can also read in data files.

And the full gallery: https://asymptote.sourceforge.io/gallery/index.html

To use asymptote from within Python:

https://ctan.org/tex-archive/graphics/asymptote/base/asymptote.py


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to 3d

Disable vertical sync for glxgears Rotating a Vector in 3D Space Displaying a 3D model in JavaScript/HTML5 Plotting a 3d cube, a sphere and a vector in Matplotlib Rotate camera in Three.js with mouse Plot 3D data in R R: Plotting a 3D surface from x, y, z How to make a 3D scatter plot in Python? How to convert a 3D point into 2D perspective projection?

Examples related to matplotlib

"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm How to increase image size of pandas.DataFrame.plot in jupyter notebook? How to create a stacked bar chart for my DataFrame using seaborn? How to display multiple images in one figure correctly? Edit seaborn legend How to hide axes and gridlines in Matplotlib (python) How to set x axis values in matplotlib python? How to specify legend position in matplotlib in graph coordinates Python "TypeError: unhashable type: 'slice'" for encoding categorical data Seaborn Barplot - Displaying Values

Examples related to plot

Fine control over the font size in Seaborn plots for academic papers Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python Modify the legend of pandas bar plot Format y axis as percent Simple line plots using seaborn Plot bar graph from Pandas DataFrame Plotting multiple lines, in different colors, with pandas dataframe Plotting in a non-blocking way with Matplotlib What does the error "arguments imply differing number of rows: x, y" mean? matplotlib get ylim values

Examples related to scatter-plot

Matplotlib scatter plot legend Matplotlib scatter plot with different text at each data point How can I label points in this scatterplot? How to change the font size and color of x-axis and y-axis label in a scatterplot with plot function in R? Setting different color for each series in scatter plot on matplotlib scatter plot in matplotlib How to do a scatter plot with empty circles in Python? Control the size of points in an R scatterplot? How to make a 3D scatter plot in Python?