Spaces:
Sleeping
Sleeping
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_X'] = df[x_values].apply(lambda x: ' '.join(x.split(',')[0].split()[:2]) if type(x)==str else np.nan) | |
if y_values == "": | |
counts = df['Simple_Source'].value_counts() | |
fig = plt.figure(figsize=(16, 7)) | |
counts.plot(kind='bar') | |
plt.title(f'Count of First Words in {x_values}') | |
plt.xlabel('First Word') | |
plt.ylabel('Count') | |
else: | |
df['Simple_Y'] = df[y_values].apply(lambda x: ' '.join(x.split(',')[0].split()[:2]) if type(x)==str else np.nan) | |
count_df = df.groupby(['Simple_Source', 'Simple_Y']).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 |