Skip to content
Advertisement

Python Plotly chart update with two dropdowns

I am trying to build a plotly scatterplot in Jupyter Lab to be able to see dependencies between various columns in a DataFrame. I want to have two dropdown menus (corresponding to the X and Y axes), in each of which a full list of the DF columns will be available. When I select a column in any of the menus, the data on the appropriate axis should be replaced by the column I selected (so, if I select the same column for X and Y, I would expect a straight line).

Below is my current implementation with a sample DataFrame:

JavaScript

The figure draws correctly initially: enter image description here

Still, there are a few problems:

  1. When I change values in a dropdown, it only works once. On the next tries nothing happens
  2. If I first change the value on one dropdown and then on the other, all data disappears from the graph (see screenshot below)
  3. The axes labels are not being updated enter image description here

Advertisement

Answer

It’s just about being systematic around the list comprehensions. Below fully works, allows selection of any column and updates appropriate axis title.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement