{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Anscombe's quartet\n\n`Anscombe's quartet`_ is a group of datasets (x, y) that have the same mean,\nstandard deviation, and regression line, but which are qualitatively different.\n\nIt is often used to illustrate the importance of looking at a set of data\ngraphically and not only relying on basic statistic properties.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\nimport numpy as np\n\nx = [10, 8, 13, 9, 11, 14, 6, 4, 12, 7, 5]\ny1 = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]\ny2 = [9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26, 4.74]\ny3 = [7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73]\nx4 = [8, 8, 8, 8, 8, 8, 8, 19, 8, 8, 8]\ny4 = [6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89]\n\ndatasets = {\n 'I': (x, y1),\n 'II': (x, y2),\n 'III': (x, y3),\n 'IV': (x4, y4)\n}\n\nfig, axs = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(6, 6),\n gridspec_kw={'wspace': 0.08, 'hspace': 0.08})\naxs[0, 0].set(xlim=(0, 20), ylim=(2, 14))\naxs[0, 0].set(xticks=(0, 10, 20), yticks=(4, 8, 12))\n\nfor ax, (label, (x, y)) in zip(axs.flat, datasets.items()):\n ax.text(0.1, 0.9, label, fontsize=20, transform=ax.transAxes, va='top')\n ax.tick_params(direction='in', top=True, right=True)\n ax.plot(x, y, 'o')\n\n # linear regression\n p1, p0 = np.polyfit(x, y, deg=1) # slope, intercept\n ax.axline(xy1=(0, p0), slope=p1, color='r', lw=2)\n\n # add text box for the statistics\n stats = (f'$\\\\mu$ = {np.mean(y):.2f}\\n'\n f'$\\\\sigma$ = {np.std(y):.2f}\\n'\n f'$r$ = {np.corrcoef(x, y)[0][1]:.2f}')\n bbox = dict(boxstyle='round', fc='blanchedalmond', ec='orange', alpha=0.5)\n ax.text(0.95, 0.07, stats, fontsize=9, bbox=bbox,\n transform=ax.transAxes, horizontalalignment='right')\n\nplt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.axline` / `matplotlib.pyplot.axline`\n - `matplotlib.axes.Axes.text` / `matplotlib.pyplot.text`\n - `matplotlib.axes.Axes.tick_params` / matplotlib.pyplot.tick_params`\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}