File size: 4,154 Bytes
848c0b1
 
 
 
 
 
 
 
 
2acc696
 
848c0b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166c645
848c0b1
 
 
 
166c645
848c0b1
 
 
 
 
 
166c645
848c0b1
 
 
 
 
 
166c645
848c0b1
 
 
 
 
 
 
 
166c645
848c0b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2024 Aniket Kamble
# SPDX-License-Identifier: MIT

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px

def main():
    st.set_page_config(page_title="Advance Data Visualization Tool", layout="wide")
    st.title("📊 Advance Data Visualization Tool")

    # Sidebar for file upload and chart selection
    with st.sidebar:
        st.header("Upload Data")
        uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])

    if uploaded_file:
        df = pd.read_csv(uploaded_file)
        display_data_preview(df)
        selected_features = select_features(df)

        if selected_features:
            if validate_features(df, selected_features):
                chart_type = select_chart_type()
                display_chart(df, selected_features, chart_type)

def display_data_preview(df):
    # Displays a preview of the uploaded data
    with st.expander("Preview of the uploaded data", expanded=False):
        st.write(df.head())

def select_features(df):
    # Allows the user to select features for comparison
    selected_features = st.multiselect("Select features for comparison", df.columns)
    if not selected_features:
        st.warning("Please select at least one feature for comparison.")
    return selected_features

def validate_features(df, selected_features):
    # Validates that selected features are numeric
    if not all(df[feature].dtype in (int, float) for feature in selected_features):
        st.error("Selected features must be numeric for visualization.")
        return False
    return True

def select_chart_type():
    # Allows the user to select the type of chart
    with st.sidebar:
        st.header("Chart Selection")
        return st.selectbox(
            "Select the type of chart",
            ["Line Chart", "Bar Chart", "Scatter Plot", "Pie Chart", "Histogram", "Heatmap"],
        )

def display_chart(df, selected_features, chart_type):
    # Displays the selected chart
    st.subheader(f"{chart_type}")
    if chart_type == "Line Chart":
        generate_line_chart(df, selected_features)
    elif chart_type == "Bar Chart":
        generate_bar_chart(df, selected_features)
    elif chart_type == "Scatter Plot":
        generate_scatter_plot(df, selected_features)
    elif chart_type == "Pie Chart":
        generate_pie_chart(df, selected_features)
    elif chart_type == "Histogram":
        generate_histogram(df, selected_features[0])
    elif chart_type == "Heatmap":
        generate_heatmap(df, selected_features)

def generate_line_chart(df, selected_features):
    fig, ax = plt.subplots(figsize=(15, 8))
    for feature in selected_features:
        ax.plot(df.index, df[feature], label=feature)

    ax.set_xlabel("Index")
    ax.set_ylabel("Values")
    ax.set_title("Line Chart")
    ax.legend()
    st.pyplot(fig)

def generate_bar_chart(df, selected_features):
    fig = px.bar(df, x=df.index, y=selected_features, barmode="group")
    fig.update_layout(title="Bar Chart", xaxis_title="Index", yaxis_title="Values", height=600, width=1000)
    st.plotly_chart(fig)

def generate_scatter_plot(df, selected_features):
    fig = px.scatter(
        df,
        x=selected_features[0],
        y=selected_features[1],
        title="Scatter Plot",
        labels={
            selected_features[0]: selected_features[0],
            selected_features[1]: selected_features[1],
        },
        height=600,
        width=1000
    )
    st.plotly_chart(fig)

def generate_pie_chart(df, selected_features):
    fig = px.pie(df, names=selected_features, title="Pie Chart", hole=0.3, height=600, width=1000)
    st.plotly_chart(fig)

def generate_histogram(df, selected_feature):
    fig, ax = plt.subplots(figsize=(15, 8))
    ax.hist(df[selected_feature], bins=20, edgecolor="black")
    ax.set_xlabel(selected_feature)
    ax.set_ylabel("Frequency")
    ax.set_title("Histogram")
    st.pyplot(fig)

def generate_heatmap(df, selected_features):
    fig = px.imshow(df[selected_features].corr(), title="Heatmap", height=600, width=1000)
    st.plotly_chart(fig)

if __name__ == "__main__":
    main()