File size: 1,449 Bytes
dc06294
 
f8e763e
 
dc06294
b8b708a
 
dc06294
b8b708a
576b629
b8b708a
 
33bdc38
576b629
b8b708a
 
 
 
 
576b629
 
33bdc38
3643d7a
cc58222
 
 
b8b708a
0b2c90c
b8b708a
 
 
 
 
d3336a3
b8b708a
 
 
 
 
d3336a3
 
 
dc06294
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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_X'].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_X', '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