Py.Cafe

maartenbreddels/

solara-scatter-plot-plotly

Scatter Plot with Plotly

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""# Scatter plot using Plotly

This example shows how to use Plotly to create a scatter plot and a slider to do some filtering.

Inspired by the dash documentation.


## Note

Solara supports plotly and plotly express. Create your figure (not a figure widget)
and pass it to the [FigurePlotly](/documentation/components/viz/plotly) component.

"""

import pandas as pd
import plotly.express as px
import solara

title = "Scatter plot using Plotly"

try:
    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv")
except Exception:
    df = None

if df is not None:
    year_min = df["year"].min()
    year_max = df["year"].max()
    years = df["year"].unique().tolist()


@solara.component
def Page():
    with solara.Div() as main:
        index = solara.ui_slider(value=0, min=0, max=len(years) - 1, tick_labels=years, key="year slider index")
        selected_year = years[index]

        filtered_df = df[df.year == selected_year].copy()

        fig = px.scatter(filtered_df, x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True, size_max=55)
        fig.update_layout(transition_duration=1500)

        solara.FigurePlotly(fig, dependencies=[selected_year])
    return main