Use three toggle buttons to set curve be display/not display. After toggle button is pressed, the curve does not be invisible, is there any missing?
Use Visual Studio code with related extension python packages
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display
# Create x values
x = np.linspace(0, 2*np.pi, 100)
# Create y values for each curve
y1 = x
# Create the initial plot with all curves visible
fig, ax = plt.subplots()
line1, = ax.plot(x, y1, label='y = x')
ax.legend()
# Create toggle buttons control
button1 = widgets.ToggleButton(value=True, description='y = x')
# Define callback function to update line visibility
def update_line_visibility(change):
line1.set_visible(button1.value)
plt.draw()
# Bind toggle buttons control and callback function
button1.observe(update_line_visibility, 'value')
# Display toggle buttons control
display(button1)
# Show the plot
plt.show()
Package Versions
python kernel = 3.11.0,
related package version are
ipykernel 6.20.2
ipympl 0.9.3
ipython 8.8.0
ipython-genutils 0.2.0
ipywidgets 7.6.3
jupyter 1.0.0
jupyter_client 7.4.9
jupyter-console 6.6.3
jupyter_core 5.1.5
jupyter-events 0.6.3
jupyter_packaging 0.12.3
jupyter_server 2.5.0
jupyter_server_fileid 0.9.0
jupyter-server-proxy 3.2.2
jupyter_server_terminals 0.4.4
jupyter_server_ydoc 0.8.0
jupyter-ydoc 0.2.4
jupyterlab 3.6.3
jupyterlab-pygments 0.2.2
jupyterlab_server 2.22.1
jupyterlab-vpython 3.1.4
jupyterlab-widgets 3.0.5
matplotlib 3.6.3
matplotlib-inline 0.1.6
python-dateutil 2.8.2
python-json-logger 2.0.7
QtPy 2.3.1
vpython 7.6.4
websocket-client 1.5.1
wheel 0.40.0
widgetsnbextension 3.5.2