Spaces:
Sleeping
Sleeping
Update chart_generation.py
Browse files- chart_generation.py +29 -7
chart_generation.py
CHANGED
|
@@ -3,12 +3,34 @@ import matplotlib.pyplot as plt
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
df = pd.read_excel(file)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return fig
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
|
| 7 |
+
def create_bar_plot(file, x_values, y_values):
|
| 8 |
df = pd.read_excel(file)
|
| 9 |
+
|
| 10 |
+
df['Simple_Source'] = df[x_values].apply(lambda x: x.split()[0].split(',')[0] if type(x)==str else np.nan)
|
| 11 |
+
|
| 12 |
+
if y_values == "":
|
| 13 |
+
counts = df['Simple_Source'].value_counts()
|
| 14 |
+
fig = plt.figure(figsize=(10, 6))
|
| 15 |
+
counts.plot(kind='bar')
|
| 16 |
+
plt.title(f'Count of First Words in {x_values}')
|
| 17 |
+
plt.xlabel('First Word')
|
| 18 |
+
plt.ylabel('Count')
|
| 19 |
+
else:
|
| 20 |
+
count_df = df.groupby(['Type', 'Simple_Source']).size().unstack(fill_value=0)
|
| 21 |
+
fig = count_df.plot(kind='bar', stacked=True, figsize=(10, 7))
|
| 22 |
+
|
| 23 |
+
plt.legend(title='Shop', bbox_to_anchor=(1.05, 1), loc='upper left')
|
| 24 |
+
|
| 25 |
+
plt.xlabel(y_values)
|
| 26 |
+
plt.ylabel('Number of Contributions')
|
| 27 |
+
plt.title(f'Number of Contributions by {y_values} and {x_values}')
|
| 28 |
+
|
| 29 |
+
for i, bar in enumerate(ax.patches):
|
| 30 |
+
h = bar.get_height()
|
| 31 |
+
w = bar.get_width()
|
| 32 |
+
x = bar.get_x()
|
| 33 |
+
y = bar.get_y()
|
| 34 |
+
if h > 0:
|
| 35 |
+
plt.text(x + w/2, y + h/2, int(h), ha='center', va='center')
|
| 36 |
return fig
|