Files
DHGeography/ratios.py

53 lines
1.6 KiB
Python

import pandas as pd
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
# Read the CSV file into a Pandas DataFrame
file_path = "texts_by_period_and_location_saparated_by_periods_in_columns.csv"
df = pd.read_csv(file_path)
# Initialize the Dash app
app = dash.Dash(__name__)
# Define the columns to summarize
columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
# Create the figure for the initial graph (placeholder data)
initial_data = df.groupby("place")[columns_to_summarize].sum().reset_index()
initial_fig = go.Figure(data=[
go.Bar(name=col, x=initial_data["place"], y=initial_data[col]) for col in columns_to_summarize
])
# Define the app layout
app.layout = html.Div([
html.H1("Dynamic Stacked Bar Graph"),
dcc.Dropdown(
id="place-selector",
options=[{"label": place, "value": place} for place in df["place"].unique()],
multi=True,
placeholder="Select Places",
),
dcc.Graph(id="bar-graph", figure=initial_fig),
])
# Define a callback to update the graph based on selected places
@app.callback(
Output("bar-graph", "figure"),
[Input("place-selector", "value")]
)
def update_graph(selected_places):
filtered_df = df[df["place"].isin(selected_places)]
grouped = filtered_df.groupby("place")[columns_to_summarize].sum().reset_index()
fig = go.Figure(data=[
go.Bar(name=col, x=grouped["place"], y=grouped[col]) for col in columns_to_summarize
])
fig.update_layout(barmode="stack") # Set the bars to be stacked
return fig
if __name__ == "__main__":
app.run_server(debug=True)