import plotly.graph_objs as go
import ipywidgets as widgets
import numpy as np
# Create some data for the plots
x = np.random.rand(50)
y = np.random.rand(50)
# Create a function that will be called when the dropdown value changes
def update_plot(plot_type):
fig = go.Figure()
if plot_type == 'Scatter Plot':
fig.add_trace(go.Scatter(x=x, y=y, mode='markers'))
elif plot_type == 'Box Plot':
fig.add_trace(go.Box(y=y))
fig.show()
# Create a dropdown menu
dropdown = widgets.Dropdown(
options=['Scatter Plot', 'Box Plot'],
value='Scatter Plot',
description='Plot Type:',
)
# Display the dropdown menu
display(dropdown)
# Call the function to update the plot whenever the dropdown value changes
widgets.interactive(update_plot, plot_type=dropdown)
This code should generate either a scatter or a box plot with plotly depending on the selection of the dropdown menu.
When the code is run inside a cell of a .ipynb file in VS Code there is a bug: every time I change the status of the dropdown, instead of updating the already existing plot, a new plot is created below the one already existing.
I tried to run the same code in Jupyter Lab, and there, the code works flawlessly. I tried a similar code inside VS Code using matplotlib, and with that I don't have issues in VS Code.
How can I fix the issue with plotly in VS Code?
VS Code version: 1.84.2 plotly: 5.18.0 ipywidgets: 8.1.1