Files
DHGeography/ratios.py
2023-10-17 18:30:41 +03:00

36 lines
1.2 KiB
Python

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# 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)
# Define the columns to summarize
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 the figure and axis for the bar chart
fig, ax = plt.subplots()
# 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 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()