# 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()