.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/demo_ribbonbox.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_demo_ribbonbox.py: ==================== Demo of SVG bar chart ==================== .. GENERATED FROM PYTHON SOURCE LINES 6-126 .. image-sg:: /examples/images/sphx_glr_demo_ribbonbox_001.png :alt: demo ribbonbox :srcset: /examples/images/sphx_glr_demo_ribbonbox_001.png :class: sphx-glr-single-img .. code-block:: Python # Let's try to read gift-pack-svgrepo-com2.svg # https://www.svgrepo.com/svg/368437/gift-pack xmlstring = b""" gift-pack """ import numpy as np import matplotlib.pyplot as plt import mpl_visual_context.patheffects as pe from mpl_simple_svg_parser import SVGMplPathIterator, get_paths_extents from mpl_pe_fancy_bar import PathsStretcher from mpl_pe_fancy_bar.paths_stretcher import ( get_normalizing_transform, ) import matplotlib.pyplot as plt def get_ribbonbox_drawer(): svg = SVGMplPathIterator(xmlstring) paths = list(svg.iter_mpl_path_patch_prop()) 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 == 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, height-0.3, y_thresh=0.4, height_lim=0.4, fn_get_color=get_color) return ribbonbox_drawer 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_ribbonbox_drawer() # from mpl_visual_context.patheffects_path import BarTransformFromFunc from mpl_pe_fancy_bar import BarToMultiplePaths #BarTransformFromFunc pe1 = BarToMultiplePaths(drawer) for p in bars: # for p, perf in zip(bars, performance): p.set_path_effects([pe1.path_only() | pe.AlphaGradient("0.2 ^ 1.0"), pe.FillColor(None) | pe1, ]) # ax.set_ylim(0, 2.8) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.363 seconds) .. _sphx_glr_download_examples_demo_ribbonbox.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_ribbonbox.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_ribbonbox.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_ribbonbox.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_