.. 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. ``mpl.violin`` with linear cmap ----------------------------------------- Make a violin plot with a linear colormap. .. GENERATED FROM PYTHON SOURCE LINES 8-46 .. 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 /Users/cbit/Desktop/repositories/github/python-spare-code/main/examples/matplotlib/plot_main10_violin_cmap.py:42: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead. | .. code-block:: default :lineno-start: 9 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.251 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 `_