.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/units/artist_tests.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. 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_gallery_units_artist_tests.py: ============ Artist tests ============ Test unit support with each of the Matplotlib primitive artist types. The axis handles unit conversions and the artists keep a pointer to their axis parent. You must initialize the artists with the axis instance if you want to use them with unit data, or else they will not know how to convert the units to scalars. .. only:: builder_html This example requires :download:`basic_units.py ` .. GENERATED FROM PYTHON SOURCE LINES 17-68 .. image-sg:: /gallery/units/images/sphx_glr_artist_tests_001.png :alt: Artists with units :srcset: /gallery/units/images/sphx_glr_artist_tests_001.png, /gallery/units/images/sphx_glr_artist_tests_001_2_00x.png 2.00x :class: sphx-glr-single-img .. code-block:: Python import random from basic_units import cm, inch import matplotlib.pyplot as plt import numpy as np import matplotlib.collections as collections import matplotlib.lines as lines import matplotlib.patches as patches import matplotlib.text as text fig, ax = plt.subplots() ax.xaxis.set_units(cm) ax.yaxis.set_units(cm) # Fixing random state for reproducibility np.random.seed(19680801) if 0: # test a line collection # Not supported at present. verts = [] for i in range(10): # a random line segment in inches verts.append(zip(*inch*10*np.random.rand(2, random.randint(2, 15)))) lc = collections.LineCollection(verts, axes=ax) ax.add_collection(lc) # test a plain-ol-line line = lines.Line2D([0*cm, 1.5*cm], [0*cm, 2.5*cm], lw=2, color='black', axes=ax) ax.add_line(line) if 0: # test a patch # Not supported at present. rect = patches.Rectangle((1*cm, 1*cm), width=5*cm, height=2*cm, alpha=0.2, axes=ax) ax.add_patch(rect) t = text.Text(3*cm, 2.5*cm, 'text label', ha='left', va='bottom', axes=ax) ax.add_artist(t) ax.set_xlim(-1*cm, 10*cm) ax.set_ylim(-1*cm, 10*cm) # ax.xaxis.set_units(inch) ax.grid(True) ax.set_title("Artists with units") plt.show() .. _sphx_glr_download_gallery_units_artist_tests.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: artist_tests.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: artist_tests.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_