forked from saret/DHGeography
keep dynamic graph
This commit is contained in:
37
ratios.py
37
ratios.py
@@ -1,6 +1,6 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import matplotlib.pyplot as plt
|
import plotly.express as px
|
||||||
import numpy as np
|
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"
|
||||||
@@ -12,24 +12,21 @@ columns_to_summarize = ["ancient", "old", "middle", "new", "late"]
|
|||||||
# Group the data by the "place" column and calculate sums within each group
|
# Group the data by the "place" column and calculate sums within each group
|
||||||
grouped = df.groupby("place")[columns_to_summarize].sum().reset_index()
|
grouped = df.groupby("place")[columns_to_summarize].sum().reset_index()
|
||||||
|
|
||||||
# Create a list of colors for the inner divisions
|
# Create a bar chart using Plotly
|
||||||
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"]
|
fig = go.Figure()
|
||||||
|
|
||||||
# Create the figure and axis for the bar chart
|
# Add a bar trace for each category
|
||||||
fig, ax = plt.subplots()
|
for col in columns_to_summarize:
|
||||||
|
fig.add_trace(go.Bar(x=grouped["place"], y=grouped[col], name=col))
|
||||||
|
|
||||||
# Create bars for each place with inner divisions
|
# Customize the layout
|
||||||
bottom = np.zeros(len(grouped))
|
fig.update_layout(
|
||||||
for i, col in enumerate(columns_to_summarize):
|
title="Summary of Categories by Place with Filtering",
|
||||||
ax.bar(grouped["place"], grouped[col], 0.6, bottom=bottom, color=colors[i], label=col)
|
xaxis_title="Place",
|
||||||
bottom += grouped[col]
|
yaxis_title="Total Count",
|
||||||
|
xaxis=dict(categoryorder='total descending'),
|
||||||
|
barmode='group'
|
||||||
|
)
|
||||||
|
|
||||||
# Customize the plot
|
# Show the interactive graph
|
||||||
ax.set_ylabel("Total Count")
|
fig.show()
|
||||||
ax.set_title("Summary of Categories by Place with Inner Divisions")
|
|
||||||
ax.set_xticks(np.arange(len(grouped["place"])))
|
|
||||||
ax.set_xticklabels(grouped["place"], rotation=45, ha="right")
|
|
||||||
ax.legend(loc='upper right')
|
|
||||||
|
|
||||||
# Display the plot
|
|
||||||
plt.show()
|
|
||||||
|
Reference in New Issue
Block a user