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

.. only:: html

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

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

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

.. _sphx_glr_usage_examples_graph_plot_custom_font_graph.py:

Custom Font
=======================================

Use ``font`` and ``font_color`` to style the dimension labels in a graph view. This example uses
Matplotlib's bundled bold monospace font, avoiding a platform-specific font path.

.. GENERATED FROM PYTHON SOURCE LINES 7-48



.. image-sg:: /usage_examples/graph/images/sphx_glr_plot_custom_font_graph_001.png
   :alt: plot custom font graph
   :srcset: /usage_examples/graph/images/sphx_glr_plot_custom_font_graph_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import matplotlib.pyplot as plt
    import visualtorch
    from matplotlib import font_manager
    from PIL import ImageFont
    from torch import nn

    # Fall back to Pillow's default font if the requested TrueType font is unavailable.
    font_properties = font_manager.FontProperties(family="DejaVu Sans Mono", weight="bold")
    try:
        font_path = font_manager.findfont(font_properties, fallback_to_default=False)
        custom_font = ImageFont.truetype(font_path, 16)
    except (OSError, ValueError):
        custom_font = ImageFont.load_default()

    model = nn.Sequential(
        nn.Conv2d(3, 8, kernel_size=3, padding=1),
        nn.ReLU(),
        nn.MaxPool2d(2, 2),
        nn.Conv2d(8, 16, kernel_size=3, padding=1),
        nn.ReLU(),
    )

    input_shape = (1, 3, 64, 64)
    img = visualtorch.render(
        model,
        input_shape=input_shape,
        style="graph",
        layer_spacing=120,
        show_neurons=False,
        show_dimension=True,
        font=custom_font,
        font_color="#1F4E79",
    )

    dpi = 150  # rendered at 2x this in the final doc build (savefig.dpi=300 in conf.py)
    plt.figure(figsize=(img.width / dpi, img.height / dpi), dpi=dpi)
    plt.imshow(img)
    plt.axis("off")
    plt.tight_layout()
    plt.show()


.. _sphx_glr_download_usage_examples_graph_plot_custom_font_graph.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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