| """ |
| 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) |
|
|
| |
| plt.style.use('default') |
| sns.set_palette("husl") |
|
|
| |
| np.random.seed(42) |
| n = 1000 |
|
|
| |
| 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") |
|
|
| |
| print("\nπ¨ Creating Matplotlib visualizations...") |
|
|
| plt.figure(figsize=(20, 15)) |
|
|
| |
| 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) |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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') |
|
|
| |
| 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() |
|
|
| |
| print("\nπ Creating Seaborn visualizations...") |
|
|
| plt.figure(figsize=(20, 12)) |
|
|
| |
| plt.subplot(2, 3, 1) |
| sns.scatterplot(data=df.sample(200), x='x', y='y', hue='category') |
| plt.title('Seaborn Scatter Plot') |
|
|
| |
| plt.subplot(2, 3, 2) |
| sns.violinplot(data=df, x='category', y='value') |
| plt.title('Violin 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') |
|
|
| |
| plt.subplot(2, 3, 4) |
| sns.swarmplot(data=df.sample(300), x='category', y='value') |
| plt.title('Swarm Plot') |
|
|
| |
| plt.subplot(2, 3, 5) |
| sns.kdeplot(data=df.sample(500), x='x', y='y', fill=True) |
| plt.title('Kernel Density Estimate') |
|
|
| |
| plt.subplot(2, 3, 6) |
| sns.countplot(data=df, x='category', palette='husl') |
| plt.title('Count Plot') |
|
|
| plt.tight_layout() |
| plt.show() |
|
|
| |
| print("\nπ Creating Plotly interactive visualizations...") |
|
|
| |
| fig1 = px.scatter(df.sample(500), x='x', y='y', color='category', |
| size='size', hover_data=['value'], |
| title='Interactive Scatter Plot') |
| fig1.show() |
|
|
| |
| fig2 = px.scatter_3d(df.sample(500), x='x', y='y', z='value', |
| color='category', size='size', |
| title='Interactive 3D Scatter Plot') |
| fig2.show() |
|
|
| |
| fig3 = px.line(df[:100], x='time', y='value', |
| title='Interactive Time Series') |
| fig3.show() |
|
|
| |
| fig4 = px.box(df, x='category', y='value', |
| title='Interactive Box Plot') |
| fig4.show() |
|
|
| |
| fig5 = px.violin(df, x='category', y='value', |
| box=True, title='Interactive Violin Plot') |
| fig5.show() |
|
|
| |
| 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}]] |
| ) |
|
|
| |
| 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() |
|
|
| |
| print("\n⨠Creating additional visualization examples...") |
|
|
| |
| fig7 = px.scatter(df.sample(100), x='x', y='y', animation_frame='category', |
| size='size', color='value', |
| title='Animated Scatter Plot') |
| fig7.show() |
|
|
| |
| fig8 = px.sunburst(df, path=['category'], values='value', |
| title='Sunburst Chart') |
| fig8.show() |
|
|
| |
| fig9 = px.treemap(df, path=['category'], values='value', |
| title='Treemap') |
| fig9.show() |
|
|
| |
| 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") |
|
|