import plotly.express as px
import pandas as pd
from dash import Dash, dcc, Input, Output, callback, html
df = pd.read_csv("data/contestants.csv")
app = Dash()
@callback(
Output("strip-graph", "figure"),
Input("country","value")
)
def update_graph(country_slctd):
dff = df.copy()
dff['color'] = dff['to_country'].apply(lambda x: country_slctd if x == country_slctd else 'other')
fig = px.strip(dff, x="points_final", y="year", hover_data="to_country", color="color", height=750,
color_discrete_map={country_slctd: 'red', 'other': 'blue'})
return fig
# Count the number of first placements for each country
first_place_counts = df[df['place_final'] == 1].groupby('to_country').size().reset_index(name='first_place_count')
def create_choropleth():
# Debug: Check the first place counts
print(first_place_counts.head())
# Debug: Check the first place counts
print("First Place Counts:")
print(first_place_counts)
fig = px.choropleth(
data_frame=first_place_counts,
locations='to_country',
locationmode='country names',
color='first_place_count',
hover_name='to_country',
color_continuous_scale=px.colors.sequential.Plasma_r,
range_color=(0, first_place_counts['first_place_count'].max()), # Set range dynamically
title='Number of Times Countries Placed First in Eurovision Throughout the Years'
)
return fig
def create_line_chart():
avg_points_by_year = df.groupby('year')['points_final'].mean().reset_index()
fig = px.line(avg_points_by_year, x='year', y='points_final', title='Average Points Over Time', color_discrete_sequence=px.colors.qualitative.Set1)
return fig
def create_bar_chart():
wins_by_country = df[df['place_final'] == 1].groupby('to_country').size().reset_index(name='wins')
return px.bar(wins_by_country, x='to_country', y='wins', title='Number of Wins by Country', color_discrete_sequence=px.colors.qualitative.Pastel)
def create_scatter_plot():
return px.scatter(df, x='running_final', y='points_final', color='to_country', title='Points vs. Running Order',
color_continuous_scale=px.colors.sequential.Viridis)
app.layout = html.Div([
html.H1("Eurovision Data Visualization"),
dcc.Tabs([
dcc.Tab(label='Countries Placed First Map π', children=[
dcc.Graph(figure=create_choropleth(), id="choropleth-map"),
html.Div(
"Eurovision song contest country placed 1st over the years.",
style={"marginBottom": "20px"}
),
html.Div(
"*Data source:* Eurovision official data.",
style={"fontSize": "12px", "color": "grey"}
)
]),
dcc.Tab(label='Final Points Based on the Year', children=[
dcc.Dropdown(
options=[{'label': country, 'value': country} for country in sorted(df['to_country'].unique())],
value="Netherlands",
clearable=False,
id="country"
),
dcc.Graph(id="strip-graph")
]),
dcc.Tab(label='Avg. Final Points Over Time', children=[
dcc.Graph(figure=create_line_chart(), id="line-chart")
]),
dcc.Tab(label='Number of Wins By Country', children=[
dcc.Graph(figure=create_bar_chart(), id="bar-chart")
]),
dcc.Tab(label='Running Order vs. Points', children=[
dcc.Graph(figure=create_scatter_plot(), id="scatter-plot")
])
])
])
# # Define content for each page
# choropleth_content = html.Div([
# html.H2("Countries Placed First Map π"),
# dcc.Graph(figure=create_choropleth(), id="choropleth-map"),
# html.Div(
# "Eurovision song contest country placed 1st over the years.",
# style={"marginBottom": "20px"}
# ),
# html.Div(
# "*Data source:* Eurovision official data.",
# style={"fontSize": "12px", "color": "grey"}
# )
# ])
# strip_chart_content = html.Div([
# html.H2("Final Points Based on the Year"),
# dcc.Dropdown(
# options=[{'label': country, 'value': country} for country in sorted(df['to_country'].unique())],
# value="Netherlands",
# clearable=False,
# id="country"
# ),
# dcc.Graph(id="strip-graph")
# ])
# line_chart_content = html.Div([
# html.H2("Avg. Final Points Over Time"),
# dcc.Graph(figure=create_line_chart(), id="line-chart")
# ])
# bar_chart_content = html.Div([
# html.H2("Number of Wins By Country"),
# dcc.Graph(figure=create_bar_chart(), id="bar-chart")
# ])
# scatter_plot_content = html.Div([
# html.H2("Running Order vs. Points"),
# dcc.Graph(figure=create_scatter_plot(), id="scatter-plot")
# ])
# app.layout = html.Div([
# html.H1("Eurovision Data Visualization"),
# dcc.Tabs([
# dcc.Tab(label='Countries Placed First Map π', children=[choropleth_content]),
# dcc.Tab(label='Final Points Based on the Year', children=[strip_chart_content]),
# dcc.Tab(label='Avg. Final Points Over Time', children=[line_chart_content]),
# dcc.Tab(label='Number of Wins By Country', children=[bar_chart_content]),
# dcc.Tab(label='Running Order vs. Points', children=[scatter_plot_content])
# ])
# ])
if __name__ == '__main__':
app.run(debug=True)