.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_gallery/window_layouts/rect_layout.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download__gallery_window_layouts_rect_layout.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr__gallery_window_layouts_rect_layout.py:


Rect Layout
===========

Create subplots using given rects in absolute pixels.
This example plots two images and their histograms in separate subplots

.. GENERATED FROM PYTHON SOURCE LINES 9-74



.. image-sg:: /_gallery/window_layouts/images/sphx_glr_rect_layout_001.webp
   :alt: rect layout
   :srcset: /_gallery/window_layouts/images/sphx_glr_rect_layout_001.webp
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    /home/runner/work/fastplotlib/fastplotlib/fastplotlib/graphics/_features/_base.py:18: UserWarning: casting float64 array to float32
      warn(f"casting {array.dtype} array to float32")







|

.. code-block:: Python


    # test_example = true

    import numpy as np
    import imageio.v3 as iio
    import fastplotlib as fpl

    # load images
    img1 = iio.imread("imageio:astronaut.png")
    img2 = iio.imread("imageio:wikkie.png")

    # calculate histograms
    hist_1, edges_1 = np.histogram(img1)
    centers_1 = edges_1[:-1] + np.diff(edges_1) / 2

    hist_2, edges_2 = np.histogram(img2)
    centers_2 = edges_2[:-1] + np.diff(edges_2) / 2

    # figure size in pixels
    size = (640, 480)

    # a rect is (x, y, width, height)
    # here it is defined in absolute pixels
    rects = [
        (0, 0, 200, 240),  # for image1
        (0, 240, 200, 240),  # for image2
        (200, 0, 440, 240),  # for image1 histogram
        (200, 240, 440, 240),  # for image2 histogram
    ]

    # create a figure using the rects and size
    # also give each subplot a name
    figure = fpl.Figure(
        rects=rects,
        names=["astronaut image", "wikkie image", "astronaut histogram", "wikkie histogram"],
        size=size
    )

    # add image to the corresponding subplots
    figure["astronaut image"].add_image(img1)
    figure["wikkie image"].add_image(img2)

    # add histogram to the corresponding subplots
    figure["astronaut histogram"].add_line(np.column_stack([centers_1, hist_1]))
    figure["wikkie histogram"].add_line(np.column_stack([centers_2, hist_2]))


    for subplot in figure:
        if "image" in subplot.name:
            # remove axes from image subplots to reduce clutter
            subplot.axes.visible = False
            continue

        # don't maintain aspect ratio for the histogram subplots
        subplot.camera.maintain_aspect = False


    figure.show()


    # NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively
    # please see our docs for using fastplotlib interactively in ipython and jupyter
    if __name__ == "__main__":
        print(__doc__)
        fpl.loop.run()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.859 seconds)


.. _sphx_glr_download__gallery_window_layouts_rect_layout.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: rect_layout.ipynb <rect_layout.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: rect_layout.py <rect_layout.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: rect_layout.zip <rect_layout.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_