Spaces:
Running
Running
File size: 56,794 Bytes
3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 84b67b2 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 939a3b2 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 939a3b2 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc 3ca3212 13d9acc | 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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Data Visualization Masterclass</title>
<link rel="stylesheet" href="style.css" />
<!-- MathJax for rendering LaTeX formulas -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
}
};
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body>
<div class="app flex">
<!-- Sidebar Navigation -->
<aside class="sidebar" id="sidebar">
<h1 class="sidebar__title">π Data Visualization</h1>
<nav>
<h3 class="sidebar__section">Foundations</h3>
<ul class="nav__list" id="navList">
<li><a href="#intro" class="nav__link">π― Why Visualize Data?</a></li>
<li><a href="#perception" class="nav__link">ποΈ Visual Perception</a></li>
<li><a href="#grammar" class="nav__link">π Grammar of Graphics</a></li>
<li><a href="#choosing-charts" class="nav__link">π¨ Choosing the Right Chart</a></li>
</ul>
<h3 class="sidebar__section">Matplotlib Essentials</h3>
<ul class="nav__list">
<li><a href="#matplotlib-anatomy" class="nav__link">π¬ Figure Anatomy</a></li>
<li><a href="#basic-plots" class="nav__link">π Basic Plots</a></li>
<li><a href="#subplots" class="nav__link">π² Subplots & Layouts</a></li>
<li><a href="#styling" class="nav__link">π¨ Styling & Themes</a></li>
</ul>
<h3 class="sidebar__section">Seaborn Statistical Viz</h3>
<ul class="nav__list">
<li><a href="#seaborn-intro" class="nav__link">π Seaborn Overview</a></li>
<li><a href="#distributions" class="nav__link">π Distribution Plots</a></li>
<li><a href="#relationships" class="nav__link">π Relationship Plots</a></li>
<li><a href="#categorical" class="nav__link">π¦ Categorical Plots</a></li>
<li><a href="#heatmaps" class="nav__link">π₯ Heatmaps & Clusters</a></li>
</ul>
<h3 class="sidebar__section">Interactive Visualization</h3>
<ul class="nav__list">
<li><a href="#plotly" class="nav__link">π Plotly Express</a></li>
<li><a href="#animations" class="nav__link">π¬ Animations</a></li>
<li><a href="#dashboards" class="nav__link">π± Dashboards (Streamlit)</a></li>
</ul>
<h3 class="sidebar__section">Advanced Topics</h3>
<ul class="nav__list">
<li><a href="#geospatial" class="nav__link">πΊοΈ Geospatial Viz</a></li>
<li><a href="#3d-plots" class="nav__link">π² 3D Visualization</a></li>
<li><a href="#storytelling" class="nav__link">π Data Storytelling</a></li>
</ul>
</nav>
</aside>
<!-- Main Content -->
<main class="content" id="content">
<!-- ============================ 1. INTRO ============================ -->
<section id="intro" class="topic-section">
<h2>π― Why Visualize Data?</h2>
<p>Data visualization transforms abstract numbers into visual stories. The human brain processes images 60,000Γ
faster than text. Visualization helps us <strong>explore</strong>, <strong>analyze</strong>, and
<strong>communicate</strong> data effectively.
</p>
<div class="info-card">
<strong>Anscombe's Quartet:</strong> Four datasets with nearly identical statistical properties (mean,
variance, correlation) that look completely different when plotted. This demonstrates why visualization is
essential - statistics alone can be misleading!
</div>
<div class="canvas-wrapper">
<canvas id="canvas-anscombe" width="800" height="400"></canvas>
</div>
<h3>Three Purposes of Visualization</h3>
<div class="info-card">
<strong>1. Exploratory:</strong> Discover patterns, anomalies, and insights in your data<br>
<strong>2. Explanatory:</strong> Communicate findings to stakeholders clearly<br>
<strong>3. Confirmatory:</strong> Verify hypotheses and validate models
</div>
<div class="callout callout--insight">π‘ "The greatest value of a picture is when it forces us to notice what we
never expected to see." β John Tukey</div>
<div class="callout callout--tip">β
Always start with visualization before building ML models.</div>
</section>
<!-- ====================== 2. VISUAL PERCEPTION ================== -->
<section id="perception" class="topic-section">
<h2>ποΈ Visual Perception & Pre-attentive Attributes</h2>
<p>The human visual system can detect certain visual attributes almost instantly (< 250ms) without conscious
effort. These are called <strong>pre-attentive attributes</strong>.</p>
<div class="info-card">
<strong>Pre-attentive Attributes:</strong>
<ul>
<li><strong>Position:</strong> Most accurate for quantitative data (use X/Y axes)</li>
<li><strong>Length:</strong> Bar charts leverage this effectively</li>
<li><strong>Color Hue:</strong> Best for categorical distinctions</li>
<li><strong>Color Intensity:</strong> Good for gradients/magnitude</li>
<li><strong>Size:</strong> Bubble charts, but humans underestimate area</li>
<li><strong>Shape:</strong> Useful for categories, but limit to 5-7 shapes</li>
<li><strong>Orientation:</strong> Lines, angles</li>
</ul>
</div>
<div class="form-group">
<button id="btn-position" class="btn btn--primary">Position Encoding</button>
<button id="btn-color" class="btn btn--primary">Color Encoding</button>
<button id="btn-size" class="btn btn--primary">Size Encoding</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-perception" width="700" height="350"></canvas>
</div>
<h3>Cleveland & McGill's Accuracy Ranking</h3>
<div class="info-card">
<strong>Most Accurate β Least Accurate:</strong><br>
1. Position on common scale (bar chart)<br>
2. Position on non-aligned scale (multiple axes)<br>
3. Length (bar)<br>
4. Angle, Slope<br>
5. Area<br>
6. Volume, Curvature<br>
7. Color saturation, Color hue
</div>
<div class="callout callout--mistake">β οΈ Pie charts use angle (low accuracy). Bar charts are almost always
better!</div>
<div class="callout callout--tip">β
Use position for most important data, color for categories.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: The Weber-Fechner Law</h3>
<p>Why are humans bad at comparing bubble sizes (area) but great at comparing bar chart heights
(length/position)? Human perception of physical magnitudes follows a logarithmic scale, not a linear one.
</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ \frac{\Delta I}{I} = k \quad \Rightarrow \quad S = c \ln\left(\frac{I}{I_0}\right) $$
</div>
<ul style="margin-bottom: 0;">
<li><strong>$I$</strong>: Initial stimulus intensity (e.g., initial bubble area)</li>
<li><strong>$\Delta I$</strong>: Just Noticeable Difference (JND) required to perceive a change</li>
<li><strong>$k$</strong>: Weber's constant. For length/position $k \approx 0.03$ (very sensitive), but for
area $k \approx 0.10$ to $0.20$ (very insensitive).</li>
</ul>
</div>
</section>
<!-- ====================== 3. GRAMMAR OF GRAPHICS ================== -->
<section id="grammar" class="topic-section">
<h2>π The Grammar of Graphics</h2>
<p>The Grammar of Graphics (Wilkinson, 1999) is a framework for describing statistical graphics. It's the
foundation of ggplot2 (R) and influences Seaborn, Altair, and Plotly.</p>
<div class="info-card">
<strong>Components of a Graphic:</strong>
<ul>
<li><strong>Data:</strong> The dataset being visualized</li>
<li><strong>Aesthetics (aes):</strong> Mapping data to visual properties (x, y, color, size)</li>
<li><strong>Geometries (geom):</strong> Visual elements (points, lines, bars, areas)</li>
<li><strong>Facets:</strong> Subplots by categorical variable</li>
<li><strong>Statistics:</strong> Transformations (binning, smoothing, aggregation)</li>
<li><strong>Coordinates:</strong> Cartesian, polar, map projections</li>
<li><strong>Themes:</strong> Non-data visual elements (fonts, backgrounds)</li>
</ul>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-grammar" width="800" height="400"></canvas>
</div>
<div class="callout callout--insight">π‘ Understanding Grammar of Graphics makes you a better visualizer in ANY
library.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Coordinate Transformations</h3>
<p>When mapping data to visuals, the coordinate system applies a mathematical transformation matrix. For
example, converting standard Cartesian coordinates $(x, y)$ to Polar coordinates $(r, \theta)$ to render a
pie chart or Coxcomb plot:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; overflow-x: auto; color: #e4e6eb;">
$$ r = \sqrt{x^2 + y^2} $$
$$ \theta = \text{atan2}(y, x) $$
$$ \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} r \cos(\theta) \\ r \sin(\theta) \end{bmatrix} $$
</div>
<p style="margin-bottom: 0;">This is why pie charts are computationally and perceptually different from bar
chartsβthey apply a non-linear polar transformation to the linear data dimensions.</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>app.py - Grammar of Graphics with Plotnine (Python)</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import pandas as pd
from plotnine import *
# Following Grammar of Graphics exactly:
# Data (mpg) -> Aesthetics (x,y,color) -> Geometries (point, smooth)
plot = (
ggplot(mpg, aes(x='displ', y='hwy', color='class'))
+ geom_point(size=3, alpha=0.7)
+ geom_smooth(method='lm', se=False) # Add regression line
+ theme_minimal() # Add theme
+ labs(title='Engine Displacement vs Highway MPG')
)
print(plot)</code></pre>
</div>
</section>
<!-- ====================== 4. CHOOSING CHARTS ================== -->
<section id="choosing-charts" class="topic-section">
<h2>π¨ Choosing the Right Chart</h2>
<p>The best visualization depends on your <strong>data type</strong> and <strong>question</strong>. Here's a
decision guide:</p>
<div class="info-card">
<strong>Single Variable (Univariate):</strong><br>
β’ Continuous: Histogram, KDE, Box plot, Violin plot<br>
β’ Categorical: Bar chart, Count plot<br><br>
<strong>Two Variables (Bivariate):</strong><br>
β’ Both Continuous: Scatter plot, Line chart, Hexbin, 2D histogram<br>
β’ Continuous + Categorical: Box plot, Violin, Strip, Swarm<br>
β’ Both Categorical: Heatmap, Grouped bar chart<br><br>
<strong>Multiple Variables (Multivariate):</strong><br>
β’ Pair plot (scatterplot matrix)<br>
β’ Parallel coordinates<br>
β’ Heatmap correlation matrix<br>
β’ Faceted plots (small multiples)
</div>
<div class="form-group">
<button id="btn-comparison" class="btn btn--primary">Comparison</button>
<button id="btn-composition" class="btn btn--primary">Composition</button>
<button id="btn-distribution" class="btn btn--primary">Distribution</button>
<button id="btn-relationship" class="btn btn--primary">Relationship</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-choosing" width="800" height="400"></canvas>
</div>
<h3>Common Chart Mistakes</h3>
<div class="callout callout--mistake">β οΈ <strong>Pie charts for many categories</strong> - Use bar chart instead
</div>
<div class="callout callout--mistake">β οΈ <strong>3D effects on 2D data</strong> - Distorts perception</div>
<div class="callout callout--mistake">β οΈ <strong>Truncated Y-axis</strong> - Exaggerates differences</div>
<div class="callout callout--mistake">β οΈ <strong>Rainbow color scales</strong> - Not perceptually uniform</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Information Entropy in Visuals</h3>
<p>How much data can a chart "handle" before it becomes cluttered? We can use Shannon Entropy ($H$) to
quantify the visual information density. If a chart has $n$ visual marks (dots, lines) with probabilities
$p_i$ of drawing attention:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; color: #e4e6eb;">
$$ H(X) = - \sum_{i=1}^{n} p_i \log_2(p_i) $$
</div>
<p style="margin-bottom: 0;"><strong>Takeaway:</strong> If you add too many dimensions (color, size, shape
simultaneously) on a single plot, the entropy $H$ exceeds human working memory limits ($\approx 2.5$ bits),
leading to chart fatigue. This is mathematically why "less is more" in dashboard design.</p>
</div>
</section>
<!-- ====================== 5. MATPLOTLIB ANATOMY ================== -->
<section id="matplotlib-anatomy" class="topic-section">
<h2>π¬ Matplotlib Figure Anatomy</h2>
<p>Understanding Matplotlib's object hierarchy is key to creating professional visualizations.</p>
<div class="info-card">
<strong>Hierarchical Structure:</strong><br>
<code>Figure β Axes β Axis β Tick β Label</code><br><br>
β’ <strong>Figure:</strong> The overall window/canvas<br>
β’ <strong>Axes:</strong> The actual plot area (NOT the X/Y axis!)<br>
β’ <strong>Axis:</strong> The X or Y axis with ticks and labels<br>
β’ <strong>Artist:</strong> Everything visible (lines, text, patches)
</div>
<div class="canvas-wrapper">
<canvas id="canvas-anatomy" width="800" height="500"></canvas>
</div>
<h3>Two Interfaces</h3>
<div class="info-card">
<strong>1. pyplot (MATLAB-style):</strong> Quick, implicit state<br>
<code>plt.plot(x, y)</code><br>
<code>plt.xlabel('Time')</code><br>
<code>plt.show()</code><br><br>
<strong>2. Object-Oriented (OO):</strong> Explicit, recommended for complex plots<br>
<code>fig, ax = plt.subplots()</code><br>
<code>ax.plot(x, y)</code><br>
<code>ax.set_xlabel('Time')</code>
</div>
<div class="callout callout--tip">β
Always use OO interface for publication-quality plots.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Affine Transformations</h3>
<p>How does Matplotlib convert your data coordinates (e.g., $x \in [0, 1000]$) into physical pixels on your
screen? It uses a continuous pipeline of <strong>Affine Transformation Matrices</strong>:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; overflow-x: auto; color: #e4e6eb;">
$$ \begin{bmatrix} x_{\text{display}} \\ y_{\text{display}} \\ 1 \end{bmatrix} = \begin{bmatrix} s_x & 0 &
t_x \\ 0 & s_y & t_y \\ 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} x_{\text{data}} \\ y_{\text{data}} \\ 1
\end{bmatrix} $$
</div>
<p style="margin-bottom: 0;">This matrix $T$ scales ($s_x, s_y$) and translates ($t_x, t_y$) data points. The
transformation pipeline is: Data $\rightarrow$ Axes (relative 0-1) $\rightarrow$ Figure (inches)
$\rightarrow$ Display (pixels based on DPI).</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>plot.py - Matplotlib Object-Oriented Setup</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import matplotlib.pyplot as plt
# 1. Create the Figure (The Canvas) and Axes (The Artist)
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
# 2. Draw on the Axes
ax.plot([1, 2, 3], [4, 5, 2], marker='o', label='Data A')
# 3. Configure the Axes (Anatomy elements)
ax.set_title("My First OOP Plot", fontsize=16, fontweight='bold')
ax.set_xlabel("X-Axis (Units)", fontsize=12)
ax.set_ylabel("Y-Axis (Units)", fontsize=12)
# Set limits and ticks
ax.set_xlim(0, 4)
ax.set_ylim(0, 6)
ax.grid(True, linestyle='--', alpha=0.7)
# 4. Add accessories
ax.legend(loc='upper right')
# 5. Render or Save
plt.tight_layout() # Prevent clipping
plt.show()
# fig.savefig('my_plot.png', dpi=300)</code></pre>
</div>
</section>
<!-- ====================== 6. BASIC PLOTS ================== -->
<section id="basic-plots" class="topic-section">
<h2>π Basic Matplotlib Plots</h2>
<p>Master the fundamental plot types that form the foundation of data visualization.</p>
<div class="form-group">
<button id="btn-line" class="btn btn--primary">Line Plot</button>
<button id="btn-scatter" class="btn btn--primary">Scatter Plot</button>
<button id="btn-bar" class="btn btn--primary">Bar Chart</button>
<button id="btn-hist" class="btn btn--primary">Histogram</button>
<button id="btn-pie" class="btn btn--primary">Pie Chart</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-basic" width="800" height="400"></canvas>
</div>
<h3>Code Examples</h3>
<div class="info-card">
<strong>Line Plot:</strong><br>
<code>ax.plot(x, y, color='blue', linestyle='--', marker='o', label='Series A')</code><br><br>
<strong>Scatter Plot:</strong><br>
<code>ax.scatter(x, y, c=colors, s=sizes, alpha=0.7, cmap='viridis')</code><br><br>
<strong>Bar Chart:</strong><br>
<code>ax.bar(categories, values, color='steelblue', edgecolor='black')</code><br><br>
<strong>Histogram:</strong><br>
<code>ax.hist(data, bins=30, edgecolor='white', density=True)</code>
</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: The Freedman-Diaconis Rule</h3>
<p>When you call a histogram without specifying bins, how does the library choose the optimal bin width?
Advanced statistical libraries use the Freedman-Diaconis rule, which minimizes the integral of the squared
difference between the histogram and the true underlying probability density:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ \text{Bin Width } (h) = 2 \frac{\text{IQR}(x)}{\sqrt[3]{n}} $$
</div>
<p style="margin-bottom: 0;">Where $\text{IQR}$ is the Interquartile Range and $n$ is the number of
observations. Unlike simpler rules (e.g., Sturges' rule), this mathematical method is extremely robust to
heavy-tailed distributions and outliers.</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>basic_plots.py - Common Matplotlib Patterns</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(1, 2, figsize=(15, 5))
# 1. Scatter Plot (Color & Size mapping)
x = np.random.randn(100)
y = x + np.random.randn(100)*0.5
sizes = np.random.uniform(10, 200, 100)
colors = x
sc = axs[0].scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.7)
axs[0].set_title('Scatter Plot')
fig.colorbar(sc, ax=axs[0], label='Color Value')
# 2. Bar Chart (with Error Bars)
categories = ['Group A', 'Group B', 'Group C']
values = [10, 22, 15]
errors = [1.5, 3.0, 2.0]
axs[1].bar(categories, values, yerr=errors, capsize=5, color='coral', alpha=0.8)
axs[1].set_title('Bar Chart with Error Bars')
for i, v in enumerate(values):
axs[1].text(i, v + 0.5, str(v), ha='center')
plt.tight_layout()
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 7. SUBPLOTS ================== -->
<section id="subplots" class="topic-section">
<h2>π² Subplots & Multi-panel Layouts</h2>
<p>Combine multiple visualizations into a single figure for comprehensive analysis.</p>
<div class="form-group">
<button id="btn-grid-2x2" class="btn btn--primary">2Γ2 Grid</button>
<button id="btn-grid-uneven" class="btn btn--primary">Uneven Grid</button>
<button id="btn-gridspec" class="btn btn--primary">GridSpec</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-subplots" width="800" height="500"></canvas>
</div>
<div class="info-card">
<strong>Methods:</strong><br>
<code>fig, axes = plt.subplots(2, 2, figsize=(12, 10))</code><br>
<code>fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)</code><br>
<code>gs = fig.add_gridspec(3, 3); ax = fig.add_subplot(gs[0, :])</code>
</div>
<div class="callout callout--tip">β
Use plt.tight_layout() or fig.set_constrained_layout(True) to prevent
overlaps.</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>subplots.py - Complex Layouts with GridSpec</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(3, 3, figure=fig)
# 1. Main large plot (spans 2x2 grid)
ax_main = fig.add_subplot(gs[0:2, 0:2])
ax_main.set_title('Main View')
# 2. Side plots (Top right, Bottom right)
ax_side1 = fig.add_subplot(gs[0, 2])
ax_side2 = fig.add_subplot(gs[1, 2])
# 3. Bottom wide plot (spans 1x3 grid)
ax_bottom = fig.add_subplot(gs[2, :])
ax_bottom.set_title('Timeline View')
plt.tight_layout()
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 8. STYLING ================== -->
<section id="styling" class="topic-section">
<h2>π¨ Styling & Professional Themes</h2>
<p>Transform basic plots into publication-quality visualizations.</p>
<div class="form-group">
<button id="btn-style-default" class="btn btn--primary">Default</button>
<button id="btn-style-seaborn" class="btn btn--primary">Seaborn</button>
<button id="btn-style-ggplot" class="btn btn--primary">ggplot</button>
<button id="btn-style-dark" class="btn btn--primary">Dark Background</button>
<button id="btn-style-538" class="btn btn--primary">FiveThirtyEight</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-styling" width="800" height="400"></canvas>
</div>
<div class="info-card">
<strong>Available Styles:</strong><br>
<code>plt.style.available</code> β Lists all built-in styles<br>
<code>plt.style.use('seaborn-v0_8-whitegrid')</code><br>
<code>with plt.style.context('dark_background'):</code>
</div>
<h3>Color Palettes</h3>
<div class="info-card">
<strong>Perceptually Uniform:</strong> viridis, plasma, inferno, magma, cividis<br>
<strong>Sequential:</strong> Blues, Greens, Oranges (for magnitude)<br>
<strong>Diverging:</strong> coolwarm, RdBu (for +/- deviations)<br>
<strong>Categorical:</strong> tab10, Set2, Paired (discrete groups)
</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Perceptually Uniform Colors (CIELAB)</h3>
<p>Why do we use "viridis" instead of "rainbow" colormaps? A color map is a mathematical function mapping data
$f(x) \rightarrow (R, G, B)$. However, standard RGB math doesn't match human perception (Euclidean distance
in RGB $\neq$ perceived color distance).</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ \Delta E^* = \sqrt{(\Delta L^*)^2 + (\Delta a^*)^2 + (\Delta b^*)^2} $$
</div>
<p style="margin-bottom: 0;">Advanced colormaps like <em>viridis</em> are calculated in the <strong>CIELAB
($L^*a^*b^*$) color space</strong>. In this space, the mathematical distance formula $\Delta E^*$
perfectly matches how the retina and brain perceive brightness and hue differences, ensuring data is never
visually distorted.</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>styling.py - Applying Professional Aesthetics</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import matplotlib.pyplot as plt
import seaborn as sns
# 1. Apply a global Seaborn theme
sns.set_theme(style="whitegrid", palette="muted")
# 2. Customize fonts globally
plt.rcParams.update({
'font.family': 'sans-serif',
'font.sans-serif': ['Helvetica', 'Arial'],
'axes.titleweight': 'bold',
'axes.titlesize': 16,
'axes.labelsize': 12,
'lines.linewidth': 2
})
# 3. Plotting with the new theme
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([1, 2, 3], [4, 5, 2], label='Data')
ax.legend()
# 4. Remove top and right spines (cleaner look)
sns.despine(ax=ax)
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 9. SEABORN INTRO ================== -->
<section id="seaborn-intro" class="topic-section">
<h2>π Seaborn: Statistical Visualization</h2>
<p>Seaborn is a high-level library built on Matplotlib that makes statistical graphics beautiful and easy.</p>
<div class="info-card">
<strong>Why Seaborn?</strong>
<ul>
<li>Beautiful default styles and color palettes</li>
<li>Works seamlessly with Pandas DataFrames</li>
<li>Statistical estimation built-in (confidence intervals, regression)</li>
<li>Faceting for multi-panel figures</li>
<li>Functions organized by plot purpose</li>
</ul>
</div>
<h3>Seaborn Function Categories</h3>
<div class="info-card">
<strong>Figure-level:</strong> Create entire figures (displot, relplot, catplot)<br>
<strong>Axes-level:</strong> Draw on specific axes (histplot, scatterplot, boxplot)<br><br>
<strong>By Purpose:</strong><br>
β’ <strong>Distribution:</strong> histplot, kdeplot, ecdfplot, rugplot<br>
β’ <strong>Relationship:</strong> scatterplot, lineplot, regplot<br>
β’ <strong>Categorical:</strong> stripplot, swarmplot, boxplot, violinplot, barplot<br>
β’ <strong>Matrix:</strong> heatmap, clustermap
</div>
<div class="canvas-wrapper">
<canvas id="canvas-seaborn-intro" width="800" height="400"></canvas>
</div>
</section>
<!-- ====================== 10. DISTRIBUTIONS ================== -->
<section id="distributions" class="topic-section">
<h2>π Distribution Plots</h2>
<p>Visualize the distribution of a single variable or compare distributions across groups.</p>
<div class="form-group">
<button id="btn-histplot" class="btn btn--primary">Histogram</button>
<button id="btn-kdeplot" class="btn btn--primary">KDE Plot</button>
<button id="btn-ecdfplot" class="btn btn--primary">ECDF</button>
<button id="btn-rugplot" class="btn btn--primary">Rug Plot</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-distributions" width="800" height="400"></canvas>
</div>
<div class="info-card">
<strong>Histogram vs KDE:</strong><br>
β’ Histogram: Discrete bins, shows raw counts<br>
β’ KDE: Smooth curve, estimates probability density<br>
β’ Use both together: <code>sns.histplot(data, kde=True)</code>
</div>
<div class="callout callout--insight">π‘ ECDF (Empirical Cumulative Distribution Function) avoids binning issues
entirely.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Kernel Density Estimation (KDE)</h3>
<p>A KDE plot is not just a smoothed line; it's a mathematical sum of continuous probability distributions
(kernels) placed at every single data point $x_i$:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ \hat{f}_h(x) = \frac{1}{n h} \sum_{i=1}^{n} K\left(\frac{x - x_i}{h}\right) $$
</div>
<p style="margin-bottom: 0;">Here, $K$ is typically the Standard Normal Gaussian density function, and $h$ is
the bandwidth parameter. If $h$ is too small, the curve is jagged (overfit); if $h$ is too large, it hides
important statistical features (underfit).</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>distributions.py - Visualizing Distributions</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset("penguins")
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
# 1. Histogram + KDE overlay
sns.histplot(
data=penguins, x="flipper_length_mm", hue="species",
element="step", stat="density", common_norm=False,
ax=axes[0]
)
axes[0].set_title("Histogram with Step Fill")
# 2. KDE Plot with Rug Plot
sns.kdeplot(
data=penguins, x="body_mass_g", hue="species",
fill=True, common_norm=False, palette="crest",
alpha=0.5, linewidth=1.5, ax=axes[1]
)
sns.rugplot(
data=penguins, x="body_mass_g", hue="species",
height=0.05, ax=axes[1]
)
axes[1].set_title("KDE Density + Rug Plot")
sns.despine()
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 11. RELATIONSHIPS ================== -->
<section id="relationships" class="topic-section">
<h2>π Relationship Plots</h2>
<p>Explore relationships between two or more continuous variables.</p>
<div class="form-group">
<button id="btn-scatter-hue" class="btn btn--primary">Scatter + Hue</button>
<button id="btn-regplot" class="btn btn--primary">Regression Plot</button>
<button id="btn-residplot" class="btn btn--primary">Residual Plot</button>
<button id="btn-pairplot" class="btn btn--primary">Pair Plot</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-relationships" width="800" height="500"></canvas>
</div>
<div class="info-card">
<strong>Key Functions:</strong><br>
<code>sns.scatterplot(data=df, x='x', y='y', hue='category', size='magnitude')</code><br>
<code>sns.regplot(data=df, x='x', y='y', scatter_kws={'alpha':0.5})</code><br>
<code>sns.pairplot(df, hue='species', diag_kind='kde')</code>
</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Ordinary Least Squares (OLS)</h3>
<p>When you use <code>sns.regplot</code>, Seaborn calculates the line of best fit by minimizing the sum of the
squared residuals ($e_i^2$). The exact matrix algebra closed-form solution for the coefficients
$\hat{\boldsymbol{\beta}}$ is:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ \hat{\boldsymbol{\beta}} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y} $$
</div>
<p style="margin-bottom: 0;">The shaded region around the line represents the 95% confidence interval, meaning
if we resampled the data 100 times, the true regression line would fall inside this shaded band 95 times
(usually computed via bootstrapping).</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>relationships.py - Scatter and Regression</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
# 1. Advanced Scatter (4 dimensions: x, y, color, size)
plt.figure(figsize=(8, 6))
sns.scatterplot(
data=tips, x="total_bill", y="tip",
hue="time", size="size", sizes=(20, 200),
palette="deep", alpha=0.8
)
plt.title("4D Scatter Plot (Total Bill vs Tip)")
plt.show()
# 2. Regression Plot with Subplots (Using lmplot)
# lmplot is a figure-level function that creates multiple subplots automatically
sns.lmplot(
data=tips, x="total_bill", y="tip", col="time", hue="smoker",
height=5, aspect=1.2, scatter_kws={'alpha':0.5}
)
plt.show()
# 3. Pairplot (Explore all pairwise relationships)
sns.pairplot(
data=tips, hue="smoker",
diag_kind="kde", markers=["o", "s"]
)
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 12. CATEGORICAL ================== -->
<section id="categorical" class="topic-section">
<h2>π¦ Categorical Plots</h2>
<p>Visualize distributions and comparisons across categorical groups.</p>
<div class="form-group">
<button id="btn-stripplot" class="btn btn--primary">Strip Plot</button>
<button id="btn-swarmplot" class="btn btn--primary">Swarm Plot</button>
<button id="btn-boxplot" class="btn btn--primary">Box Plot</button>
<button id="btn-violinplot" class="btn btn--primary">Violin Plot</button>
<button id="btn-barplot" class="btn btn--primary">Bar Plot</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-categorical" width="800" height="400"></canvas>
</div>
<div class="info-card">
<strong>When to Use:</strong><br>
β’ <strong>Strip/Swarm:</strong> Show all data points (small datasets)<br>
β’ <strong>Box:</strong> Summary statistics (median, quartiles, outliers)<br>
β’ <strong>Violin:</strong> Full distribution shape + summary<br>
β’ <strong>Bar:</strong> Mean/count with error bars
</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: The IQR Outlier Rule</h3>
<p>Box plots identify "outliers" (the individual dots beyond the whiskers) purely mathematically, not
visually. They use John Tukey's Interquartile Range (IQR) method:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; color: #e4e6eb;">
$$ \text{IQR} = Q_3 - Q_1 $$
$$ \text{Lower Fence} = Q_1 - 1.5 \times \text{IQR} $$
$$ \text{Upper Fence} = Q_3 + 1.5 \times \text{IQR} $$
</div>
<p style="margin-bottom: 0;">Any point strictly outside $[Lower, Upper]$ is plotted as an outlier. <strong>Fun
Fact:</strong> In a perfectly normal Gaussian distribution $\mathcal{N}(\mu, \sigma^2)$, exactly 0.70% of
the data will be incorrectly flagged as outliers by this static math rule!</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>categorical.py - Categories and Factor Variables</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
# 1. Violin Plot (Distribution density across categories)
sns.violinplot(
data=tips, x="day", y="total_bill", hue="sex",
split=True, inner="quart", palette="muted",
ax=axes[0]
)
axes[0].set_title("Violin Plot (Split by Sex)")
# 2. Boxplot + Swarmplot Overlay
# Good for showing summary stats PLUS underlying data points
sns.boxplot(
data=tips, x="day", y="total_bill", color="white",
width=.5, showfliers=False, ax=axes[1] # hide boxplot outliers to avoid overlap
)
sns.swarmplot(
data=tips, x="day", y="total_bill", hue="time",
size=6, alpha=0.7, ax=axes[1]
)
axes[1].set_title("Boxplot + Swarmplot Overlay")
plt.tight_layout()
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 13. HEATMAPS ================== -->
<section id="heatmaps" class="topic-section">
<h2>π₯ Heatmaps & Correlation Matrices</h2>
<p>Visualize matrices of values using color intensity. Essential for EDA correlation analysis.</p>
<div class="form-group">
<button id="btn-heatmap-basic" class="btn btn--primary">Basic Heatmap</button>
<button id="btn-corr-matrix" class="btn btn--primary">Correlation Matrix</button>
<button id="btn-clustermap" class="btn btn--primary">Clustermap</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-heatmaps" width="800" height="500"></canvas>
</div>
<div class="info-card">
<strong>Best Practices:</strong><br>
β’ Always annotate with values: <code>annot=True</code><br>
β’ Use diverging colormap for correlation: <code>cmap='coolwarm', center=0</code><br>
β’ Mask upper/lower triangle: <code>mask=np.triu(np.ones_like(corr))</code><br>
β’ Square cells: <code>square=True</code>
</div>
<div class="callout callout--insight">π‘ Clustermap automatically clusters similar rows/columns together.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Correlation Coefficients</h3>
<p>Correlation heatmaps display the strength of linear relationships between variables, typically mapping the
<strong>Pearson Correlation Coefficient ($r$)</strong> to a discrete color gradient hexbin:
</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ r = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2 \sum (y_i - \bar{y})^2}} $$
</div>
<p style="margin-bottom: 0;">For non-linear but monotonic relationships, you should switch pandas to use
<strong>Spearman's Rank Correlation ($\rho$)</strong>, which mathematically converts raw values to ranks
$R(x_i)$ before applying the same formula. Both map perfectly bounds of $[-1, 1]$.
</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>heatmaps.py - Correlation Matrix</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Load data and calculate correlation matrix
penguins = sns.load_dataset("penguins")
# Select only numerical columns for correlation
numerical_df = penguins.select_dtypes(include=[np.number])
corr = numerical_df.corr()
# Create a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))
plt.figure(figsize=(8, 6))
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(
corr,
mask=mask,
cmap='coolwarm',
vmax=1, vmin=-1,
center=0,
square=True,
linewidths=.5,
annot=True,
fmt=".2f",
cbar_kws={"shrink": .8}
)
plt.title("Penguin Feature Correlation")
plt.tight_layout()
plt.show()</code></pre>
</div>
</section>
<!-- ====================== 14. PLOTLY ================== -->
<section id="plotly" class="topic-section">
<h2>π Plotly Express: Interactive Visualization</h2>
<p>Plotly creates interactive, web-based visualizations with zoom, pan, hover tooltips, and more.</p>
<div class="info-card">
<strong>Why Plotly?</strong>
<ul>
<li>Interactive out of the box (zoom, pan, select)</li>
<li>Hover tooltips with data details</li>
<li>Export as HTML, PNG, or embed in dashboards</li>
<li>Works in Jupyter, Streamlit, Dash</li>
<li>plotly.express is the high-level API (like Seaborn for Matplotlib)</li>
</ul>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-plotly" width="800" height="400"></canvas>
</div>
<div class="info-card">
<strong>Common Functions:</strong><br>
<code>px.scatter(df, x='x', y='y', color='category', size='value', hover_data=['name'])</code><br>
<code>px.line(df, x='date', y='price', color='stock')</code><br>
<code>px.bar(df, x='category', y='count', color='group', barmode='group')</code><br>
<code>px.histogram(df, x='value', nbins=50, marginal='box')</code>
</div>
</section>
<!-- ====================== 15. ANIMATIONS ================== -->
<section id="animations" class="topic-section">
<h2>π¬ Animated Visualizations</h2>
<p>Add time dimension to your visualizations with animations.</p>
<div class="form-group">
<button id="btn-animate" class="btn btn--primary">βΆ Play Animation</button>
<button id="btn-stop" class="btn btn--primary">βΉ Stop</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-animation" width="800" height="400"></canvas>
</div>
<div class="info-card">
<strong>Plotly Animation:</strong><br>
<code>px.scatter(df, x='gdp', y='life_exp', animation_frame='year', animation_group='country', size='pop', color='continent')</code><br><br>
<strong>Matplotlib Animation:</strong><br>
<code>from matplotlib.animation import FuncAnimation</code><br>
<code>ani = FuncAnimation(fig, update_func, frames=100, interval=50)</code>
</div>
<div class="callout callout--tip">β
Hans Rosling's Gapminder is the classic example of animated scatter plots!
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>animation_example.py - Gapminder Scatter</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import plotly.express as px
df = px.data.gapminder()
# Plotly makes animations incredibly easy with two arguments:
# 'animation_frame' (the time dimension) and 'animation_group' (the entity)
fig = px.scatter(
df,
x="gdpPercap", y="lifeExp",
animation_frame="year", animation_group="country",
size="pop", color="continent",
hover_name="country",
log_x=True, size_max=55,
range_x=[100,100000], range_y=[25,90],
title="Global Development 1952 - 2007"
)
fig.show()</code></pre>
</div>
</section>
<!-- ====================== 16. DASHBOARDS ================== -->
<section id="dashboards" class="topic-section">
<h2>π± Interactive Dashboards with Streamlit</h2>
<p>Build interactive web apps for data exploration without web development experience.</p>
<div class="info-card">
<strong>Streamlit Basics:</strong><br>
<code>streamlit run app.py</code><br><br>
<code>import streamlit as st</code><br>
<code>st.title("My Dashboard")</code><br>
<code>st.slider("Select value", 0, 100, 50)</code><br>
<code>st.selectbox("Choose", ["A", "B", "C"])</code><br>
<code>st.plotly_chart(fig)</code>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-dashboard" width="800" height="400"></canvas>
</div>
<div class="callout callout--insight">π‘ Streamlit auto-reruns when input changes - no callbacks needed!</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>app.py - Minimal Streamlit Dashboard</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import streamlit as st
import pandas as pd
import plotly.express as px
# 1. Page Configuration
st.set_page_config(page_title="Sales Dashboard", layout="wide")
st.title("Interactive Sales Dashboard π")
# 2. Sidebar Filters
st.sidebar.header("Filters")
category = st.sidebar.selectbox("Select Category", ["Electronics", "Clothing", "Home"])
min_sales = st.sidebar.slider("Minimum Sales", 0, 1000, 200)
# Mock Data Generation
df = pd.DataFrame({
'Date': pd.date_range(start='2023-01-01', periods=30),
'Sales': [x * 10 for x in range(30)],
'Category': [category] * 30
})
filtered_df = df[df['Sales'] >= min_sales]
# 3. Layout with Columns
col1, col2 = st.columns(2)
# KPI Metric
col1.metric("Total Filtered Sales", f"${filtered_df['Sales'].sum()}")
# 4. Insert Plotly Chart
fig = px.line(filtered_df, x='Date', y='Sales', title=f"{category} Sales Trend")
col2.plotly_chart(fig, use_container_width=True)</code></pre>
</div>
</section>
<!-- ====================== 17. GEOSPATIAL ================== -->
<section id="geospatial" class="topic-section">
<h2>πΊοΈ Geospatial Visualization</h2>
<p>Visualize geographic data with maps, choropleth, and point plots.</p>
<div class="form-group">
<button id="btn-choropleth" class="btn btn--primary">Choropleth Map</button>
<button id="btn-scatter-geo" class="btn btn--primary">Scatter on Map</button>
<button id="btn-heatmap-geo" class="btn btn--primary">Density Map</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-geo" width="800" height="500"></canvas>
</div>
<div class="info-card">
<strong>Libraries:</strong><br>
β’ <strong>Plotly:</strong> <code>px.choropleth(df, locations='country', color='value')</code><br>
β’ <strong>Folium:</strong> Interactive Leaflet maps<br>
β’ <strong>Geopandas + Matplotlib:</strong> Static maps with shapefiles<br>
β’ <strong>Kepler.gl:</strong> Large-scale geospatial visualization
</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: Geospatial Math</h3>
<p>Visualizing data on a map requires mathematically converting a 3D spherical Earth into 2D screen pixels.
The <strong>Web Mercator Projection</strong> (used by Google Maps and Plotly) achieves this by preserving
angles (conformal) but heavily distorting sizes near the poles:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; font-size: 1.1em; color: #e4e6eb;">
$$ x = R \cdot \lambda \qquad y = R \ln\left[\tan\left(\frac{\pi}{4} + \frac{\varphi}{2}\right)\right] $$
</div>
<p style="margin-bottom: 0;">Furthermore, when calculating distances between two GPS coordinates (e.g., to
color a density heatmap), you cannot use straight Euclidean distance $d = \sqrt{x^2+y^2}$. Advanced
libraries compute the <strong>Haversine formula</strong> to find the true great-circle distance over the
sphere.</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>geospatial.py - Plotly Choropleth</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import plotly.express as px
# Plotly includes built-in geospatial data
df = px.data.gapminder().query("year==2007")
# Create a choropleth map
# 'locations' takes ISO-3 country codes by default
fig = px.choropleth(
df,
locations="iso_alpha", # Geopolitical boundaries
color="lifeExp", # Data to map to color
hover_name="country", # Tooltip label
color_continuous_scale=px.colors.sequential.Plasma,
title="Global Life Expectancy (2007)"
)
# Customize the map projection type
fig.update_geos(
projection_type="orthographic", # "natural earth", "mercator", etc.
showcoastlines=True,
coastlinecolor="DarkBlue"
)
fig.show()</code></pre>
</div>
</section>
<!-- ====================== 18. 3D PLOTS ================== -->
<section id="3d-plots" class="topic-section">
<h2>π² 3D Visualization</h2>
<p>Visualize three-dimensional relationships with surface plots, scatter plots, and more.</p>
<div class="form-group">
<button id="btn-3d-scatter" class="btn btn--primary">3D Scatter</button>
<button id="btn-3d-surface" class="btn btn--primary">Surface Plot</button>
<button id="btn-3d-wireframe" class="btn btn--primary">Wireframe</button>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-3d" width="800" height="500"></canvas>
</div>
<div class="callout callout--mistake">β οΈ 3D plots can obscure data. Often, multiple 2D views are more effective.
</div>
<div class="callout callout--tip">β
Use Plotly for interactive 3D (rotate, zoom) instead of static Matplotlib
3D.</div>
<div class="info-card" style="margin-top: 20px; border-left-color: #9900ff;">
<h3 style="margin-top: 0; color: #9900ff;">π§ Under the Hood: 3D Perspective Projection Matrix</h3>
<p>To render 3D data $(x, y, z)$ on a 2D screen browser, libraries like Plotly.js apply a <strong>Perspective
Projection Matrix</strong>. This creates the optical illusion of depth by scaling $x$ and $y$ inversely
with distance $z$:</p>
<div
style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 8px; text-align: center; margin: 15px 0; overflow-x: auto; color: #e4e6eb;">
$$ \begin{bmatrix} x' \\ y' \\ z' \\ w \end{bmatrix} = \begin{bmatrix} \frac{1}{\text{aspect} \cdot
\tan(\frac{fov}{2})} & 0 & 0 & 0 \\ 0 & \frac{1}{\tan(\frac{fov}{2})} & 0 & 0 \\ 0 & 0 & \frac{f+n}{f-n} &
\frac{-2fn}{f-n} \\ 0 & 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} $$
</div>
<p style="margin-bottom: 0;">Once multiplied out, the final screen coordinates are $(x'/w, y'/w)$. When you
rapidly drag to rotate a 3D Plotly graph, your browser's WebGL engine is recalculating this exact matrix
millions of times per second to update the viewpoint mapping in real-time!</p>
</div>
<div class="code-block" style="margin-top: 20px;">
<div class="code-header">
<span>3d_plots.py - Interactive 3D Scatter</span>
<button class="copy-btn" onclick="copyCode(this)">Copy</button>
</div>
<pre><code>import plotly.express as px
# Load Iris dataset
df = px.data.iris()
# Create interactive 3D scatter plot
fig = px.scatter_3d(
df,
x='sepal_length',
y='sepal_width',
z='petal_width',
color='species',
size='petal_length',
size_max=18,
symbol='species',
opacity=0.7,
title="Iris 3D Feature Space"
)
# Tight layout for 3D plot
fig.update_layout(margin=dict(l=0, r=0, b=0, t=40))
fig.show()</code></pre>
</div>
</section>
<!-- ====================== 19. STORYTELLING ================== -->
<section id="storytelling" class="topic-section">
<h2>π Data Storytelling</h2>
<p>Transform visualizations into compelling narratives that drive action.</p>
<div class="info-card">
<strong>The Data Storytelling Framework:</strong>
<ol>
<li><strong>Context:</strong> Why does this matter? Who is the audience?</li>
<li><strong>Data:</strong> What insights did you discover?</li>
<li><strong>Narrative:</strong> What's the storyline (beginning, middle, end)?</li>
<li><strong>Visual:</strong> Which chart best supports the story?</li>
<li><strong>Call to Action:</strong> What should the audience do?</li>
</ol>
</div>
<div class="canvas-wrapper">
<canvas id="canvas-storytelling" width="800" height="400"></canvas>
</div>
<h3>Design Principles</h3>
<div class="info-card">
<strong>Remove Clutter:</strong> Eliminate chartjunk, gridlines, borders<br>
<strong>Focus Attention:</strong> Use color strategically (grey + accent)<br>
<strong>Think Like a Designer:</strong> Alignment, white space, hierarchy<br>
<strong>Tell a Story:</strong> Title = conclusion, not description<br>
<strong>Bad:</strong> "Sales by Region"<br>
<strong>Good:</strong> "West Region Sales Dropped 23% in Q4"
</div>
<div class="callout callout--insight">π‘ "If you can't explain it simply, you don't understand it well enough."
β Einstein</div>
<div class="callout callout--tip">β
Read "Storytelling with Data" by Cole Nussbaumer Knaflic</div>
</section>
<!-- Footer -->
<footer
style="text-align: center; padding: 40px 20px; border-top: 1px solid var(--border-color); margin-top: 60px;">
<p style="color: var(--text-secondary);">π Data Visualization Masterclass | Part of the Data Science & AI
Curriculum</p>
<a href="../index.html" class="btn btn--primary" style="margin-top: 16px;">β Back to Home</a>
</footer>
</main>
</div>
<script src="app.js"></script>
</body>
</html> |