File size: 7,031 Bytes
523f6c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
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")