[PYTHON] Interactive plot of 3D graph

Interactive plots in Jupyter Notebook

I couldn't find a simple example, so I made it as a memo.

See below for detailed instructions and setup.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact, FloatSlider, IntSlider
import numpy as np

x = y = np.arange(-20, 20, 0.5)
X, Y = np.meshgrid(x, y)
Z = X*X + 2 * Y*Y


@interact(elev=IntSlider(min=-180, max=180, step=10, value=30, continuous_update=False), 
          azim=IntSlider(min=-180, max=180, step=10, value=30, continuous_update=False))
def plot_3d(elev, azim):
    #Figure settings
    fig = plt.figure(figsize=(10, 10))
    
    ax = fig.add_subplot(111, projection='3d')
    #Display 3D graph
    ax.plot_surface(X, Y, Z)
    
    #Set the initial value of the viewing direction of the 3D graph
    ax.view_init(elev=elev, azim=azim)
    plt.show()

image.png

Recommended Posts

Interactive plot of 3D graph
[Python] limit axis of 3D graph with Matplotlib
3D plot Pandas DataFrame
2D plot in matplotlib
3D plot with matplotlib
python> Handling of 2D arrays
3D scatter plot with PyQtGraph
One-liner basic graph of HoloViews
Plot of regression line by residual plot