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 matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import plotly.express as px
|
||||
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"
|
||||
@@ -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
|
||||
grouped = df.groupby("place")[columns_to_summarize].sum().reset_index()
|
||||
|
||||
# Create a list of colors for the inner divisions
|
||||
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"]
|
||||
# Create a bar chart using Plotly
|
||||
fig = go.Figure()
|
||||
|
||||
# Create the figure and axis for the bar chart
|
||||
fig, ax = plt.subplots()
|
||||
# Add a bar trace for each category
|
||||
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
|
||||
bottom = np.zeros(len(grouped))
|
||||
for i, col in enumerate(columns_to_summarize):
|
||||
ax.bar(grouped["place"], grouped[col], 0.6, bottom=bottom, color=colors[i], label=col)
|
||||
bottom += grouped[col]
|
||||
# Customize the layout
|
||||
fig.update_layout(
|
||||
title="Summary of Categories by Place with Filtering",
|
||||
xaxis_title="Place",
|
||||
yaxis_title="Total Count",
|
||||
xaxis=dict(categoryorder='total descending'),
|
||||
barmode='group'
|
||||
)
|
||||
|
||||
# Customize the plot
|
||||
ax.set_ylabel("Total Count")
|
||||
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()
|
||||
# Show the interactive graph
|
||||
fig.show()
|
||||
|
Reference in New Issue
Block a user