.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/pencil_notebook.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_pencil_notebook.py: ==================== Another Demo of SVG bar chart ==================== .. GENERATED FROM PYTHON SOURCE LINES 6-142 .. image-sg:: /examples/images/sphx_glr_pencil_notebook_001.png :alt: pencil notebook :srcset: /examples/images/sphx_glr_pencil_notebook_001.png :class: sphx-glr-single-img .. code-block:: Python from matplotlib.transforms import Affine2D xmlstring = b""" """ from mpl_simple_svg_parser import SVGMplPathIterator, get_paths_extents from mpl_pe_fancy_bar import ( PathsStretcher, # BarTransformFromFunc, BarToMultiplePaths, ) from mpl_pe_fancy_bar.paths_stretcher import ( get_normalizing_transform, show_paths_with_gc_prop, show_paths_with_patch_prop ) import matplotlib.pyplot as plt if False: svg = SVGMplPathIterator(xmlstring) paths = list(svg.iter_mpl_path_patch_prop()) fig, ax = plt.subplots(1, 1, num=1, clear=True) ax.set_aspect(True) show_paths_with_patch_prop(ax, paths) tr_rotate = Affine2D().rotate_deg(-135) paths = [(tr_rotate.transform_path(p), _) for (p, _) in paths] fig, ax = plt.subplots(1, 1, num=2, clear=True) ax.set_aspect(True) show_paths_with_patch_prop(ax, paths, show_i=True) bb = get_paths_extents([p for p, _ in paths]) tr, height = get_normalizing_transform(bb) paths = [(tr.transform_path(p), _) for (p, _) in paths] fig, ax = plt.subplots(1, 1, num=2, clear=True) ax.set_aspect(True) show_paths_with_patch_prop(ax, paths, show_i=True) plt.show() def get_pencil_drawer(): svg = SVGMplPathIterator(xmlstring) paths = list(svg.iter_mpl_path_patch_prop()) tr_rotate = Affine2D().rotate_deg(-135) paths = [(tr_rotate.transform_path(p), _) for (p, _) in paths] bb = get_paths_extents([p for p, _ in paths]) tr, height = get_normalizing_transform(bb) paths = [(tr.transform_path(p), _) for (p, _) in paths] def get_color(i, p, fc, ec, fc_new, ec_new): if i in [0]: return fc_new, ec return fc, ec # we use height of the box without the ribbon part at the top. ribbonbox_drawer = PathsStretcher(paths[:-3] + paths[-1:], height, y_thresh=2.7, height_lim=2.1, fn_get_color=get_color, low_height_mode=None, paths_toggle=paths[-3:-1], height_toggle=3.5) return ribbonbox_drawer if False: ribbonbox_drawer = get_pencil_drawer() paths = ribbonbox_drawer(3.6, "y") fig, ax = plt.subplots(1, 1, num=5, clear=True) ax.set_aspect(True) show_paths_with_gc_prop(ax, paths) plt.show() if True: import numpy as np import matplotlib.pyplot as plt import mpl_visual_context.patheffects as pe np.random.seed(19680) # Example data n = 7 x_pos = np.arange(n) performance = 5 * np.random.rand(n) colors = [f"C{i}" for i in range(n)] fig, ax = plt.subplots(1, 1, num=6, clear=True) bars = ax.bar(x_pos, performance, align='center', alpha=0.7, color=colors) drawer = get_pencil_drawer() pe1 = BarToMultiplePaths(drawer) for p, perf in zip(bars, performance): p.set_path_effects([pe1.path_only() | pe.ClipRect(ax, top=perf) | pe.AlphaGradient("up"), pe.ClipPathSelf() | pe.FillColor(None) | pe1, ]) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.215 seconds) .. _sphx_glr_download_examples_pencil_notebook.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pencil_notebook.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pencil_notebook.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_