- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Plotting points on the surface of a sphere in Python's Matplotlib
To plot points on the surface of a sphere in Python, we can use plot_surface() method.
Steps
Create a new figure or activate an existing figure using figure() method.
Add a set of subplots using add_subplot() method with 3d projection.
Initialize a variable, r.
Get the theta value for spherical points and x, y, and z data points using numpy.
Plot the surface using plot_surface() method.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(projection='3d') r = 0.05 u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j] x = np.cos(u) * np.sin(v) y = np.sin(u) * np.sin(v) z = np.cos(v) ax.plot_surface(x, y, z, cmap=plt.cm.YlGnBu_r) plt.show()
Output
Advertisements