Standard_Intelligence_Dev / chart_generation.py
YchKhan's picture
Update chart_generation.py
bd38b84 verified
raw
history blame
1.26 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(['Type', 'Simple_Source']).size().unstack(fill_value=0)
fig = count_df.plot(kind='bar', stacked=True, figsize=(10, 7))
plt.legend(title='Shop', 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(fig.patches):
h = bar.get_height()
w = bar.get_width()
x = bar.get_x()
y = bar.get_y()
if h > 0:
plt.text(x + w/2, y + h/2, int(h), ha='center', va='center')
return fig