CoolFace
Apppublic

Almaatla/Standard_Intelligence_Dev

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
chart_generation.py43 linesDownload Raw Back to root
1from excel_chat import get_columns2import matplotlib.pyplot as plt3import pandas as pd4import numpy as np5 6 7def create_bar_plot(file, x_values, y_values):8    df = pd.read_excel(file)9 10    df['Simple_X'] = df[x_values].apply(lambda x: ' '.join(x.split(',')[0].split()[:2]) if type(x)==str else np.nan)11 12    if y_values == "":13        counts = df['Simple_X'].value_counts()14        fig = plt.figure(figsize=(16, 7))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        df['Simple_Y'] = df[y_values].apply(lambda x: ' '.join(x.split(',')[0].split()[:2]) if type(x)==str else np.nan)21        22        count_df = df.groupby(['Simple_X', 'Simple_Y']).size().unstack(fill_value=0)23        fig, ax = plt.subplots(figsize=(16, 7))24 25        count_df.plot(kind='bar', stacked=True, ax=ax)26 27        28        plt.legend(title=y_values, bbox_to_anchor=(1.05, 1), loc='upper left')29        30        plt.xlabel(y_values)31        plt.ylabel('Number of Contributions')32        plt.title(f'Number of Contributions by {y_values} and {x_values}')33 34        for i, bar in enumerate(ax.patches):35            h = bar.get_height()36            w = bar.get_width()37            x = bar.get_x()38            y = bar.get_y()39            if h > 0:40                ax.text(x + w/2, y + h/2, int(h), ha='center', va='center')41 42        plt.tight_layout()43    return fig