File size: 3,264 Bytes
6f6318e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""

Ashley Wu 12/4/25

Creates visualizations for frontend of app 

"""
import matplotlib.pyplot as plt
import numpy as np
import gradio as gr
import math
import utils


def time_series(df, xcol, ycol):
    """

    Creates a time series graph 

    

    Args: 

        df: dataframe 

        xcol: column of x calues

        ycoL: column of y values

    Returns: 

        fig: time series graph

    """
    fig, ax = plt.subplots()

    if df[ycol].dtype.name == 'object':
        raise gr.Error("The Y value can not be a string or a category!", duration=10)
    
    
    x = df[xcol].to_numpy() 
    y = df[ycol].to_numpy()

    ax.plot(x, y, color='red', linewidth=2, marker='o')
    ax.set_xlabel(xcol)
    ax.set_ylabel(ycol)
    ax.set_title(f"Time Series of {xcol} and {ycol}")

    return fig

def bar_chart(df, xcol, ycol, aggmethod):
    """

    Creates bar chart of information 

    

    Args: 

        df: dataframe

        xcol: column of x values

        ycol: column of y values

        aggmethod: aggregation method 

    Return: 

        fig: bar chart 

    """

    df = utils.detect_categories(df)
    fig, ax = plt.subplots()

    if df[ycol].dtype.name == 'category' or df[ycol].dtype.name == 'object':
        raise gr.Error("The Y variable can not be a string or a category!", duration=10)
    if df[xcol].dtype.name != 'category':
        raise gr.Error("The X variable must be a category!")
    
    if aggmethod == 'sum':
        y = df.groupby(xcol)[ycol].sum()
    elif aggmethod == 'mean':
        y = df.groupby(xcol)[ycol].mean()
    elif aggmethod == 'count':
        y = df[xcol].value_counts().tolist()
    elif aggmethod == 'median':
        y = df.groupby(xcol)[ycol].median()
    else: 
        y = df[xcol].tolist() 

    x = df[xcol].unique().tolist()

    ax.bar(x, y)
    
    ax.set_xlabel('Category')
    ax.set_ylabel(ycol)
    ax.set_title(f'{aggmethod} by Category')
    ax.set_xticks(ticks=x)
    return fig

def histogram(df, col, bins):
    """

    Creates a histogram 

    

    Args: 

        df: dataframe

        col: data for histogram 

        bins: number of bins to put histogram data into

    Returns: 

        fig: histogram created 

    """
    fig, ax = plt.subplots()

    bins = math.floor(bins)

    if df[col].dtype.name == 'object':
        raise gr.Error("The Y value can not be a string or a category!", duration=10)
    
    ax.hist(df[col], bins)
    ax.set_xlabel(f'{col}')
    ax.set_title(f'Histogram of {col}')
    return fig

def scatter(df, xcol, ycol):
    """

    Creates a scatter plot

    

    Args: 

        df: dataframe 

        xcol: column of x values

        ycol: column of y values

    Returns: 

        fig: scatter plot 

    """
    fig, ax = plt.subplots()

    if df[ycol].dtype.name == 'object' or df[xcol].dtype.name == 'object':
        raise gr.Error("The X or Y values can not be a string or a category!", duration=10)
    
    x = df[xcol].to_numpy() 
    y = df[ycol].to_numpy()

    ax.plot(x, y, color='red', marker='o')
    ax.set_xlabel(xcol)
    ax.set_ylabel(ycol)
    ax.set_title(f"Scatter Plot of {xcol} and {ycol}")

    return fig