|
122 | 122 | },
|
123 | 123 | "outputs": [],
|
124 | 124 | "source": [
|
125 |
| - "x = np.linspace(0, 1, 100) # 100 numbers from 0 to 1\n", |
| 125 | + "x = np.linspace(0, 1, 250) # 250 numbers from 0 to 1\n", |
126 | 126 | "\n",
|
127 | 127 | "plt.plot(x, x**2)\n",
|
128 | 128 | "# If not interactive, e.g. in a script: \n",
|
|
144 | 144 | },
|
145 | 145 | "outputs": [],
|
146 | 146 | "source": [
|
147 |
| - "t = np.linspace(0, 2 * np.pi) # 50 points between 0 and 2π\n", |
| 147 | + "t = np.linspace(0, 2 * np.pi, 50) # 50 points between 0 and 2π\n", |
148 | 148 | "plt.plot(t, np.sin(t));"
|
149 | 149 | ]
|
150 | 150 | },
|
|
373 | 373 | "**Remember**: Legend entries are only generated for plot objects that have a label (note x⁴ is missing)!"
|
374 | 374 | ]
|
375 | 375 | },
|
| 376 | + { |
| 377 | + "cell_type": "markdown", |
| 378 | + "metadata": {}, |
| 379 | + "source": [ |
| 380 | + "### Legend outside the plot" |
| 381 | + ] |
| 382 | + }, |
| 383 | + { |
| 384 | + "cell_type": "code", |
| 385 | + "execution_count": null, |
| 386 | + "metadata": {}, |
| 387 | + "outputs": [], |
| 388 | + "source": [ |
| 389 | + "plt.plot(t, np.sin(t), label=r'$\\sin(t)$')\n", |
| 390 | + "plt.plot(t, np.cos(t), label=r'$\\cos(t)$')\n", |
| 391 | + "plt.legend()\n", |
| 392 | + "\n", |
| 393 | + "# put the legend in the center above the plot\n", |
| 394 | + "# coordinates are relative to the axes size.\n", |
| 395 | + "plt.legend(\n", |
| 396 | + " bbox_to_anchor=(0.5, 1.01),\n", |
| 397 | + " loc='lower center', # this no sets the anchor of the legend\n", |
| 398 | + " ncol=2,\n", |
| 399 | + ")\n", |
| 400 | + "plt.tight_layout()" |
| 401 | + ] |
| 402 | + }, |
376 | 403 | {
|
377 | 404 | "cell_type": "markdown",
|
378 | 405 | "metadata": {},
|
|
494 | 521 | "errX = np.random.normal(0, 0.4, 10)\n",
|
495 | 522 | "errY = np.random.normal(0, 0.4, 10)\n",
|
496 | 523 | "\n",
|
497 |
| - "plt.errorbar(x + errX, x + errY, xerr=0.4, yerr=errY, fmt='o');" |
| 524 | + "plt.errorbar(x + errX, x + errY, xerr=0.4, yerr=errY, linestyle='');" |
498 | 525 | ]
|
499 | 526 | },
|
500 | 527 | {
|
|
699 | 726 | "### Using Norms – e.g. Logarithmic colorscale"
|
700 | 727 | ]
|
701 | 728 | },
|
| 729 | + { |
| 730 | + "cell_type": "code", |
| 731 | + "execution_count": null, |
| 732 | + "metadata": {}, |
| 733 | + "outputs": [], |
| 734 | + "source": [ |
| 735 | + "from matplotlib.colors import LogNorm, DivergingNorm, SymLogNorm\n", |
| 736 | + "\n", |
| 737 | + "plt.hist2d(x, y, bins=50, norm=LogNorm())\n", |
| 738 | + "plt.colorbar(label='Counts');" |
| 739 | + ] |
| 740 | + }, |
| 741 | + { |
| 742 | + "cell_type": "markdown", |
| 743 | + "metadata": {}, |
| 744 | + "source": [ |
| 745 | + "### SymLogNorm\n", |
| 746 | + "\n", |
| 747 | + "The SymLogNorm uses two logscales, one for the negative, one for the positive numbers.\n", |
| 748 | + "Around 0, a linear scale is used. The threshold value for the linear scale\n", |
| 749 | + "has to be given." |
| 750 | + ] |
| 751 | + }, |
702 | 752 | {
|
703 | 753 | "cell_type": "code",
|
704 | 754 | "execution_count": null,
|
|
707 | 757 | },
|
708 | 758 | "outputs": [],
|
709 | 759 | "source": [
|
710 |
| - "from matplotlib.colors import LogNorm, DivergingNorm, SymLogNorm\n", |
| 760 | + "x2, y2 = np.random.multivariate_normal(mean, cov, size=10000).T\n", |
711 | 761 | "\n",
|
712 |
| - "plt.hist2d(x, y, bins=50, norm=LogNorm())\n", |
| 762 | + "\n", |
| 763 | + "hist1, xedges, yedges = np.histogram2d(x, y, bins=100)\n", |
| 764 | + "hist2, _, _ = np.histogram2d(x2, y2, bins=[xedges, yedges])\n", |
| 765 | + "\n", |
| 766 | + "\n", |
| 767 | + "plt.pcolormesh(\n", |
| 768 | + " xedges,\n", |
| 769 | + " yedges,\n", |
| 770 | + " hist1 - hist2,\n", |
| 771 | + " cmap='RdBu',\n", |
| 772 | + " norm=SymLogNorm(0.1),\n", |
| 773 | + " vmin=-15,\n", |
| 774 | + " vmax=15,\n", |
| 775 | + ")\n", |
713 | 776 | "\n",
|
714 | 777 | "plt.colorbar();"
|
715 | 778 | ]
|
|
0 commit comments