.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "extensions/sphinx-gallery/plot_03_subplots.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_extensions_sphinx-gallery_plot_03_subplots.py: Subplot Layouts =============== This example shows how to create figures with multiple subplots. Subplots are useful for comparing related visualizations side by side. .. GENERATED FROM PYTHON SOURCE LINES 8-12 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 13-17 Grid of Subplots ---------------- A 2x2 grid showing different mathematical functions. .. GENERATED FROM PYTHON SOURCE LINES 17-49 .. code-block:: Python fig, axes = plt.subplots(2, 2, figsize=(10, 8)) x = np.linspace(0, 2 * np.pi, 100) # Sine axes[0, 0].plot(x, np.sin(x), "b-", linewidth=2) axes[0, 0].set_title("Sine") axes[0, 0].grid(True, alpha=0.3) # Cosine axes[0, 1].plot(x, np.cos(x), "r-", linewidth=2) axes[0, 1].set_title("Cosine") axes[0, 1].grid(True, alpha=0.3) # Tangent (clipped) y_tan = np.tan(x) y_tan[np.abs(y_tan) > 5] = np.nan axes[1, 0].plot(x, y_tan, "g-", linewidth=2) axes[1, 0].set_title("Tangent") axes[1, 0].set_ylim(-5, 5) axes[1, 0].grid(True, alpha=0.3) # Combined axes[1, 1].plot(x, np.sin(x), "b-", label="sin", linewidth=2) axes[1, 1].plot(x, np.cos(x), "r-", label="cos", linewidth=2) axes[1, 1].set_title("Combined") axes[1, 1].legend() axes[1, 1].grid(True, alpha=0.3) plt.tight_layout() plt.show() .. image-sg:: /extensions/sphinx-gallery/images/sphx_glr_plot_03_subplots_001.png :alt: Sine, Cosine, Tangent, Combined :srcset: /extensions/sphinx-gallery/images/sphx_glr_plot_03_subplots_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.249 seconds) .. _sphx_glr_download_extensions_sphinx-gallery_plot_03_subplots.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_subplots.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_subplots.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_subplots.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_