.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/plot_04_sierpinski_triangle.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_examples_plot_04_sierpinski_triangle.py: Sierpiński Triangle With Recursive Views ======================================== Matplotview's views support recursive drawing of other views and themselves to a configurable depth. This feature allows matplotview to be used to generate fractals, such as a sierpiński triangle as shown in the following example. .. GENERATED FROM PYTHON SOURCE LINES 9-49 .. image-sg:: /examples/images/sphx_glr_plot_04_sierpinski_triangle_001.png :alt: plot 04 sierpinski triangle :srcset: /examples/images/sphx_glr_plot_04_sierpinski_triangle_001.png :class: sphx-glr-single-img .. code-block:: default :lineno-start: 10 import matplotlib.pyplot as plt import matplotview as mpv from matplotlib.patches import PathPatch from matplotlib.path import Path from matplotlib.transforms import Affine2D # We'll plot a white upside down triangle inside of black one, and then use # 3 views to draw all the rest of the recursions of the sierpiński triangle. outside_color = "black" inner_color = "white" t = Affine2D().scale(-0.5) outer_triangle = Path.unit_regular_polygon(3) inner_triangle = t.transform_path(outer_triangle) b = outer_triangle.get_extents() fig, ax = plt.subplots(1) ax.set_aspect(1) ax.add_patch(PathPatch(outer_triangle, fc=outside_color, ec=[0] * 4)) ax.add_patch(PathPatch(inner_triangle, fc=inner_color, ec=[0] * 4)) ax.set_xlim(b.x0, b.x1) ax.set_ylim(b.y0, b.y1) ax_locs = [ [0, 0, 0.5, 0.5], [0.5, 0, 0.5, 0.5], [0.25, 0.5, 0.5, 0.5] ] for loc in ax_locs: # Here we limit the render depth to 6 levels in total for each zoom view.... inax = mpv.inset_zoom_axes(ax, loc, render_depth=6) inax.set_xlim(b.x0, b.x1) inax.set_ylim(b.y0, b.y1) inax.axis("off") inax.patch.set_visible(False) fig.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.346 seconds) .. _sphx_glr_download_examples_plot_04_sierpinski_triangle.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_sierpinski_triangle.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_sierpinski_triangle.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_