Use toggle button to set the visibility of a curve

E-John 0 Reputation points
2023-06-17T00:04:01.45+00:00

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
Developer technologies Visual Studio Extensions
{count} votes

1 answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 31,056 Reputation points Microsoft External Staff
    2023-06-22T06:36:30.1666667+00:00

    Hi @E-John, 

    Welcome to Microsoft Q&A! 

    The tag “Visual Studio Extensions” is related to Visual Studio, if your question is related to Visual Studio Code, please post it to the Stack Overview tagged visual-studio-code forum or GitHub repository where you would get professional support for this product.

    Thanks for your understanding. 

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

     

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.