3D graph in Plotly
Here’s an example of how to create a 3D graph using Python’s $Plotly$ library.
In this example, we’ll create a 3D surface plot that visualizes a mathematical function.
Step 1: Install Plotly
If you don’t have $Plotly$ installed, you can install it using pip:
1 | pip install plotly |
Step 2: Create the 3D Graph
1 | import numpy as np |
Explanation of the Code:
Data Generation:
xandyare generated usingnumpy‘slinspacefunction to create a grid of values.zis the function we want to plot, in this case, $z = sin(sqrt(x^2 + y^2))$.
Surface Plot:
- We use
plotly.graph_objs.Surfaceto create the 3D surface plot.
Thecolorscale='Viridis'argument applies a color gradient to the surface.
- We use
Layout:
- The
layoutvariable customizes the appearance of the plot, including axis titles and the camera angle. - The
camerasettings control the perspective of the 3D plot.
- The
Display:
pio.show(fig)is used to render and display the graph in a new browser window or in your notebook interface.
Result:
Running this code will produce a beautiful, interactive 3D surface plot.
You can rotate, zoom, and pan the graph to explore it from different angles.
The Viridis colorscale provides a visually appealing gradient to highlight the surface’s features.
Output:














