.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples\matplotlib\plot_main10_violin_cmap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_matplotlib_plot_main10_violin_cmap.py: 10. Violin plot with linear colormap ============================================ This script demonstrates a clever technique for creating a violin plot where the fill color is a continuous, linear colormap mapped to the y-axis values. 🎻 Instead of using a simple solid fill, it uses the paths of the violin bodies as clipping masks. An ``imshow`` gradient is then displayed underneath, making it visible only within the violin shapes. A corresponding colorbar is added to complete the visualization. .. GENERATED FROM PYTHON SOURCE LINES 16-56 .. image-sg:: /_examples/matplotlib/images/sphx_glr_plot_main10_violin_cmap_001.png :alt: plot main10 violin cmap :srcset: /_examples/matplotlib/images/sphx_glr_plot_main10_violin_cmap_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none C:\Users\kelda\Desktop\repositories\github\python-spare-code\main\examples\matplotlib\plot_main10_violin_cmap.py:52: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed in 3.11. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()`` or ``pyplot.get_cmap()`` instead. C:\Users\kelda\Desktop\repositories\github\python-spare-code\main\examples\matplotlib\plot_main10_violin_cmap.py:56: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown | .. code-block:: default :lineno-start: 17 # ``mpl.violin`` import matplotlib import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path from matplotlib.patches import PathPatch from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable x = [np.random.normal(loc=i, scale=1, size=(100,)) for i in range(5)] fig, ax = plt.subplots() violins = ax.violinplot(x) ymin, ymax = ax.get_ylim() xmin, xmax = ax.get_xlim() # create a numpy image to use as a gradient Nx,Ny=1,1000 imgArr = np.tile(np.linspace(0,1,Ny), (Nx,1)).T cmap = 'hsv' for violin in violins['bodies']: path = Path(violin.get_paths()[0].vertices) patch = PathPatch(path, facecolor='none', edgecolor='none') ax.add_patch(patch) img = ax.imshow(imgArr, origin="lower", extent=[xmin,xmax,ymin,ymax], aspect="auto", cmap=cmap, clip_path=patch) # colorbar ax_divider = make_axes_locatable(ax) cax = ax_divider.append_axes("right", size="5%", pad="2%") norm = matplotlib.colors.Normalize(vmin=ymin, vmax=ymax) cb = matplotlib.colorbar.ColorbarBase(cax, cmap=matplotlib.cm.get_cmap(cmap), norm=norm, orientation='vertical') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.246 seconds) .. _sphx_glr_download__examples_matplotlib_plot_main10_violin_cmap.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_main10_violin_cmap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_main10_violin_cmap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_