code-interpreter-sandbox / examples /visualization_example.py
likhonsheikh's picture
Initial commit: Advanced Code Interpreter Sandbox
523f6c3 verified
"""
Data Visualization Example
Demonstrates various plotting libraries and techniques
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.offline as pyo
print("=" * 60)
print("DATA VISUALIZATION EXAMPLE")
print("=" * 60)
# Set style
plt.style.use('default')
sns.set_palette("husl")
# Generate sample data
np.random.seed(42)
n = 1000
# Create diverse dataset
data = {
'x': np.random.randn(n),
'y': np.random.randn(n),
'category': np.random.choice(['A', 'B', 'C', 'D'], n),
'size': np.random.uniform(10, 100, n),
'value': np.random.randn(n).cumsum(),
'time': pd.date_range('2023-01-01', periods=n, freq='H')
}
df = pd.DataFrame(data)
print("\nπŸ“Š Dataset created with", n, "records")
# MATPLOTLIB VISUALIZATIONS
print("\n🎨 Creating Matplotlib visualizations...")
plt.figure(figsize=(20, 15))
# 1. Line plot
plt.subplot(3, 3, 1)
plt.plot(df['time'][:100], df['value'][:100])
plt.title('Time Series (Line Plot)')
plt.xlabel('Time')
plt.ylabel('Value')
plt.xticks(rotation=45)
# 2. Scatter plot
plt.subplot(3, 3, 2)
plt.scatter(df['x'], df['y'], c=df['value'], cmap='viridis', alpha=0.6)
plt.colorbar(label='Value')
plt.title('Scatter Plot with Color')
plt.xlabel('X')
plt.ylabel('Y')
# 3. Histogram
plt.subplot(3, 3, 3)
plt.hist(df['x'], bins=30, alpha=0.7, color='skyblue', edgecolor='black')
plt.title('Histogram')
plt.xlabel('X Values')
plt.ylabel('Frequency')
# 4. Box plot
plt.subplot(3, 3, 4)
categories = [df[df['category'] == c]['value'].values for c in ['A', 'B', 'C', 'D']]
plt.boxplot(categories, labels=['A', 'B', 'C', 'D'])
plt.title('Box Plot by Category')
plt.ylabel('Value')
# 5. Bar chart
plt.subplot(3, 3, 5)
category_counts = df['category'].value_counts()
plt.bar(category_counts.index, category_counts.values, color=['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A'])
plt.title('Category Distribution')
plt.xlabel('Category')
plt.ylabel('Count')
# 6. Area plot
plt.subplot(3, 3, 6)
plt.fill_between(range(100), df['value'][:100], alpha=0.3)
plt.plot(range(100), df['value'][:100])
plt.title('Area Plot')
plt.xlabel('Index')
plt.ylabel('Value')
# 7. Heatmap (correlation)
plt.subplot(3, 3, 7)
corr_data = df[['x', 'y', 'value']].corr()
sns.heatmap(corr_data, annot=True, cmap='coolwarm', center=0)
plt.title('Correlation Heatmap')
# 8. 3D scatter
from mpl_toolkits.mplot3d import Axes3D
ax = plt.subplot(3, 3, 8, projection='3d')
scatter = ax.scatter(df['x'][:200], df['y'][:200], df['value'][:200],
c=df['value'][:200], cmap='viridis')
ax.set_title('3D Scatter Plot')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Value')
# 9. Pie chart
plt.subplot(3, 3, 9)
plt.pie(category_counts.values, labels=category_counts.index, autopct='%1.1f%%',
colors=['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A'])
plt.title('Pie Chart')
plt.tight_layout()
plt.show()
# SEABORN VISUALIZATIONS
print("\nπŸ“ˆ Creating Seaborn visualizations...")
plt.figure(figsize=(20, 12))
# 1. Pairplot
plt.subplot(2, 3, 1)
sns.scatterplot(data=df.sample(200), x='x', y='y', hue='category')
plt.title('Seaborn Scatter Plot')
# 2. Violin plot
plt.subplot(2, 3, 2)
sns.violinplot(data=df, x='category', y='value')
plt.title('Violin Plot')
# 3. Joint plot
plt.subplot(2, 3, 3)
sns.scatterplot(data=df.sample(200), x='x', y='y')
sns.regplot(data=df.sample(200), x='x', y='y', scatter=False, color='red')
plt.title('Scatter with Regression Line')
# 4. Swarm plot
plt.subplot(2, 3, 4)
sns.swarmplot(data=df.sample(300), x='category', y='value')
plt.title('Swarm Plot')
# 5. KDE plot
plt.subplot(2, 3, 5)
sns.kdeplot(data=df.sample(500), x='x', y='y', fill=True)
plt.title('Kernel Density Estimate')
# 6. Count plot
plt.subplot(2, 3, 6)
sns.countplot(data=df, x='category', palette='husl')
plt.title('Count Plot')
plt.tight_layout()
plt.show()
# PLOTLY INTERACTIVE VISUALIZATIONS
print("\nπŸš€ Creating Plotly interactive visualizations...")
# 1. Interactive scatter plot
fig1 = px.scatter(df.sample(500), x='x', y='y', color='category',
size='size', hover_data=['value'],
title='Interactive Scatter Plot')
fig1.show()
# 2. Interactive 3D scatter
fig2 = px.scatter_3d(df.sample(500), x='x', y='y', z='value',
color='category', size='size',
title='Interactive 3D Scatter Plot')
fig2.show()
# 3. Line chart
fig3 = px.line(df[:100], x='time', y='value',
title='Interactive Time Series')
fig3.show()
# 4. Box plot
fig4 = px.box(df, x='category', y='value',
title='Interactive Box Plot')
fig4.show()
# 5. Violin plot
fig5 = px.violin(df, x='category', y='value',
box=True, title='Interactive Violin Plot')
fig5.show()
# 6. Subplot example
fig6 = make_subplots(
rows=2, cols=2,
subplot_titles=('Scatter', 'Line', 'Bar', 'Box'),
specs=[[{"secondary_y": True}, {"secondary_y": False}],
[{"secondary_y": False}, {"secondary_y": False}]]
)
# Add traces
fig6.add_trace(go.Scatter(x=df['x'][:200], y=df['y'][:200],
mode='markers', name='Scatter'), row=1, col=1)
fig6.add_trace(go.Line(x=range(100), y=df['value'][:100],
name='Line'), row=1, col=2)
fig6.add_trace(go.Bar(x=category_counts.index, y=category_counts.values,
name='Bar'), row=2, col=1)
fig6.add_trace(go.Box(y=df['value'], name='Box'), row=2, col=2)
fig6.update_layout(height=600, title_text="Subplot Example")
fig6.show()
# ADDITIONAL EXAMPLES
print("\n✨ Creating additional visualization examples...")
# Animated scatter plot (plotly)
fig7 = px.scatter(df.sample(100), x='x', y='y', animation_frame='category',
size='size', color='value',
title='Animated Scatter Plot')
fig7.show()
# Sunburst chart
fig8 = px.sunburst(df, path=['category'], values='value',
title='Sunburst Chart')
fig8.show()
# Treemap
fig9 = px.treemap(df, path=['category'], values='value',
title='Treemap')
fig9.show()
# Summary
print("\n" + "=" * 60)
print("VISUALIZATION SUMMARY")
print("=" * 60)
print("βœ… Matplotlib plots: 9 different chart types")
print("βœ… Seaborn plots: 6 statistical visualizations")
print("βœ… Plotly plots: 9 interactive visualizations")
print("βœ… Total: 24 unique visualization examples")
print("\nπŸ“š Visualization libraries demonstrated:")
print(" - matplotlib: Static, publication-quality plots")
print(" - seaborn: Statistical data visualization")
print(" - plotly: Interactive, web-based visualizations")
print(" - numpy/pandas: Data generation and manipulation")
print("\n🎨 Visualization types covered:")
print(" - Line charts, Scatter plots, Histograms")
print(" - Box plots, Violin plots, Bar charts")
print(" - Heatmaps, KDE plots, 3D plots")
print(" - Interactive plots, Animations")
print(" - Sunburst, Treemap, Subplots")