- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make a 3D scatter plot in Python?
To get a 3D plot, we can use fig.add_subplot(111, projection='3d') method to instantiate the axis. After that, we can use the scatter method to draw different data points on the x, y, and z axes.
Steps
Create a new figure, or activate an existing figure.
Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrows = 1, ncols = 1, index = 1 and projection is ‘3d’.
Iterate a list of marks, xs, ys and zs, to make scatter points.
Set x, y, and z labels using set_xlabel, y_label, and z_label methods.
Use plt.show() method to plot the figure.
Example
import matplotlib.pyplot as plt import numpy as np np.random.seed(1000) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') n = 100 for m, zl, zh in [('o', -50, -25), ('^', -30, -5)]: xs = (32 - 23) * np.random.rand(n) + 23 ys = (100 - 0) * np.random.rand(n) zs = (zh - zl) * np.random.rand(n) + zl ax.scatter(xs, ys, zs, marker=m) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
Output
- Related Articles
- How to make a scatter plot for clustering in Python?
- Connecting two points on a 3D scatter plot in Python and Matplotlib
- How to plot scatter points in a 3D figure with a colorbar in Matplotlib?
- Plot a 3D surface from {x,y,z}-scatter data in Python Matplotlib
- How to turn off transparency in Matplotlib's 3D Scatter plot?
- Controlling the alpha value on a 3D scatter plot using Python and Matplotlib
- How to make a discrete colorbar for a scatter plot in matplotlib?
- Plot scatter points on 3d plot without axes and grids in Matplotlib
- Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)
- How to overplot a line on a scatter plot in Python?
- How can I make a scatter plot colored by density in Matplotlib?
- Plot scatter points on a 3D projection with varying marker size in Matplotlib
- Saving a 3D-plot in a PDF 3D with Python
- How to animate a scatter plot in Matplotlib?
- How to plot a 3D density map in Python with Matplotlib?

Advertisements