forked from saret/DHGeography
with selection of the places
This commit is contained in:
92
ratios.py
92
ratios.py
@@ -1,58 +1,64 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import dash
|
||||||
|
import dash_core_components as dcc
|
||||||
|
import dash_html_components as html
|
||||||
|
from dash.dependencies import Input, Output
|
||||||
import plotly.graph_objects as go
|
import plotly.graph_objects as go
|
||||||
|
|
||||||
# Read the CSV file into a Pandas DataFrame
|
# Read the CSV file into a Pandas DataFrame
|
||||||
file_path = "texts_by_period_and_location_saparated_by_periods_in_columns.csv"
|
file_path = "texts_by_period_and_location_saparated_by_periods_in_columns.csv"
|
||||||
df = pd.read_csv(file_path)
|
df = pd.read_csv(file_path)
|
||||||
|
|
||||||
|
# Initialize the Dash app
|
||||||
|
app = dash.Dash(__name__)
|
||||||
|
|
||||||
# Define the columns to summarize
|
# Define the columns to summarize
|
||||||
columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
|
columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
|
||||||
|
|
||||||
# Group the data by the "place" column and calculate sums within each group
|
# Create the figure
|
||||||
grouped = df.groupby("place")[columns_to_summarize].sum().reset_index()
|
|
||||||
|
|
||||||
# Create a stacked bar chart using Plotly
|
|
||||||
fig = go.Figure()
|
fig = go.Figure()
|
||||||
|
|
||||||
# Add a bar trace for each category
|
# Add an initial trace for demonstration
|
||||||
for col in columns_to_summarize:
|
fig.add_trace(go.Bar(x=["Place 1", "Place 2", "Place 3"],
|
||||||
fig.add_trace(go.Bar(x=grouped["place"], y=grouped[col], name=col, text=grouped[col], textposition='auto'))
|
y=[5, 7, 4], name="ancient"))
|
||||||
|
|
||||||
# Customize the layout
|
# Define the app layout
|
||||||
fig.update_layout(
|
app.layout = html.Div([
|
||||||
title="Summary of Categories by Place with Selection",
|
html.H1("Interactive Bar Graph with Filtering"),
|
||||||
xaxis_title="Place",
|
dcc.Checklist(
|
||||||
yaxis_title="Total Count",
|
id="place-selector",
|
||||||
xaxis=dict(categoryorder='total descending'),
|
options=[{"label": place, "value": place} for place in df["place"].unique()],
|
||||||
barmode='stack'
|
value=df["place"].unique().tolist(),
|
||||||
)
|
labelStyle={'display': 'block'}
|
||||||
|
),
|
||||||
# Enable selection by clicking
|
dcc.Checklist(
|
||||||
fig.update_traces(selector=dict(mode='lasso'))
|
id="column-selector",
|
||||||
|
options=[{"label": col, "value": col} for col in columns_to_summarize],
|
||||||
# Enable searching for place names
|
value=columns_to_summarize,
|
||||||
fig.update_layout(updatemenus=[
|
labelStyle={'display': 'block'}
|
||||||
{
|
),
|
||||||
'buttons': [
|
dcc.Graph(id="bar-graph", figure=fig)
|
||||||
{
|
|
||||||
'args': [{'visible': [True] * len(grouped)}],
|
|
||||||
'label': 'Show All',
|
|
||||||
'method': 'restyle'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'args': [{'visible': [grouped["place"] == place for place in grouped["place"]]}],
|
|
||||||
'label': 'Select Place',
|
|
||||||
'method': 'restyle'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
'direction': 'down',
|
|
||||||
'showactive': False,
|
|
||||||
'x': 0.1,
|
|
||||||
'xanchor': 'left',
|
|
||||||
'y': 1.1,
|
|
||||||
'yanchor': 'top'
|
|
||||||
}
|
|
||||||
])
|
])
|
||||||
|
|
||||||
# Show the interactive graph
|
# Define callback functions to update the graph
|
||||||
fig.show()
|
@app.callback(
|
||||||
|
Output("bar-graph", "figure"),
|
||||||
|
[Input("place-selector", "value"), Input("column-selector", "value")]
|
||||||
|
)
|
||||||
|
def update_graph(selected_places, selected_columns):
|
||||||
|
filtered_df = df[(df["place"].isin(selected_places)) & (df[selected_columns].sum(axis=1) > 0)]
|
||||||
|
grouped = filtered_df.groupby("place")[selected_columns].sum().reset_index()
|
||||||
|
fig = go.Figure()
|
||||||
|
for col in selected_columns:
|
||||||
|
fig.add_trace(go.Bar(x=grouped["place"], y=grouped[col], name=col, text=grouped[col], textposition='auto'))
|
||||||
|
fig.update_layout(
|
||||||
|
title="Summary of Categories by Place with Selection",
|
||||||
|
xaxis_title="Place",
|
||||||
|
yaxis_title="Total Count",
|
||||||
|
xaxis=dict(categoryorder='total descending'),
|
||||||
|
barmode='stack'
|
||||||
|
)
|
||||||
|
return fig
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run_server(debug=True)
|
||||||
|
Reference in New Issue
Block a user