Standard_Intelligence_Dev / chart_generation.py
YchKhan's picture
Update chart_generation.py
3643d7a verified
raw
history blame
1.32 kB
from excel_chat import get_columns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
def create_bar_plot(file, x_values, y_values):
df = pd.read_excel(file)
df['Simple_Source'] = df[x_values].apply(lambda x: x.split()[0].split(',')[0] if type(x)==str else np.nan)
if y_values == "":
counts = df['Simple_Source'].value_counts()
fig = plt.figure(figsize=(10, 6))
counts.plot(kind='bar')
plt.title(f'Count of First Words in {x_values}')
plt.xlabel('First Word')
plt.ylabel('Count')
else:
count_df = df.groupby(['Simple_Source', y_values]).size().unstack(fill_value=0)
fig, ax = plt.subplots(figsize=(16, 7))
count_df.plot(kind='bar', stacked=True, ax=ax)
plt.legend(title=y_values, bbox_to_anchor=(1.05, 1), loc='upper left')
plt.xlabel(y_values)
plt.ylabel('Number of Contributions')
plt.title(f'Number of Contributions by {y_values} and {x_values}')
for i, bar in enumerate(ax.patches):
h = bar.get_height()
w = bar.get_width()
x = bar.get_x()
y = bar.get_y()
if h > 0:
ax.text(x + w/2, y + h/2, int(h), ha='center', va='center')
plt.tight_layout()
return fig