{
  "title": "Plot Types Cheat Sheet",
  "intro": "40 plot types in 7 groups. Each one has when to use it, when not to use it, sample data, runnable code, and the chart that code produces.",
  "setupCode": "import numpy as np, pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nrng = np.random.default_rng(0)   # makes the sample data repeatable",
  "sections": [
    {
      "id": "distribution",
      "title": "1. Distribution: how is one variable spread? (6)",
      "kind": "plots",
      "category": "Distribution",
      "expectedCount": 6,
      "plots": [
        {
          "id": "histogram",
          "sourceId": "p1",
          "name": "Histogram",
          "category": "Distribution",
          "useWhen": "you want to see the shape, skew or outliers of one numeric column.",
          "dontUse": "with very few data points, or without trying different bin sizes.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.hist · pandas df.hist() · plotly px.histogram",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>age</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>36.0</td>\n    </tr>\n    <tr>\n      <td>34.0</td>\n    </tr>\n    <tr>\n      <td>41.0</td>\n    </tr>\n    <tr>\n      <td>36.0</td>\n    </tr>\n    <tr>\n      <td>30.0</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"age\": rng.normal(35, 10, 500).round()})\nsns.histplot(sample, x=\"age\", bins=30)",
          "image": "/images/plots/histogram.png",
          "imageAlt": "Histogram example chart",
          "imageWidth": 552,
          "imageHeight": 359
        },
        {
          "id": "kde-density-plot",
          "sourceId": "p2",
          "name": "KDE (density) plot",
          "category": "Distribution",
          "useWhen": "you want a smooth curve, or to compare two or three groups on one chart.",
          "dontUse": "for data with hard limits (like age ≥ 0), because the curve spills past them.",
          "library": "seaborn",
          "alsoIn": "pandas df.plot.kde() · scipy gaussian_kde · plotly ff.create_distplot",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>income</th>\n      <th>gender</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>41.0</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>38.9</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>45.1</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>40.8</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>35.7</td>\n      <td>F</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\n    \"income\": np.r_[rng.normal(40, 8, 300), rng.normal(55, 10, 300)].round(1),\n    \"gender\": [\"F\"]*300 + [\"M\"]*300})\nsns.kdeplot(sample, x=\"income\", hue=\"gender\", fill=True)",
          "image": "/images/plots/kde-density-plot.png",
          "imageAlt": "KDE (density) plot example chart",
          "imageWidth": 574,
          "imageHeight": 359
        },
        {
          "id": "box-plot",
          "sourceId": "p3",
          "name": "Box plot",
          "category": "Distribution",
          "useWhen": "you want the median, spread and outliers for several groups.",
          "dontUse": "if the data has two peaks, because the box hides them.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.boxplot · pandas df.boxplot() · plotly px.box",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>A</td>\n      <td>61.0</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>58.9</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>65.1</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>60.8</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>55.7</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\n    \"class\": np.repeat([\"A\", \"B\", \"C\"], 100),\n    \"score\": np.r_[rng.normal(60,8,100), rng.normal(70,12,100), rng.normal(65,5,100)].round(1)})\nsns.boxplot(sample, x=\"class\", y=\"score\")",
          "image": "/images/plots/box-plot.png",
          "imageAlt": "Box plot example chart",
          "imageWidth": 552,
          "imageHeight": 359
        },
        {
          "id": "violin-plot",
          "sourceId": "p4",
          "name": "Violin plot",
          "category": "Distribution",
          "useWhen": "you want a box plot that also shows the full shape.",
          "dontUse": "for small groups (under ~30 rows) or audiences who don't know it.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.violinplot · plotly px.violin",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>A</td>\n      <td>61.0</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>58.9</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>65.1</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>60.8</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>55.7</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\n    \"class\": np.repeat([\"A\", \"B\", \"C\"], 150),\n    \"score\": np.r_[rng.normal(60,8,150),\n                   np.r_[rng.normal(55,5,75), rng.normal(80,5,75)],   # two peaks\n                   rng.normal(65,5,150)].round(1)})\nsns.violinplot(sample, x=\"class\", y=\"score\", inner=\"quart\")",
          "image": "/images/plots/violin-plot.png",
          "imageAlt": "Violin plot example chart",
          "imageWidth": 561,
          "imageHeight": 359
        },
        {
          "id": "strip-swarm-plot",
          "sourceId": "p5",
          "name": "Strip / Swarm plot",
          "category": "Distribution",
          "useWhen": "you have a small dataset and want to show every point.",
          "dontUse": "for thousands of points, because they pile on top of each other.",
          "library": "seaborn",
          "alsoIn": "plotly px.strip",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>class</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>A</td>\n      <td>61.0</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>58.9</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>65.1</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>60.8</td>\n    </tr>\n    <tr>\n      <td>A</td>\n      <td>55.7</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\n    \"class\": np.repeat([\"A\", \"B\", \"C\"], 30),\n    \"score\": np.r_[rng.normal(60,8,30), rng.normal(70,8,30), rng.normal(65,8,30)].round(1)})\nsns.swarmplot(sample, x=\"class\", y=\"score\")",
          "image": "/images/plots/strip-swarm-plot.png",
          "imageAlt": "Strip / Swarm plot example chart",
          "imageWidth": 552,
          "imageHeight": 359
        },
        {
          "id": "q-q-plot",
          "sourceId": "p6",
          "name": "Q-Q plot",
          "category": "Distribution",
          "useWhen": "you need to check whether data or residuals follow a normal distribution.",
          "dontUse": "with huge data and panic over tiny wiggles.",
          "library": "scipy + matplotlib",
          "alsoIn": "statsmodels sm.qqplot · pingouin qqplot",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>x</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1.36</td>\n    </tr>\n    <tr>\n      <td>2.04</td>\n    </tr>\n    <tr>\n      <td>0.04</td>\n    </tr>\n    <tr>\n      <td>0.00</td>\n    </tr>\n    <tr>\n      <td>1.10</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from scipy import stats\nsample = pd.DataFrame({\"x\": rng.exponential(2, 200).round(2)})   # skewed data\nstats.probplot(sample[\"x\"], dist=\"norm\", plot=plt)\nplt.title(\"Q-Q plot (points bend away = not normal)\")",
          "image": "/images/plots/q-q-plot.png",
          "imageAlt": "Q-Q plot example chart",
          "imageWidth": 555,
          "imageHeight": 382
        }
      ]
    },
    {
      "id": "relationship",
      "title": "2. Relationship: do two or more variables move together? (6)",
      "kind": "plots",
      "category": "Relationship",
      "expectedCount": 6,
      "plots": [
        {
          "id": "scatter-plot",
          "sourceId": "p7",
          "name": "Scatter plot",
          "category": "Relationship",
          "useWhen": "you want to see how two numeric variables relate, or spot clusters and outliers.",
          "dontUse": "with 10,000+ points without transparency (points hide each other).",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.scatter · pandas df.plot.scatter · plotly px.scatter",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>height</th>\n      <th>weight</th>\n      <th>sex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>171.1</td>\n      <td>59.4</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>168.8</td>\n      <td>57.6</td>\n      <td>F</td>\n    </tr>\n    <tr>\n      <td>175.8</td>\n      <td>57.0</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <td>170.9</td>\n      <td>69.0</td>\n      <td>M</td>\n    </tr>\n    <tr>\n      <td>165.2</td>\n      <td>64.3</td>\n      <td>M</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "h = rng.normal(170, 9, 200)\nsample = pd.DataFrame({\"height\": h.round(1),\n    \"weight\": (h*0.9 - 90 + rng.normal(0, 7, 200)).round(1),\n    \"sex\": rng.choice([\"F\", \"M\"], 200)})\nsns.scatterplot(sample, x=\"height\", y=\"weight\", hue=\"sex\", alpha=.8)",
          "image": "/images/plots/scatter-plot.png",
          "imageAlt": "Scatter plot example chart",
          "imageWidth": 561,
          "imageHeight": 364
        },
        {
          "id": "bubble-chart",
          "sourceId": "p8",
          "name": "Bubble chart",
          "category": "Relationship",
          "useWhen": "you want a scatter plot with a third variable shown as bubble size.",
          "dontUse": "when exact values matter, because bubble sizes are hard to judge.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.scatter(s=...) · plotly px.scatter(size=...)",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>country</th>\n      <th>gdp</th>\n      <th>life</th>\n      <th>population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>A</td>\n      <td>38.6</td>\n      <td>79.6</td>\n      <td>13.0</td>\n    </tr>\n    <tr>\n      <td>B</td>\n      <td>16.9</td>\n      <td>60.1</td>\n      <td>42.0</td>\n    </tr>\n    <tr>\n      <td>C</td>\n      <td>3.4</td>\n      <td>80.6</td>\n      <td>203.0</td>\n    </tr>\n    <tr>\n      <td>D</td>\n      <td>2.0</td>\n      <td>60.8</td>\n      <td>196.0</td>\n    </tr>\n    <tr>\n      <td>E</td>\n      <td>49.0</td>\n      <td>77.5</td>\n      <td>187.0</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"country\": list(\"ABCDEFGHIJ\"),\n    \"gdp\": rng.uniform(1, 60, 10).round(1),\n    \"life\": rng.uniform(60, 84, 10).round(1),\n    \"population\": rng.uniform(5, 300, 10).round()})\nsns.scatterplot(sample, x=\"gdp\", y=\"life\", size=\"population\", sizes=(40, 800), alpha=.6, legend=False)",
          "image": "/images/plots/bubble-chart.png",
          "imageAlt": "Bubble chart example chart",
          "imageWidth": 552,
          "imageHeight": 359
        },
        {
          "id": "hexbin-plot",
          "sourceId": "p9",
          "name": "Hexbin plot",
          "category": "Relationship",
          "useWhen": "a scatter plot has too many points and you want to see where they are dense.",
          "dontUse": "for small datasets.",
          "library": "matplotlib",
          "alsoIn": "pandas df.plot.hexbin · seaborn jointplot(kind='hex') · plotly px.density_heatmap",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>x</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.13</td>\n      <td>0.38</td>\n    </tr>\n    <tr>\n      <td>-0.13</td>\n      <td>0.77</td>\n    </tr>\n    <tr>\n      <td>0.64</td>\n      <td>0.75</td>\n    </tr>\n    <tr>\n      <td>0.10</td>\n      <td>0.57</td>\n    </tr>\n    <tr>\n      <td>-0.54</td>\n      <td>-0.34</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "x = rng.normal(0, 1, 20000)\nsample = pd.DataFrame({\"x\": x.round(2), \"y\": (x + rng.normal(0, .8, 20000)).round(2)})\nplt.hexbin(sample.x, sample.y, gridsize=35, cmap=\"Blues\")\nplt.colorbar(label=\"count\")",
          "image": "/images/plots/hexbin-plot.png",
          "imageAlt": "Hexbin plot example chart",
          "imageWidth": 538,
          "imageHeight": 338
        },
        {
          "id": "regression-plot",
          "sourceId": "p10",
          "name": "Regression plot",
          "category": "Relationship",
          "useWhen": "you want a scatter plot with a trend line.",
          "dontUse": "to claim one thing causes another.",
          "library": "seaborn",
          "alsoIn": "seaborn lmplot · plotly px.scatter(trendline='ols')",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>hours</th>\n      <th>marks</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>6.4</td>\n      <td>72.0</td>\n    </tr>\n    <tr>\n      <td>2.7</td>\n      <td>66.0</td>\n    </tr>\n    <tr>\n      <td>0.4</td>\n      <td>43.0</td>\n    </tr>\n    <tr>\n      <td>0.2</td>\n      <td>37.0</td>\n    </tr>\n    <tr>\n      <td>8.1</td>\n      <td>78.0</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "hrs = rng.uniform(0, 10, 80)\nsample = pd.DataFrame({\"hours\": hrs.round(1), \"marks\": (40 + 5*hrs + rng.normal(0, 6, 80)).round()})\nsns.regplot(sample, x=\"hours\", y=\"marks\")",
          "image": "/images/plots/regression-plot.png",
          "imageAlt": "Regression plot example chart",
          "imageWidth": 552,
          "imageHeight": 359
        },
        {
          "id": "pair-plot",
          "sourceId": "p11",
          "name": "Pair plot",
          "category": "Relationship",
          "useWhen": "you're starting EDA and want every pair of columns at once.",
          "dontUse": "with more than ~10 columns, because it gets too crowded.",
          "library": "seaborn",
          "alsoIn": "pandas scatter_matrix · plotly px.scatter_matrix",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>sepal_length</th>\n      <th>sepal_width</th>\n      <th>petal_length</th>\n      <th>petal_width</th>\n      <th>species</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>5.1</td>\n      <td>3.5</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <td>4.9</td>\n      <td>3.0</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <td>4.7</td>\n      <td>3.2</td>\n      <td>1.3</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <td>4.6</td>\n      <td>3.1</td>\n      <td>1.5</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <td>5.0</td>\n      <td>3.6</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = sns.load_dataset(\"iris\")        # built-in sample dataset\nsns.pairplot(sample, hue=\"species\", vars=[\"sepal_length\", \"petal_length\", \"petal_width\"], height=1.6)",
          "image": "/images/plots/pair-plot.png",
          "imageAlt": "Pair plot example chart",
          "imageWidth": 644,
          "imageHeight": 521
        },
        {
          "id": "correlation-heatmap",
          "sourceId": "p12",
          "name": "Correlation heatmap",
          "category": "Relationship",
          "useWhen": "you want to find strongly related features before modeling.",
          "dontUse": "as your only check, because it misses non-linear relationships.",
          "library": "pandas + seaborn",
          "alsoIn": "matplotlib plt.imshow · plotly px.imshow",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>sepal_length</th>\n      <th>sepal_width</th>\n      <th>petal_length</th>\n      <th>petal_width</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>5.1</td>\n      <td>3.5</td>\n      <td>1.4</td>\n      <td>0.2</td>\n    </tr>\n    <tr>\n      <td>4.9</td>\n      <td>3.0</td>\n      <td>1.4</td>\n      <td>0.2</td>\n    </tr>\n    <tr>\n      <td>4.7</td>\n      <td>3.2</td>\n      <td>1.3</td>\n      <td>0.2</td>\n    </tr>\n    <tr>\n      <td>4.6</td>\n      <td>3.1</td>\n      <td>1.5</td>\n      <td>0.2</td>\n    </tr>\n    <tr>\n      <td>5.0</td>\n      <td>3.6</td>\n      <td>1.4</td>\n      <td>0.2</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = sns.load_dataset(\"iris\").drop(columns=\"species\")\nsns.heatmap(sample.corr(), annot=True, fmt=\".2f\", cmap=\"RdBu_r\", vmin=-1, vmax=1)",
          "image": "/images/plots/correlation-heatmap.png",
          "imageAlt": "Correlation heatmap example chart",
          "imageWidth": 600,
          "imageHeight": 344
        }
      ]
    },
    {
      "id": "comparison",
      "title": "3. Comparison: which is bigger or better? (5)",
      "kind": "plots",
      "category": "Comparison",
      "expectedCount": 5,
      "plots": [
        {
          "id": "bar-chart",
          "sourceId": "p13",
          "name": "Bar chart",
          "category": "Comparison",
          "useWhen": "you're comparing numbers across categories.",
          "dontUse": "with the y-axis not starting at 0, or to show distributions.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.bar · pandas df.plot.bar · plotly px.bar",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>model</th>\n      <th>accuracy</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>LogReg</td>\n      <td>0.81</td>\n    </tr>\n    <tr>\n      <td>Tree</td>\n      <td>0.78</td>\n    </tr>\n    <tr>\n      <td>Forest</td>\n      <td>0.86</td>\n    </tr>\n    <tr>\n      <td>XGBoost</td>\n      <td>0.88</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"model\": [\"LogReg\", \"Tree\", \"Forest\", \"XGBoost\"],\n                       \"accuracy\": [0.81, 0.78, 0.86, 0.88]})\nsns.barplot(sample, x=\"model\", y=\"accuracy\", color=\"#2a78d6\")",
          "image": "/images/plots/bar-chart.png",
          "imageAlt": "Bar chart example chart",
          "imageWidth": 556,
          "imageHeight": 359
        },
        {
          "id": "count-plot",
          "sourceId": "p14",
          "name": "Count plot",
          "category": "Comparison",
          "useWhen": "you want to count rows per category, e.g. check class balance.",
          "dontUse": "with hundreds of categories.",
          "library": "seaborn",
          "alsoIn": "pandas value_counts().plot.bar() · plotly px.histogram",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>not fraud</td>\n    </tr>\n    <tr>\n      <td>not fraud</td>\n    </tr>\n    <tr>\n      <td>not fraud</td>\n    </tr>\n    <tr>\n      <td>not fraud</td>\n    </tr>\n    <tr>\n      <td>not fraud</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"label\": rng.choice([\"not fraud\", \"fraud\"], 1000, p=[.93, .07])})\nsns.countplot(sample, x=\"label\", color=\"#2a78d6\")",
          "image": "/images/plots/count-plot.png",
          "imageAlt": "Count plot example chart",
          "imageWidth": 561,
          "imageHeight": 359
        },
        {
          "id": "grouped-bar-chart",
          "sourceId": "p15",
          "name": "Grouped bar chart",
          "category": "Comparison",
          "useWhen": "you're comparing sub-groups inside each category.",
          "dontUse": "with more than ~4 sub-groups.",
          "library": "seaborn",
          "alsoIn": "pandas df.plot.bar · plotly px.bar(barmode='group')",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>metric</th>\n      <th>model</th>\n      <th>score</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>precision</td>\n      <td>Forest</td>\n      <td>0.84</td>\n    </tr>\n    <tr>\n      <td>recall</td>\n      <td>Forest</td>\n      <td>0.79</td>\n    </tr>\n    <tr>\n      <td>f1</td>\n      <td>Forest</td>\n      <td>0.81</td>\n    </tr>\n    <tr>\n      <td>precision</td>\n      <td>XGBoost</td>\n      <td>0.87</td>\n    </tr>\n    <tr>\n      <td>recall</td>\n      <td>XGBoost</td>\n      <td>0.83</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\n    \"metric\": [\"precision\", \"recall\", \"f1\"] * 2,\n    \"model\":  [\"Forest\"]*3 + [\"XGBoost\"]*3,\n    \"score\":  [0.84, 0.79, 0.81, 0.87, 0.83, 0.85]})\nsns.barplot(sample, x=\"metric\", y=\"score\", hue=\"model\")",
          "image": "/images/plots/grouped-bar-chart.png",
          "imageAlt": "Grouped bar chart example chart",
          "imageWidth": 556,
          "imageHeight": 359
        },
        {
          "id": "lollipop-dot-plot",
          "sourceId": "p16",
          "name": "Lollipop / Dot plot",
          "category": "Comparison",
          "useWhen": "you have many categories or a ranked list and want a cleaner bar chart.",
          "dontUse": "when the audience expects a plain bar chart.",
          "library": "matplotlib",
          "alsoIn": "matplotlib plt.stem · seaborn pointplot · plotly px.scatter",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>feature</th>\n      <th>importance</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>region</td>\n      <td>0.06</td>\n    </tr>\n    <tr>\n      <td>visits</td>\n      <td>0.10</td>\n    </tr>\n    <tr>\n      <td>balance</td>\n      <td>0.14</td>\n    </tr>\n    <tr>\n      <td>tenure</td>\n      <td>0.17</td>\n    </tr>\n    <tr>\n      <td>age</td>\n      <td>0.22</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"feature\": [\"income\", \"age\", \"tenure\", \"balance\", \"visits\", \"region\"],\n                       \"importance\": [0.31, 0.22, 0.17, 0.14, 0.10, 0.06]}).iloc[::-1]\nplt.hlines(sample.feature, 0, sample.importance, color=\"#9ec5f4\", lw=2)\nplt.plot(sample.importance, sample.feature, \"o\", color=\"#2a78d6\", ms=8)\nplt.xlabel(\"importance\")",
          "image": "/images/plots/lollipop-dot-plot.png",
          "imageAlt": "Lollipop / Dot plot example chart",
          "imageWidth": 568,
          "imageHeight": 359
        },
        {
          "id": "radar-chart",
          "sourceId": "p17",
          "name": "Radar chart",
          "category": "Comparison",
          "useWhen": "you're comparing 2–3 items across 4–8 metrics.",
          "dontUse": "for precise comparison or many items, because the shape depends on axis order.",
          "library": "matplotlib (polar axes)",
          "alsoIn": "plotly px.line_polar",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>metric</th>\n      <th>Model A</th>\n      <th>Model B</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>accuracy</td>\n      <td>0.8</td>\n      <td>0.9</td>\n    </tr>\n    <tr>\n      <td>precision</td>\n      <td>0.7</td>\n      <td>0.8</td>\n    </tr>\n    <tr>\n      <td>recall</td>\n      <td>0.9</td>\n      <td>0.6</td>\n    </tr>\n    <tr>\n      <td>speed</td>\n      <td>0.6</td>\n      <td>0.9</td>\n    </tr>\n    <tr>\n      <td>memory</td>\n      <td>0.8</td>\n      <td>0.5</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"metric\": [\"accuracy\", \"precision\", \"recall\", \"speed\", \"memory\"],\n                       \"Model A\": [.8, .7, .9, .6, .8], \"Model B\": [.9, .8, .6, .9, .5]})\nang = np.linspace(0, 2*np.pi, len(sample), endpoint=False).tolist(); ang += ang[:1]\nax = plt.subplot(polar=True)\nfor m in [\"Model A\", \"Model B\"]:\n    v = sample[m].tolist(); v += v[:1]\n    ax.plot(ang, v, label=m); ax.fill(ang, v, alpha=.15)\nax.set_xticks(ang[:-1], sample.metric); ax.legend(loc=\"upper right\", bbox_to_anchor=(1.35, 1.1))",
          "image": "/images/plots/radar-chart.png",
          "imageAlt": "Radar chart example chart",
          "imageWidth": 417,
          "imageHeight": 357
        }
      ]
    },
    {
      "id": "composition",
      "title": "4. Composition: what makes up the whole? (5)",
      "kind": "plots",
      "category": "Composition",
      "expectedCount": 5,
      "plots": [
        {
          "id": "pie-donut-chart",
          "sourceId": "p18",
          "name": "Pie / Donut chart",
          "category": "Composition",
          "useWhen": "you have 2–5 parts that add up to 100%.",
          "dontUse": "with more than 5 slices, or to compare pies. Use a bar chart instead.",
          "library": "matplotlib",
          "alsoIn": "pandas df.plot.pie · plotly px.pie",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>device</th>\n      <th>share</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>Mobile</td>\n      <td>62</td>\n    </tr>\n    <tr>\n      <td>Desktop</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <td>Tablet</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"device\": [\"Mobile\", \"Desktop\", \"Tablet\"], \"share\": [62, 30, 8]})\nplt.pie(sample.share, labels=sample.device, autopct=\"%1.0f%%\", startangle=90,\n        wedgeprops=dict(width=.45, edgecolor=\"white\"))",
          "image": "/images/plots/pie-donut-chart.png",
          "imageAlt": "Pie / Donut chart example chart",
          "imageWidth": 367,
          "imageHeight": 309
        },
        {
          "id": "stacked-bar-chart",
          "sourceId": "p19",
          "name": "Stacked bar chart",
          "category": "Composition",
          "useWhen": "you want to show how parts differ across categories.",
          "dontUse": "when readers need to compare the middle segments exactly.",
          "library": "pandas",
          "alsoIn": "matplotlib plt.bar(bottom=...) · seaborn histplot(multiple='stack') · plotly px.bar",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>cat</th>\n      <th>dog</th>\n      <th>bird</th>\n    </tr>\n    <tr>\n      <th>split</th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>train</th>\n      <td>420</td>\n      <td>380</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>valid</th>\n      <td>90</td>\n      <td>80</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>test</th>\n      <td>85</td>\n      <td>95</td>\n      <td>20</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"split\": [\"train\", \"valid\", \"test\"],\n                       \"cat\": [420, 90, 85], \"dog\": [380, 80, 95], \"bird\": [200, 30, 20]}).set_index(\"split\")\nsample.plot.bar(stacked=True, rot=0, edgecolor=\"white\")",
          "image": "/images/plots/stacked-bar-chart.png",
          "imageAlt": "Stacked bar chart example chart",
          "imageWidth": 548,
          "imageHeight": 359
        },
        {
          "id": "stacked-area-chart",
          "sourceId": "p20",
          "name": "Stacked area chart",
          "category": "Composition",
          "useWhen": "you want to show how parts of a total change over time.",
          "dontUse": "with many series.",
          "library": "matplotlib",
          "alsoIn": "pandas df.plot.area · plotly px.area",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>North</th>\n      <th>South</th>\n      <th>West</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2025-01-01</th>\n      <td>37</td>\n      <td>22</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>2025-02-01</th>\n      <td>32</td>\n      <td>24</td>\n      <td>22</td>\n    </tr>\n    <tr>\n      <th>2025-03-01</th>\n      <td>30</td>\n      <td>29</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>2025-04-01</th>\n      <td>25</td>\n      <td>25</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>2025-05-01</th>\n      <td>26</td>\n      <td>24</td>\n      <td>21</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "months = pd.date_range(\"2025-01\", periods=12, freq=\"MS\")\nsample = pd.DataFrame({\"North\": rng.integers(20, 40, 12), \"South\": rng.integers(15, 30, 12),\n                       \"West\": rng.integers(10, 25, 12)}, index=months)\nplt.stackplot(sample.index, sample.T, labels=sample.columns, alpha=.85)\nplt.legend(loc=\"upper left\")",
          "image": "/images/plots/stacked-area-chart.png",
          "imageAlt": "Stacked area chart example chart",
          "imageWidth": 531,
          "imageHeight": 338
        },
        {
          "id": "treemap",
          "sourceId": "p21",
          "name": "Treemap",
          "category": "Composition",
          "useWhen": "you want to show parts of a whole with nested levels (category → sub-category).",
          "dontUse": "when exact values matter.",
          "library": "squarify + matplotlib",
          "alsoIn": "plotly px.treemap",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>city</th>\n      <th>sales</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>Delhi</td>\n      <td>420</td>\n    </tr>\n    <tr>\n      <td>Mumbai</td>\n      <td>380</td>\n    </tr>\n    <tr>\n      <td>Pune</td>\n      <td>160</td>\n    </tr>\n    <tr>\n      <td>Chennai</td>\n      <td>210</td>\n    </tr>\n    <tr>\n      <td>Kolkata</td>\n      <td>150</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "import squarify\nsample = pd.DataFrame({\"city\": [\"Delhi\", \"Mumbai\", \"Pune\", \"Chennai\", \"Kolkata\", \"Jaipur\"],\n                       \"sales\": [420, 380, 160, 210, 150, 90]})\nsquarify.plot(sizes=sample.sales, label=sample.city + \"\\n\" + sample.sales.astype(str),\n              color=[\"#2a78d6\",\"#eb6834\",\"#1baf7a\",\"#eda100\",\"#e87ba4\",\"#4a3aa7\"], alpha=.85, ec=\"white\")\nplt.axis(\"off\")",
          "image": "/images/plots/treemap.png",
          "imageAlt": "Treemap example chart",
          "imageWidth": 499,
          "imageHeight": 309
        },
        {
          "id": "waterfall-chart",
          "sourceId": "p22",
          "name": "Waterfall chart",
          "category": "Composition",
          "useWhen": "you want to show how gains and losses add up to a total.",
          "dontUse": "when the steps have no order.",
          "library": "matplotlib",
          "alsoIn": "plotly go.Waterfall",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>step</th>\n      <th>change</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>Start</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <td>Sales</td>\n      <td>60</td>\n    </tr>\n    <tr>\n      <td>Refunds</td>\n      <td>-15</td>\n    </tr>\n    <tr>\n      <td>Costs</td>\n      <td>-40</td>\n    </tr>\n    <tr>\n      <td>Tax</td>\n      <td>-20</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"step\": [\"Start\", \"Sales\", \"Refunds\", \"Costs\", \"Tax\"],\n                       \"change\": [100, 60, -15, -40, -20]})\nbase = sample.change.cumsum().shift(fill_value=0).where(sample.index > 0, 0)\ncolors = [\"#2a78d6\"] + [\"#1baf7a\" if c > 0 else \"#e34948\" for c in sample.change[1:]]\nplt.bar(sample.step, sample.change, bottom=base, color=colors)\nplt.bar(\"End\", sample.change.sum(), color=\"#2a78d6\")",
          "image": "/images/plots/waterfall-chart.png",
          "imageAlt": "Waterfall chart example chart",
          "imageWidth": 540,
          "imageHeight": 343
        }
      ]
    },
    {
      "id": "time-series",
      "title": "5. Time series: how does it change over time? (4)",
      "kind": "plots",
      "category": "Time series",
      "expectedCount": 4,
      "plots": [
        {
          "id": "line-chart",
          "sourceId": "p23",
          "name": "Line chart",
          "category": "Time series",
          "useWhen": "you want to show trends over time, including training loss.",
          "dontUse": "for unordered categories, or with more than ~5 lines.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.plot · pandas df.plot.line · plotly px.line",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>date</th>\n      <th>price</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2026-01-01</td>\n      <td>100.13</td>\n    </tr>\n    <tr>\n      <td>2026-01-02</td>\n      <td>99.99</td>\n    </tr>\n    <tr>\n      <td>2026-01-03</td>\n      <td>100.63</td>\n    </tr>\n    <tr>\n      <td>2026-01-04</td>\n      <td>100.74</td>\n    </tr>\n    <tr>\n      <td>2026-01-05</td>\n      <td>100.20</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"date\": pd.date_range(\"2026-01-01\", periods=120),\n                       \"price\": (100 + rng.normal(0, 1, 120).cumsum()).round(2)})\nsns.lineplot(sample, x=\"date\", y=\"price\"); plt.xticks(rotation=30)",
          "image": "/images/plots/line-chart.png",
          "imageAlt": "Line chart example chart",
          "imageWidth": 594,
          "imageHeight": 397
        },
        {
          "id": "seasonal-decomposition",
          "sourceId": "p24",
          "name": "Seasonal decomposition",
          "category": "Time series",
          "useWhen": "you want to split a series into trend, seasonality and noise before forecasting.",
          "dontUse": "on irregular or very short series.",
          "library": "statsmodels",
          "alsoIn": "statsmodels STL · prophet plot_components",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sales</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2022-01-01</th>\n      <td>100.5</td>\n    </tr>\n    <tr>\n      <th>2022-02-01</th>\n      <td>109.0</td>\n    </tr>\n    <tr>\n      <th>2022-03-01</th>\n      <td>119.6</td>\n    </tr>\n    <tr>\n      <th>2022-04-01</th>\n      <td>121.4</td>\n    </tr>\n    <tr>\n      <th>2022-05-01</th>\n      <td>118.8</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from statsmodels.tsa.seasonal import seasonal_decompose\nt = np.arange(48)\nsample = pd.DataFrame({\"sales\": (100 + 2*t + 15*np.sin(2*np.pi*t/12) + rng.normal(0, 4, 48)).round(1)},\n                      index=pd.date_range(\"2022-01\", periods=48, freq=\"MS\"))\nseasonal_decompose(sample[\"sales\"], period=12).plot()",
          "image": "/images/plots/seasonal-decomposition.png",
          "imageAlt": "Seasonal decomposition example chart",
          "imageWidth": 597,
          "imageHeight": 356
        },
        {
          "id": "acf-pacf-plot",
          "sourceId": "p25",
          "name": "ACF / PACF plot",
          "category": "Time series",
          "useWhen": "you need to pick ARIMA settings or check whether residuals are random.",
          "dontUse": "on non-time-series data.",
          "library": "statsmodels",
          "alsoIn": "pandas autocorrelation_plot",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>value</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.05</td>\n    </tr>\n    <tr>\n      <td>0.45</td>\n    </tr>\n    <tr>\n      <td>1.12</td>\n    </tr>\n    <tr>\n      <td>1.04</td>\n    </tr>\n    <tr>\n      <td>0.65</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from statsmodels.graphics.tsaplots import plot_acf\nt = np.arange(200)\nsample = pd.DataFrame({\"value\": (np.sin(2*np.pi*t/12) + rng.normal(0, .4, 200)).round(3)})\nplot_acf(sample[\"value\"], lags=36)",
          "image": "/images/plots/acf-pacf-plot.png",
          "imageAlt": "ACF / PACF plot example chart",
          "imageWidth": 556,
          "imageHeight": 361
        },
        {
          "id": "candlestick-chart",
          "sourceId": "p26",
          "name": "Candlestick chart",
          "category": "Time series",
          "useWhen": "you're plotting stock or crypto prices (open, high, low, close).",
          "dontUse": "for any non-price data.",
          "library": "mplfinance",
          "alsoIn": "plotly go.Candlestick",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Open</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Close</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2026-03-01</th>\n      <td>98.93</td>\n      <td>100.98</td>\n      <td>97.48</td>\n      <td>100.19</td>\n    </tr>\n    <tr>\n      <th>2026-03-02</th>\n      <td>101.50</td>\n      <td>102.68</td>\n      <td>99.15</td>\n      <td>99.99</td>\n    </tr>\n    <tr>\n      <th>2026-03-03</th>\n      <td>102.30</td>\n      <td>102.92</td>\n      <td>100.56</td>\n      <td>100.95</td>\n    </tr>\n    <tr>\n      <th>2026-03-04</th>\n      <td>101.89</td>\n      <td>102.99</td>\n      <td>100.75</td>\n      <td>101.11</td>\n    </tr>\n    <tr>\n      <th>2026-03-05</th>\n      <td>100.57</td>\n      <td>101.64</td>\n      <td>98.97</td>\n      <td>100.30</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "import mplfinance as mpf\nclose = 100 + rng.normal(0, 1.5, 40).cumsum()\nopen_ = close + rng.normal(0, 1, 40)\nsample = pd.DataFrame({\"Open\": open_, \"High\": np.maximum(open_, close) + rng.uniform(0, 1.5, 40),\n    \"Low\": np.minimum(open_, close) - rng.uniform(0, 1.5, 40), \"Close\": close},\n    index=pd.date_range(\"2026-03-01\", periods=40)).round(2)\nmpf.plot(sample, type=\"candle\", style=\"yahoo\", figsize=(5.6, 3.4))",
          "image": "/images/plots/candlestick-chart.png",
          "imageAlt": "Candlestick chart example chart",
          "imageWidth": 540,
          "imageHeight": 342
        }
      ]
    },
    {
      "id": "high-dimensional-data",
      "title": "6. High-dimensional data: are there hidden groups? (4)",
      "kind": "plots",
      "category": "High-dimensional data",
      "expectedCount": 4,
      "plots": [
        {
          "id": "heatmap",
          "sourceId": "p27",
          "name": "Heatmap",
          "category": "High-dimensional data",
          "useWhen": "you want to show a table of values as colors (crosstabs, grid-search results).",
          "dontUse": "with rows in random order; sort or cluster them first.",
          "library": "seaborn",
          "alsoIn": "matplotlib plt.imshow · plotly px.imshow",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>max_depth</th>\n      <th>2</th>\n      <th>4</th>\n      <th>6</th>\n      <th>8</th>\n      <th>10</th>\n    </tr>\n    <tr>\n      <th>learning_rate</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0.001</th>\n      <td>0.83</td>\n      <td>0.75</td>\n      <td>0.71</td>\n      <td>0.70</td>\n      <td>0.86</td>\n    </tr>\n    <tr>\n      <th>0.010</th>\n      <td>0.88</td>\n      <td>0.82</td>\n      <td>0.85</td>\n      <td>0.81</td>\n      <td>0.89</td>\n    </tr>\n    <tr>\n      <th>0.100</th>\n      <td>0.86</td>\n      <td>0.70</td>\n      <td>0.87</td>\n      <td>0.71</td>\n      <td>0.85</td>\n    </tr>\n    <tr>\n      <th>1.000</th>\n      <td>0.74</td>\n      <td>0.87</td>\n      <td>0.81</td>\n      <td>0.76</td>\n      <td>0.78</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame(rng.uniform(.70, .90, (4, 5)).round(3),\n    index=[0.001, 0.01, 0.1, 1.0], columns=[2, 4, 6, 8, 10])\nsample.index.name, sample.columns.name = \"learning_rate\", \"max_depth\"\nsns.heatmap(sample, annot=True, fmt=\".2f\", cmap=\"Blues\")",
          "image": "/images/plots/heatmap.png",
          "imageAlt": "Heatmap example chart",
          "imageWidth": 608,
          "imageHeight": 476
        },
        {
          "id": "pca-plot",
          "sourceId": "p28",
          "name": "PCA plot",
          "category": "High-dimensional data",
          "useWhen": "you want to squash many features into 2D and look for structure.",
          "dontUse": "when the structure is curved or non-linear.",
          "library": "scikit-learn + seaborn",
          "alsoIn": "yellowbrick PCA · plotly px.scatter",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>alcohol</th>\n      <th>malic_acid</th>\n      <th>ash</th>\n      <th>alcalinity_of_ash</th>\n      <th>magnesium</th>\n      <th>total_phenols</th>\n      <th>…</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>14.23</td>\n      <td>1.71</td>\n      <td>2.43</td>\n      <td>15.6</td>\n      <td>127.0</td>\n      <td>2.80</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>13.20</td>\n      <td>1.78</td>\n      <td>2.14</td>\n      <td>11.2</td>\n      <td>100.0</td>\n      <td>2.65</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>13.16</td>\n      <td>2.36</td>\n      <td>2.67</td>\n      <td>18.6</td>\n      <td>101.0</td>\n      <td>2.80</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>14.37</td>\n      <td>1.95</td>\n      <td>2.50</td>\n      <td>16.8</td>\n      <td>113.0</td>\n      <td>3.85</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>13.24</td>\n      <td>2.59</td>\n      <td>2.87</td>\n      <td>21.0</td>\n      <td>118.0</td>\n      <td>2.80</td>\n      <td>…</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import load_wine\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nwine = load_wine(as_frame=True); sample = wine.frame\nX2 = PCA(2).fit_transform(StandardScaler().fit_transform(wine.data))\nsns.scatterplot(x=X2[:, 0], y=X2[:, 1], hue=wine.target.astype(str), palette=\"deep\")\nplt.xlabel(\"PC1\"); plt.ylabel(\"PC2\")",
          "image": "/images/plots/pca-plot.png",
          "imageAlt": "PCA plot example chart",
          "imageWidth": 621,
          "imageHeight": 475
        },
        {
          "id": "t-sne-umap-plot",
          "sourceId": "p29",
          "name": "t-SNE / UMAP plot",
          "category": "High-dimensional data",
          "useWhen": "you want to see clusters in embeddings or complex data.",
          "dontUse": "to judge distances between clusters or cluster sizes, because those aren't meaningful.",
          "library": "scikit-learn + matplotlib",
          "alsoIn": "umap-learn umap.plot · plotly px.scatter",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>pixel_0_0</th>\n      <th>pixel_0_1</th>\n      <th>pixel_0_2</th>\n      <th>pixel_0_3</th>\n      <th>pixel_0_4</th>\n      <th>pixel_0_5</th>\n      <th>…</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>5.0</td>\n      <td>13.0</td>\n      <td>9.0</td>\n      <td>1.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>12.0</td>\n      <td>13.0</td>\n      <td>5.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>4.0</td>\n      <td>15.0</td>\n      <td>12.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>7.0</td>\n      <td>15.0</td>\n      <td>13.0</td>\n      <td>1.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>11.0</td>\n      <td>0.0</td>\n      <td>…</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import load_digits\nfrom sklearn.manifold import TSNE\ndigits = load_digits(as_frame=True); sample = digits.frame\nX2 = TSNE(2, random_state=0).fit_transform(digits.data)\nplt.scatter(X2[:, 0], X2[:, 1], c=digits.target, cmap=\"tab10\", s=6)\nplt.colorbar(label=\"digit\")",
          "image": "/images/plots/t-sne-umap-plot.png",
          "imageAlt": "t-SNE / UMAP plot example chart",
          "imageWidth": 590,
          "imageHeight": 460
        },
        {
          "id": "parallel-coordinates",
          "sourceId": "p30",
          "name": "Parallel coordinates",
          "category": "High-dimensional data",
          "useWhen": "you're comparing many features at once, e.g. hyperparameter tuning results.",
          "dontUse": "without scaling the features first.",
          "library": "pandas",
          "alsoIn": "plotly px.parallel_coordinates · optuna plot_parallel_coordinate",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>lr</th>\n      <th>depth</th>\n      <th>dropout</th>\n      <th>result</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.64</td>\n      <td>0.69</td>\n      <td>0.40</td>\n      <td>good</td>\n    </tr>\n    <tr>\n      <td>0.27</td>\n      <td>0.39</td>\n      <td>0.20</td>\n      <td>poor</td>\n    </tr>\n    <tr>\n      <td>0.04</td>\n      <td>0.14</td>\n      <td>0.09</td>\n      <td>poor</td>\n    </tr>\n    <tr>\n      <td>0.02</td>\n      <td>0.72</td>\n      <td>0.58</td>\n      <td>poor</td>\n    </tr>\n    <tr>\n      <td>0.81</td>\n      <td>0.53</td>\n      <td>0.30</td>\n      <td>good</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "sample = pd.DataFrame({\"lr\": rng.uniform(0, 1, 30), \"depth\": rng.uniform(0, 1, 30),\n                       \"dropout\": rng.uniform(0, 1, 30)}).round(2)\nsample[\"result\"] = np.where(sample.lr + sample.depth > 1, \"good\", \"poor\")\npd.plotting.parallel_coordinates(sample, \"result\", color=[\"#2a78d6\", \"#eb6834\"], alpha=.7)",
          "image": "/images/plots/parallel-coordinates.png",
          "imageAlt": "Parallel coordinates example chart",
          "imageWidth": 537,
          "imageHeight": 385
        }
      ]
    },
    {
      "id": "machine-learning",
      "title": "7. Machine learning: model results (10)",
      "kind": "plots",
      "category": "Machine learning",
      "expectedCount": 10,
      "plots": [
        {
          "id": "confusion-matrix",
          "sourceId": "p31",
          "name": "Confusion matrix",
          "category": "Machine learning",
          "useWhen": "you're checking any classifier: which classes get mixed up.",
          "dontUse": "raw counts on imbalanced data; normalize them.",
          "library": "scikit-learn",
          "alsoIn": "seaborn heatmap · yellowbrick ConfusionMatrix",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>y_test</th>\n      <th>y_pred</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>bird</td>\n      <td>bird</td>\n    </tr>\n    <tr>\n      <td>dog</td>\n      <td>dog</td>\n    </tr>\n    <tr>\n      <td>dog</td>\n      <td>dog</td>\n    </tr>\n    <tr>\n      <td>cat</td>\n      <td>cat</td>\n    </tr>\n    <tr>\n      <td>cat</td>\n      <td>cat</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.metrics import ConfusionMatrixDisplay\ny_test = rng.choice([\"cat\", \"dog\", \"bird\"], 300)\ny_pred = np.where(rng.random(300) < .8, y_test, rng.choice([\"cat\", \"dog\", \"bird\"], 300))\nsample = pd.DataFrame({\"y_test\": y_test, \"y_pred\": y_pred})\nConfusionMatrixDisplay.from_predictions(y_test, y_pred, normalize=\"true\", cmap=\"Blues\", values_format=\".2f\")\nplt.grid(False)",
          "image": "/images/plots/confusion-matrix.png",
          "imageAlt": "Confusion matrix example chart",
          "imageWidth": 572,
          "imageHeight": 475
        },
        {
          "id": "roc-curve",
          "sourceId": "p32",
          "name": "ROC curve",
          "category": "Machine learning",
          "useWhen": "you're comparing binary classifiers on balanced data.",
          "dontUse": "on highly imbalanced data (fraud, disease). Use a PR curve instead.",
          "library": "scikit-learn",
          "alsoIn": "yellowbrick ROCAUC · scikit-plot",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>f1</th>\n      <th>f2</th>\n      <th>f3</th>\n      <th>f4</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2.32</td>\n      <td>0.64</td>\n      <td>-1.50</td>\n      <td>-0.03</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <td>0.29</td>\n      <td>2.18</td>\n      <td>0.57</td>\n      <td>0.43</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <td>-0.27</td>\n      <td>1.34</td>\n      <td>0.88</td>\n      <td>-1.36</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>0.21</td>\n      <td>0.03</td>\n      <td>3.06</td>\n      <td>0.04</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <td>-1.25</td>\n      <td>0.79</td>\n      <td>-1.43</td>\n      <td>0.73</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import RocCurveDisplay\nX, y = make_classification(1000, n_features=10, random_state=0)\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\nsample = pd.DataFrame(X_test[:, :4], columns=[\"f1\", \"f2\", \"f3\", \"f4\"]).round(2).assign(y=y_test)\nmodel = LogisticRegression().fit(X_train, y_train)\nRocCurveDisplay.from_estimator(model, X_test, y_test)\nplt.plot([0, 1], [0, 1], \"--\", color=\"gray\")",
          "image": "/images/plots/roc-curve.png",
          "imageAlt": "ROC curve example chart",
          "imageWidth": 492,
          "imageHeight": 477
        },
        {
          "id": "precision-recall-curve",
          "sourceId": "p33",
          "name": "Precision-Recall curve",
          "category": "Machine learning",
          "useWhen": "the positive class is rare.",
          "dontUse": "when both classes matter equally and the data is balanced (ROC is fine there).",
          "library": "scikit-learn",
          "alsoIn": "yellowbrick PrecisionRecallCurve",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>f1</th>\n      <th>f2</th>\n      <th>f3</th>\n      <th>f4</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.43</td>\n      <td>-0.72</td>\n      <td>-0.01</td>\n      <td>-2.27</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>0.28</td>\n      <td>-1.57</td>\n      <td>0.01</td>\n      <td>0.73</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>-1.09</td>\n      <td>0.17</td>\n      <td>0.26</td>\n      <td>0.35</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>0.49</td>\n      <td>-0.69</td>\n      <td>-0.02</td>\n      <td>0.35</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>-0.47</td>\n      <td>0.40</td>\n      <td>0.11</td>\n      <td>-0.88</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import PrecisionRecallDisplay\nX, y = make_classification(2000, weights=[.95], random_state=0)   # only 5% positives\nX_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)\nsample = pd.DataFrame(X_test[:, :4], columns=[\"f1\", \"f2\", \"f3\", \"f4\"]).round(2).assign(y=y_test)\nmodel = RandomForestClassifier(random_state=0).fit(X_train, y_train)\nPrecisionRecallDisplay.from_estimator(model, X_test, y_test)",
          "image": "/images/plots/precision-recall-curve.png",
          "imageAlt": "Precision-Recall curve example chart",
          "imageWidth": 492,
          "imageHeight": 477
        },
        {
          "id": "calibration-curve",
          "sourceId": "p34",
          "name": "Calibration curve",
          "category": "Machine learning",
          "useWhen": "you need predicted probabilities you can trust (risk scores).",
          "dontUse": "when you only care about the final label.",
          "library": "scikit-learn",
          "alsoIn": "yellowbrick (calibration via sklearn)",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>f1</th>\n      <th>f2</th>\n      <th>f3</th>\n      <th>f4</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>-0.22</td>\n      <td>1.92</td>\n      <td>-0.30</td>\n      <td>-0.24</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <td>-0.61</td>\n      <td>-0.36</td>\n      <td>1.04</td>\n      <td>-0.26</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <td>-1.24</td>\n      <td>-1.47</td>\n      <td>-0.98</td>\n      <td>0.03</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>0.63</td>\n      <td>-0.07</td>\n      <td>-1.16</td>\n      <td>1.18</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <td>1.52</td>\n      <td>-0.20</td>\n      <td>0.03</td>\n      <td>1.77</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import make_classification\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.calibration import CalibrationDisplay\nX, y = make_classification(3000, random_state=0)\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\nsample = pd.DataFrame(X_test[:, :4], columns=[\"f1\", \"f2\", \"f3\", \"f4\"]).round(2).assign(y=y_test)\nmodel = GaussianNB().fit(X_train, y_train)\nCalibrationDisplay.from_estimator(model, X_test, y_test, n_bins=10)",
          "image": "/images/plots/calibration-curve.png",
          "imageAlt": "Calibration curve example chart",
          "imageWidth": 623,
          "imageHeight": 475
        },
        {
          "id": "residual-plot",
          "sourceId": "p35",
          "name": "Residual plot",
          "category": "Machine learning",
          "useWhen": "you're checking a regression model; errors should look random.",
          "dontUse": "for classification models.",
          "library": "seaborn",
          "alsoIn": "scikit-learn PredictionErrorDisplay · yellowbrick ResidualsPlot",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>y_test</th>\n      <th>y_pred</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>18.89</td>\n      <td>19.61</td>\n    </tr>\n    <tr>\n      <td>6.49</td>\n      <td>8.59</td>\n    </tr>\n    <tr>\n      <td>3.39</td>\n      <td>1.73</td>\n    </tr>\n    <tr>\n      <td>-0.08</td>\n      <td>1.00</td>\n    </tr>\n    <tr>\n      <td>24.57</td>\n      <td>24.90</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "x = rng.uniform(0, 10, 150)\ny_test = 3*x + rng.normal(0, 2, 150)\ny_pred = 3*x + 0.5                       # predictions from some model\nsample = pd.DataFrame({\"y_test\": y_test, \"y_pred\": y_pred}).round(2)\nsns.residplot(x=y_pred, y=y_test, lowess=True, line_kws={\"color\": \"#eb6834\"})\nplt.xlabel(\"predicted\"); plt.ylabel(\"residual (actual − predicted)\")",
          "image": "/images/plots/residual-plot.png",
          "imageAlt": "Residual plot example chart",
          "imageWidth": 623,
          "imageHeight": 475
        },
        {
          "id": "predicted-vs-actual",
          "sourceId": "p36",
          "name": "Predicted vs Actual",
          "category": "Machine learning",
          "useWhen": "you're checking a regression model; points should sit close to the diagonal.",
          "dontUse": "without drawing the y = x line.",
          "library": "matplotlib",
          "alsoIn": "scikit-learn PredictionErrorDisplay · yellowbrick PredictionError",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>y_test</th>\n      <th>y_pred</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>354.8</td>\n      <td>351.5</td>\n    </tr>\n    <tr>\n      <td>207.9</td>\n      <td>183.8</td>\n    </tr>\n    <tr>\n      <td>116.4</td>\n      <td>148.8</td>\n    </tr>\n    <tr>\n      <td>106.6</td>\n      <td>97.9</td>\n    </tr>\n    <tr>\n      <td>425.3</td>\n      <td>427.8</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "y_test = rng.uniform(100, 500, 150)\ny_pred = y_test + rng.normal(0, 30, 150)\nsample = pd.DataFrame({\"y_test\": y_test, \"y_pred\": y_pred}).round(1)\nplt.scatter(y_test, y_pred, alpha=.7)\nplt.axline((100, 100), slope=1, color=\"gray\", ls=\"--\")\nplt.xlabel(\"actual\"); plt.ylabel(\"predicted\")",
          "image": "/images/plots/predicted-vs-actual.png",
          "imageAlt": "Predicted vs Actual example chart",
          "imageWidth": 628,
          "imageHeight": 475
        },
        {
          "id": "loss-accuracy-curve",
          "sourceId": "p37",
          "name": "Loss / Accuracy curve",
          "category": "Machine learning",
          "useWhen": "you're training a neural network and need to spot overfitting (val loss going up).",
          "dontUse": "with training loss alone; always plot validation too.",
          "library": "matplotlib",
          "alsoIn": "TensorBoard · Weights & Biases · MLflow",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>epoch</th>\n      <th>loss</th>\n      <th>val_loss</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>1</td>\n      <td>1.11</td>\n      <td>1.18</td>\n    </tr>\n    <tr>\n      <td>2</td>\n      <td>0.98</td>\n      <td>1.06</td>\n    </tr>\n    <tr>\n      <td>3</td>\n      <td>0.88</td>\n      <td>0.94</td>\n    </tr>\n    <tr>\n      <td>4</td>\n      <td>0.78</td>\n      <td>0.85</td>\n    </tr>\n    <tr>\n      <td>5</td>\n      <td>0.69</td>\n      <td>0.76</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "epochs = np.arange(1, 31)\nsample = pd.DataFrame({\"epoch\": epochs,\n    \"loss\": (1.2*np.exp(-epochs/8) + .05).round(3),\n    \"val_loss\": (1.2*np.exp(-epochs/8) + .12 + .006*np.maximum(0, epochs-12)**1.3).round(3)})\n# with Keras: sample = pd.DataFrame(history.history)\nplt.plot(sample.epoch, sample.loss, label=\"train\")\nplt.plot(sample.epoch, sample.val_loss, label=\"validation\")\nplt.xlabel(\"epoch\"); plt.ylabel(\"loss\"); plt.legend()",
          "image": "/images/plots/loss-accuracy-curve.png",
          "imageAlt": "Loss / Accuracy curve example chart",
          "imageWidth": 623,
          "imageHeight": 475
        },
        {
          "id": "learning-curve",
          "sourceId": "p38",
          "name": "Learning curve",
          "category": "Machine learning",
          "useWhen": "you need to decide whether more data would help.",
          "dontUse": "on very large models where retraining many times is too slow.",
          "library": "scikit-learn",
          "alsoIn": "yellowbrick LearningCurve",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>pixel_0_0</th>\n      <th>pixel_0_1</th>\n      <th>pixel_0_2</th>\n      <th>pixel_0_3</th>\n      <th>pixel_0_4</th>\n      <th>pixel_0_5</th>\n      <th>…</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>5.0</td>\n      <td>13.0</td>\n      <td>9.0</td>\n      <td>1.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>12.0</td>\n      <td>13.0</td>\n      <td>5.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>4.0</td>\n      <td>15.0</td>\n      <td>12.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>7.0</td>\n      <td>15.0</td>\n      <td>13.0</td>\n      <td>1.0</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>11.0</td>\n      <td>0.0</td>\n      <td>…</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import load_digits\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import LearningCurveDisplay\ndigits = load_digits(as_frame=True); sample = digits.frame\nLearningCurveDisplay.from_estimator(SVC(gamma=.001), digits.data, digits.target, cv=5,\n                                    train_sizes=np.linspace(.1, 1, 6))",
          "image": "/images/plots/learning-curve.png",
          "imageAlt": "Learning curve example chart",
          "imageWidth": 633,
          "imageHeight": 475
        },
        {
          "id": "elbow-plot",
          "sourceId": "p39",
          "name": "Elbow plot",
          "category": "Machine learning",
          "useWhen": "you're choosing the number of clusters (k) for K-Means.",
          "dontUse": "by itself, because the elbow is often unclear. Also check silhouette score.",
          "library": "scikit-learn + matplotlib",
          "alsoIn": "yellowbrick KElbowVisualizer",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>x</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>2.63</td>\n      <td>1.25</td>\n    </tr>\n    <tr>\n      <td>4.36</td>\n      <td>-0.16</td>\n    </tr>\n    <tr>\n      <td>1.02</td>\n      <td>4.12</td>\n    </tr>\n    <tr>\n      <td>-0.18</td>\n      <td>5.08</td>\n    </tr>\n    <tr>\n      <td>0.75</td>\n      <td>2.56</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "from sklearn.datasets import make_blobs\nfrom sklearn.cluster import KMeans\nX, _ = make_blobs(500, centers=4, random_state=0)\nsample = pd.DataFrame(X, columns=[\"x\", \"y\"]).round(2)\nk_values = range(1, 10)\ninertias = [KMeans(k, n_init=10, random_state=0).fit(X).inertia_ for k in k_values]\nplt.plot(k_values, inertias, \"o-\"); plt.xlabel(\"k (clusters)\"); plt.ylabel(\"inertia\")",
          "image": "/images/plots/elbow-plot.png",
          "imageAlt": "Elbow plot example chart",
          "imageWidth": 638,
          "imageHeight": 475
        },
        {
          "id": "feature-importance-shap-plot",
          "sourceId": "p40",
          "name": "Feature importance / SHAP plot",
          "category": "Machine learning",
          "useWhen": "you want to explain which features drive the model.",
          "dontUse": "tree \"feature_importances_\" alone, because it is biased. Prefer SHAP or permutation importance.",
          "library": "shap",
          "alsoIn": "scikit-learn permutation_importance · xgboost plot_importance · lightgbm plot_importance",
          "sampleTableHtml": "<table class=\"dataframe data\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>age</th>\n      <th>sex</th>\n      <th>bmi</th>\n      <th>bp</th>\n      <th>s1</th>\n      <th>s2</th>\n      <th>…</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <td>0.04</td>\n      <td>0.05</td>\n      <td>0.06</td>\n      <td>0.02</td>\n      <td>-0.04</td>\n      <td>-0.03</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>-0.00</td>\n      <td>-0.04</td>\n      <td>-0.05</td>\n      <td>-0.03</td>\n      <td>-0.01</td>\n      <td>-0.02</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.09</td>\n      <td>0.05</td>\n      <td>0.04</td>\n      <td>-0.01</td>\n      <td>-0.05</td>\n      <td>-0.03</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>-0.09</td>\n      <td>-0.04</td>\n      <td>-0.01</td>\n      <td>-0.04</td>\n      <td>0.01</td>\n      <td>0.02</td>\n      <td>…</td>\n    </tr>\n    <tr>\n      <td>0.01</td>\n      <td>-0.04</td>\n      <td>-0.04</td>\n      <td>0.02</td>\n      <td>0.00</td>\n      <td>0.02</td>\n      <td>…</td>\n    </tr>\n  </tbody>\n</table>",
          "code": "import shap\nfrom sklearn.datasets import load_diabetes\nfrom sklearn.ensemble import RandomForestRegressor\ndata = load_diabetes(as_frame=True); sample = data.frame\nmodel = RandomForestRegressor(100, random_state=0).fit(data.data, data.target)\nshap_values = shap.TreeExplainer(model)(data.data[:200])\nshap.plots.beeswarm(shap_values, max_display=8, show=False)",
          "image": "/images/plots/feature-importance-shap-plot.png",
          "imageAlt": "Feature importance / SHAP plot example chart",
          "imageWidth": 941,
          "imageHeight": 480
        }
      ]
    },
    {
      "id": "quick-rule",
      "title": "Quick rule: which plot?",
      "kind": "rules",
      "items": [
        "One number column → Histogram or Box plot",
        "Two number columns → Scatter plot",
        "Category vs number → Bar chart or Box plot",
        "Over time → Line chart",
        "Parts of a whole → Stacked bar (pie only for ≤5 parts)",
        "Many features → Correlation heatmap or Pair plot",
        "Classifier results → Confusion matrix + ROC / PR curve",
        "Regression results → Residual plot + Predicted vs Actual"
      ]
    }
  ]
}
