|
|
import gradio as gr |
|
|
import seaborn as sns |
|
|
import matplotlib.pyplot as plt |
|
|
import plotly.express as px |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
df = sns.load_dataset('tips') |
|
|
|
|
|
|
|
|
def basic_plot(plot_type): |
|
|
plt.figure(figsize=(6,4)) |
|
|
if plot_type == "Scatter": |
|
|
sns.scatterplot(x='total_bill', y='tip', data=df) |
|
|
elif plot_type == "Line": |
|
|
sns.lineplot(x='size', y='tip', data=df) |
|
|
elif plot_type == "Bar": |
|
|
sns.barplot(x='day', y='total_bill', data=df) |
|
|
elif plot_type == "Histogram": |
|
|
sns.histplot(df['total_bill'], bins=10, kde=True) |
|
|
plt.title(f"{plot_type} Plot") |
|
|
return plt.gcf() |
|
|
|
|
|
def advanced_plot(plot_type): |
|
|
if plot_type == "Heatmap": |
|
|
plt.figure(figsize=(6,4)) |
|
|
sns.heatmap(df.corr(), annot=True, cmap='coolwarm') |
|
|
plt.title("Heatmap") |
|
|
return plt.gcf() |
|
|
elif plot_type == "Pair Plot": |
|
|
g = sns.pairplot(df[['total_bill', 'tip', 'size']]) |
|
|
return g.fig |
|
|
elif plot_type == "Facet Grid": |
|
|
g = sns.FacetGrid(df, col='sex', row='time') |
|
|
g.map(sns.scatterplot, 'total_bill', 'tip') |
|
|
return g.fig |
|
|
|
|
|
def interactive_plot(_): |
|
|
fig = px.scatter(df, x='total_bill', y='tip', color='sex', |
|
|
size='size', hover_data=['day', 'time'], |
|
|
title='Interactive Scatter Plot') |
|
|
return fig |
|
|
|
|
|
def storytelling_plot(_): |
|
|
avg_tip = df.groupby('day')['tip'].mean().reset_index() |
|
|
plt.figure(figsize=(6,4)) |
|
|
sns.barplot(x='day', y='tip', data=avg_tip, palette='viridis') |
|
|
plt.axhline(y=avg_tip['tip'].mean(), color='red', linestyle='--', label='Overall Avg') |
|
|
plt.title('Average Tip by Day') |
|
|
plt.legend() |
|
|
return plt.gcf() |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## π Data Visualization Explorer") |
|
|
|
|
|
with gr.Tab("Basic Plots"): |
|
|
with gr.Row(): |
|
|
plot_type = gr.Dropdown(["Scatter", "Line", "Bar", "Histogram"], label="Choose Plot Type") |
|
|
submit_btn = gr.Button("Submit") |
|
|
plot_output = gr.Plot() |
|
|
submit_btn.click(fn=basic_plot, inputs=plot_type, outputs=plot_output) |
|
|
|
|
|
with gr.Tab("Advanced Plots"): |
|
|
with gr.Row(): |
|
|
adv_type = gr.Dropdown(["Heatmap", "Pair Plot", "Facet Grid"], label="Choose Advanced Plot") |
|
|
adv_btn = gr.Button("Submit") |
|
|
adv_output = gr.Plot() |
|
|
adv_btn.click(fn=advanced_plot, inputs=adv_type, outputs=adv_output) |
|
|
|
|
|
with gr.Tab("Interactive Plot"): |
|
|
inter_btn = gr.Button("Submit") |
|
|
inter_output = gr.Plot() |
|
|
inter_btn.click(fn=interactive_plot, inputs=[], outputs=inter_output) |
|
|
|
|
|
with gr.Tab("Storytelling"): |
|
|
story_btn = gr.Button("Submit") |
|
|
story_output = gr.Plot() |
|
|
story_btn.click(fn=storytelling_plot, inputs=[], outputs=story_output) |
|
|
|
|
|
demo.launch() |
|
|
|