Py.Cafe

huong-li-nguyen/

vizro-data-visualization-0

Data Visualization with Gapminder and Iris Datasets

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.

import vizro.plotly.express as px
from vizro import Vizro
import vizro.models as vm
from vizro._themes._colors import get_colors

COLORS = get_colors()
iris = px.data.iris()
gapminder = px.data.gapminder()
tips = px.data.tips()
tips["size"] = tips["size"].astype(str)
gapminder_positive = gapminder.copy()
gapminder_negative = gapminder.copy()
gapminder_negative["lifeExp"] = -gapminder_negative["lifeExp"]
gapminder_mixed = gapminder.copy()
gapminder_mixed["lifeExp"] = gapminder_mixed["lifeExp"] - gapminder_mixed["lifeExp"].mean()

# FOR STEPH TO CUSTOMIZE
# EXAMPLE:
# COLORS_DIVERGING = ['#d73027', '#f7f7f7', '#003096']
COLORS_DIVERGING = COLORS["DIVERGING_RED_CYAN"]
COLORS_DISCRETE = COLORS["DISCRETE_10"]
COLORS_SEQUENTIAL = COLORS["SEQUENTIAL_CYAN"]
COLORS_SEQUENTIALMINUS = COLORS["SEQUENTIAL_RED"]

page = vm.Page(
    title="Default discrete and continuous",
    components=[
        vm.Graph(
            figure=px.choropleth(
                gapminder, locations="iso_alpha", color="lifeExp", color_continuous_scale=COLORS_SEQUENTIAL
            )
        ),
        vm.Graph(
            figure=px.box(
                gapminder, x="continent", y="lifeExp", color="continent", color_discrete_sequence=COLORS_DISCRETE
            )
        ),
    ],
)

page_two = vm.Page(
    title="Continuous scales",
    layout=vm.Grid(grid=[[0, 1, 2]]),
    components=[
        vm.Graph(
            title="Positive sequential",
            figure=px.choropleth(
                gapminder_positive, locations="iso_alpha", color="lifeExp", color_continuous_scale=COLORS_SEQUENTIAL
            ),
        ),
        vm.Graph(
            title="Negative sequential",
            figure=px.choropleth(
                gapminder_negative,
                locations="iso_alpha",
                color="lifeExp",
                color_continuous_scale=COLORS_SEQUENTIALMINUS,
            ),
        ),
        vm.Graph(
            title="Diverging",
            figure=px.choropleth(
                gapminder_mixed, locations="iso_alpha", color="lifeExp", color_continuous_scale=COLORS_DIVERGING
            ),
        ),
    ],
)

page_three = vm.Page(
    title="Discrete with diverging",
    components=[
        vm.Graph(
            title="Diverging",
            figure=px.choropleth(
                gapminder_mixed, locations="iso_alpha", color="lifeExp", color_continuous_scale=COLORS_DIVERGING
            ),
        ),
        vm.Graph(
            figure=px.box(
                gapminder, x="continent", y="lifeExp", color="continent", color_discrete_sequence=COLORS_DISCRETE
            )
        ),
    ],
)


dashboard = vm.Dashboard(pages=[page, page_two, page_three])
Vizro().build(dashboard).run()