Py.Cafe

huong-li-nguyen/

vizro-figure-friday-iteration-ufo

Top UFO Sighting Shapes Over Time (1998-2014)

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
# 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
import pandas as pd
from vizro.models.types import capture

df = pd.read_csv('https://raw.githubusercontent.com/plotly/Figure-Friday/refs/heads/main/2024/week-47/scrubbed.csv',
                 low_memory=False)

df['year'] = pd.to_datetime(df['date posted']).dt.year
df = df.dropna(subset=["shape", "year"])
# Remove unknown category, but mention it in the footer.
df = df[~df['shape'].isin(['unknown'])]
shape_counts = df['shape'].value_counts() 
top_7_shapes = shape_counts.nlargest(7).index
df_top_7 = df[df['shape'].isin(top_7_shapes)] 
# Sort values by counts such that the legend is sorted
df_grouped = df_top_7.groupby(['year', 'shape'])['shape'].count().reset_index(name='sightings').sort_values(["year", "sightings"], ascending=False)

@capture("graph")
def line(data_frame, **kwargs):
    fig = px.line(data_frame, **kwargs) 
    # Declutter chart further
    fig.update_layout(legend_title="", xaxis_showgrid=False, yaxis_rangemode="tozero", xaxis_title="")
    return fig

page = vm.Page(
    title="Figure Friday - Week 47 🛸",
    components=[
        vm.Graph(
            # Add title and footer
            title='Top 7 Most Frequently Mentioned UFO Sighting Shapes (1998-2014)',
            footer='Data: [National UFO Reporting Center (on Kaggle)](https://www.kaggle.com/datasets/NUFORC/ufo-sightings). For this analysis, entries with unknown shapes have been excluded.',
            figure=line(df_grouped,
            x='year',
            y='sightings',
            color='shape',
            labels={'sightings': 'Number of Sightings'})),
    ],
)

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