|
192 | 192 | " ax.set_title(\"title\");" |
193 | 193 | ] |
194 | 194 | }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "For complex subplots on a non-uniform grid, use `subplot_mosaic`: https://matplotlib.org/stable/users/explain/axes/mosaic.html#mosaic" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [ |
| 208 | + "fig, axes = plt.subplot_mosaic(\n", |
| 209 | + " \"\"\"\n", |
| 210 | + " A.D\n", |
| 211 | + " CBD\n", |
| 212 | + " \"\"\"\n", |
| 213 | + " )# There is no internal meaning to the letters used, they could be anything.\n", |
| 214 | + "# `axes`` now is a dictionary with the letters as keys, Axes objects as values.\n", |
| 215 | + "for i, ax in axes.items():\n", |
| 216 | + " ax.plot(x, y, \"r\")\n", |
| 217 | + " ax.set_xlabel(\"x\")\n", |
| 218 | + " ax.set_ylabel(\"y\")\n", |
| 219 | + " ax.set_title(i);\n" |
| 220 | + ] |
| 221 | + }, |
195 | 222 | { |
196 | 223 | "cell_type": "markdown", |
197 | 224 | "metadata": {}, |
|
249 | 276 | "source": [ |
250 | 277 | "The advantage with this method is that if curves are added or removed from the figure, the legend is automatically updated accordingly.\n", |
251 | 278 | "\n", |
252 | | - "The `legend` function takes an optional keyword argument `loc` that can be used to specify where in the figure the legend is to be drawn. The allowed values of `loc` are keywords for the various places the legend can be drawn. See https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib.axes.Axes.legend for details. Some of the most common `loc` values are:\n", |
| 279 | + "The `legend` function takes an optional keyword argument `loc` that can be used to specify where in the figure the legend is to be drawn. The allowed values of `loc` are keywords for the various places the legend can be drawn. See https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib.axes.Axes.legend for details. Some common `loc` values are:\n", |
253 | 280 | "\n", |
254 | 281 | "```python\n", |
255 | 282 | "ax.legend(loc='best')\n", |
|
285 | 312 | "cell_type": "markdown", |
286 | 313 | "metadata": {}, |
287 | 314 | "source": [ |
288 | | - "Colors can also be defined by their names or RGB hex codes and optionally provide an alpha value using the `color` and `alpha` keyword arguments:" |
| 315 | + "Colors can also be defined by their names or RGB hex codes and optionally provide an alpha value using the `color` and `alpha` keyword arguments.\n", |
| 316 | + "\n", |
| 317 | + "There is also a [large list of named colors](https://matplotlib.org/stable/gallery/color/named_colors.html) available." |
289 | 318 | ] |
290 | 319 | }, |
291 | 320 | { |
|
449 | 478 | "\n", |
450 | 479 | "That was easy, but it isn't so pretty with overlapping figure axes and labels, right?\n", |
451 | 480 | "\n", |
452 | | - "Overlapping figures can be dealt with using the `fig.tight_layout` method, which automatically adjusts the positions of the axes on the figure canvas so that there is no overlapping content:" |
| 481 | + "Overlapping figures can be dealt with by passing `layout=\"constrained\"` keyword argument when creating a `fig` object. The `constrained` layout automatically adjusts the positions of the axes on the figure canvas so that there is no overlapping content. See https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html for more details" |
453 | 482 | ] |
454 | 483 | }, |
455 | 484 | { |
|
458 | 487 | "metadata": {}, |
459 | 488 | "outputs": [], |
460 | 489 | "source": [ |
461 | | - "fig, axes = plt.subplots(nrows=1, ncols=2)\n", |
| 490 | + "fig, axes = plt.subplots(nrows=1, ncols=2, layout=\"constrained\")\n", |
462 | 491 | "\n", |
463 | 492 | "for ax in axes:\n", |
464 | 493 | " ax.plot(x, y, \"r\")\n", |
465 | 494 | " ax.set_xlabel(\"x\")\n", |
466 | 495 | " ax.set_ylabel(\"y\")\n", |
467 | | - " ax.set_title(\"title\")\n", |
468 | | - "\n", |
469 | | - "fig.tight_layout();" |
| 496 | + " ax.set_title(\"title\")" |
470 | 497 | ] |
471 | 498 | }, |
472 | 499 | { |
|
487 | 514 | "cell_type": "markdown", |
488 | 515 | "metadata": {}, |
489 | 516 | "source": [ |
490 | | - "`matplotlib` allows the aspect ratio, DPI and figure size to be specified when the `Figure` object is created, using the `figsize` and `dpi` keyword arguments. `figsize` is a tuple of the width and height of the figure in inches, and `dpi` is the dots-per-inch (pixel per inch). To create an 800x400 pixel, 100 dots-per-inch figure, we can do: " |
| 517 | + "`matplotlib` allows the aspect ratio, DPI and figure size to be specified when the `Figure` object is created, using the `figsize` and `dpi` keyword arguments. `figsize` is a tuple of the width and height of the figure in inches, and `dpi` is the dots-per-inch (pixel per inch). To create an 400x400 pixel, 100 dots-per-inch figure, we can do: " |
491 | 518 | ] |
492 | 519 | }, |
493 | 520 | { |
|
496 | 523 | "metadata": {}, |
497 | 524 | "outputs": [], |
498 | 525 | "source": [ |
499 | | - "fig, axes = plt.subplots(figsize=(12, 3), dpi=100)\n", |
| 526 | + "fig, axes = plt.subplots(figsize=(4, 4), dpi=100)\n", |
500 | 527 | "\n", |
501 | 528 | "axes.plot(x, y, \"r\")\n", |
502 | 529 | "axes.set_xlabel(\"x\")\n", |
|
555 | 582 | "metadata": {}, |
556 | 583 | "outputs": [], |
557 | 584 | "source": [ |
558 | | - "fig.savefig(output_path / \"filename.svg\")" |
| 585 | + "fig.savefig(output_path / \"filename.pdf\")" |
559 | 586 | ] |
560 | 587 | }, |
561 | 588 | { |
|
616 | 643 | "axes[1].set_title(\"tight axes\")\n", |
617 | 644 | "\n", |
618 | 645 | "axes[2].plot(x, x**2, x, x**3)\n", |
619 | | - "axes[2].set_ylim([0, 60])\n", |
620 | | - "axes[2].set_xlim([2, 3])\n", |
| 646 | + "axes[2].set_ylim(0, 60)\n", |
| 647 | + "axes[2].set_xlim(2, 3)\n", |
621 | 648 | "axes[2].set_title(\"custom axes range\");" |
622 | 649 | ] |
623 | 650 | }, |
|
650 | 677 | "cell_type": "markdown", |
651 | 678 | "metadata": {}, |
652 | 679 | "source": [ |
653 | | - "Logarithmic scales can also be specified using `semilogx()`, `semilogy()`, or `loglog()`. What are the differences in the figures plotted using `.set_yscale(\"log\")` and `.semilogy()`?\n", |
654 | | - "\n", |
| 680 | + "Logarithmic scales can also be specified using `semilogx()`, `semilogy()`, or `loglog()`. What are the differences in the figures plotted using `.set_yscale(\"log\")` and `.semilogy()`?" |
| 681 | + ] |
| 682 | + }, |
| 683 | + { |
| 684 | + "cell_type": "code", |
| 685 | + "execution_count": null, |
| 686 | + "metadata": {}, |
| 687 | + "outputs": [], |
| 688 | + "source": [] |
| 689 | + }, |
| 690 | + { |
| 691 | + "cell_type": "markdown", |
| 692 | + "metadata": {}, |
| 693 | + "source": [ |
655 | 694 | "### Placement of ticks and custom tick labels\n", |
656 | 695 | "\n", |
657 | 696 | "The axis ticks can be explicitly set using with `set_xticks` and `set_yticks`, which both take a list of values for where on the axis the ticks are to be placed. The `set_xticklabels` and `set_yticklabels` methods can be used to provide a list of custom text labels for each tick location:" |
|
683 | 722 | "cell_type": "markdown", |
684 | 723 | "metadata": {}, |
685 | 724 | "source": [ |
686 | | - "There are a number of more advanced methods for controlling major and minor tick placement in `matplotlib` figures, such as automatic placement according to different policies. See http://`matplotlib`.org/api/ticker_api.html for details.\n", |
| 725 | + "There are a number of more advanced methods for controlling major and minor tick placement in `matplotlib` figures, such as automatic placement according to different policies. See http://matplotlib.org/api/ticker_api.html for details.\n", |
687 | 726 | "\n", |
688 | 727 | "### Axis grid\n", |
689 | 728 | "\n", |
|
727 | 766 | "ax1.plot(x, x**2, lw=2, color=\"blue\")\n", |
728 | 767 | "ax1.set_ylabel(r\"area $(m^2)$\", fontsize=18, color=\"blue\")\n", |
729 | 768 | "for label in ax1.get_yticklabels():\n", |
730 | | - " label.set_color(\"blue\")\n", |
731 | | - "\n", |
| 769 | + " label.set_color(\"blue\")\n" |
| 770 | + ] |
| 771 | + }, |
| 772 | + { |
| 773 | + "cell_type": "code", |
| 774 | + "execution_count": null, |
| 775 | + "metadata": {}, |
| 776 | + "outputs": [], |
| 777 | + "source": [ |
732 | 778 | "ax2 = ax1.twinx()\n", |
733 | 779 | "ax2.plot(x, x**3, lw=2, color=\"red\")\n", |
734 | 780 | "ax2.set_ylabel(r\"volume $(m^3)$\", fontsize=18, color=\"red\")\n", |
|
742 | 788 | "source": [ |
743 | 789 | "## Other 2D plot styles\n", |
744 | 790 | "\n", |
745 | | - "In addition to the regular `plot` method, there are a number of other functions for generating different kind of plots. See the `matplotlib` plot gallery for a complete list of available plot types: http://`matplotlib`.org/gallery.html. Some of the more useful ones are show below:" |
| 791 | + "In addition to the regular `plot` method, there are a number of other functions for generating different kind of plots. See the `matplotlib` plot gallery for a complete list of available plot types: http://matplotlib.org/gallery.html. Some of the more useful ones are show below:" |
746 | 792 | ] |
747 | 793 | }, |
748 | 794 | { |
|
801 | 847 | "source": [ |
802 | 848 | "## Text annotation\n", |
803 | 849 | "\n", |
804 | | - "Annotating text in `matplotlib` figures can be done using the `text` function. It supports LaTeX formatting just like axis label texts and titles:" |
| 850 | + "Annotating text in `matplotlib` figures can be done using the `annotate` function. It supports LaTeX formatting just like axis label texts and titles:" |
805 | 851 | ] |
806 | 852 | }, |
807 | 853 | { |
|
814 | 860 | "\n", |
815 | 861 | "ax.plot(xx, xx**2, xx, xx**3)\n", |
816 | 862 | "\n", |
817 | | - "ax.text(0.15, 0.2, r\"$\\alpha y=x^2$\", fontsize=20, color=\"blue\")\n", |
818 | | - "ax.text(0.65, 0.1, r\"$y=x^3$\", fontsize=20, color=\"green\");" |
| 863 | + "ax.annotate(text=r'$\\alpha y=x^2$', xy=(0.5, 0.2), fontsize=20, color='tab:blue', va='bottom', ha='right')\n", |
| 864 | + "ax.annotate(r'$y=x^3$', xy=(xx[25], (xx**4)[25]), xytext=(0.7, -0.2), fontsize=20, color='tab:orange',\n", |
| 865 | + " arrowprops=dict(arrowstyle='->'));" |
819 | 866 | ] |
820 | 867 | }, |
821 | 868 | { |
|
824 | 871 | "source": [ |
825 | 872 | "## Colormap and contour figures\n", |
826 | 873 | "\n", |
827 | | - "Colormaps and contour figures are useful for plotting functions of two variables. A colormap will be used to encode one dimension of the data in most of these functions. There are a number of predefined colormaps. It is relatively straightforward to define custom colormaps. For a list of pre-defined colormaps, see: http://www.scipy.org/Cookbook/`matplotlib`/Show_colormaps" |
| 874 | + "Colormaps and contour figures are useful for plotting functions of two variables. A colormap will be used to encode one dimension of the data in most of these functions. There are a number of predefined colormaps. It is relatively straightforward to define custom colormaps. For a list of pre-defined colormaps, see: http://www.scipy.org/Cookbook/matplotlib/Show_colormaps" |
828 | 875 | ] |
829 | 876 | }, |
830 | 877 | { |
|
862 | 909 | "cell_type": "markdown", |
863 | 910 | "metadata": {}, |
864 | 911 | "source": [ |
865 | | - "### pcolor" |
| 912 | + "### pcolormesh" |
866 | 913 | ] |
867 | 914 | }, |
868 | 915 | { |
|
873 | 920 | "source": [ |
874 | 921 | "fig, ax = plt.subplots()\n", |
875 | 922 | "\n", |
876 | | - "p = ax.pcolor(\n", |
877 | | - " X / (2 * np.pi),\n", |
878 | | - " Y / (2 * np.pi),\n", |
| 923 | + "p = ax.pcolormesh(\n", |
| 924 | + " X / (2 * np.pi), # corners of quadrilaterals\n", |
| 925 | + " Y / (2 * np.pi), # corners of quadrilaterals\n", |
879 | 926 | " Z,\n", |
880 | | - " cmap=plt.cm.RdBu,\n", |
| 927 | + " cmap='viridis',\n", |
881 | 928 | " vmin=abs(Z).min(),\n", |
882 | 929 | " vmax=abs(Z).max(),\n", |
883 | 930 | ")\n", |
|
899 | 946 | "source": [ |
900 | 947 | "fig, ax = plt.subplots()\n", |
901 | 948 | "\n", |
902 | | - "im = plt.imshow(\n", |
| 949 | + "im = ax.imshow(\n", |
903 | 950 | " Z,\n", |
904 | 951 | " cmap=plt.cm.RdBu,\n", |
905 | 952 | " vmin=abs(Z).min(),\n", |
906 | 953 | " vmax=abs(Z).max(),\n", |
907 | 954 | " extent=[0, 1, 0, 1],\n", |
| 955 | + " interpolation=\"bilinear\",\n", |
908 | 956 | ")\n", |
909 | | - "im.set_interpolation(\"bilinear\")\n", |
910 | 957 | "\n", |
911 | 958 | "cb = fig.colorbar(im, ax=ax)" |
912 | 959 | ] |
|
1121 | 1168 | " ax.plot(\n", |
1122 | 1169 | " xx, yy, lw=0, marker=\"o\", ms=4, mfc=\"red\", mec=\"red\", clip_on=False\n", |
1123 | 1170 | " )\n", |
1124 | | - "\n", |
| 1171 | + " \n", |
| 1172 | + " ax.set_title(f'Page {idx + 1} - {xx:6.2f} $\\pi$')\n", |
1125 | 1173 | " pdf.savefig() # saves the current figure into a pdf page\n", |
1126 | 1174 | " plt.close()" |
1127 | 1175 | ] |
1128 | | - }, |
1129 | | - { |
1130 | | - "cell_type": "markdown", |
1131 | | - "metadata": {}, |
1132 | | - "source": [ |
1133 | | - "\n", |
1134 | | - "\n" |
1135 | | - ] |
1136 | 1176 | } |
1137 | 1177 | ], |
1138 | 1178 | "metadata": { |
1139 | 1179 | "kernelspec": { |
1140 | | - "display_name": "pyclass", |
| 1180 | + "display_name": "lisus_p2", |
1141 | 1181 | "language": "python", |
1142 | 1182 | "name": "python3" |
1143 | 1183 | }, |
|
1151 | 1191 | "name": "python", |
1152 | 1192 | "nbconvert_exporter": "python", |
1153 | 1193 | "pygments_lexer": "ipython3", |
1154 | | - "version": "3.11.7" |
| 1194 | + "version": "3.11.10" |
1155 | 1195 | } |
1156 | 1196 | }, |
1157 | 1197 | "nbformat": 4, |
|
0 commit comments