DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Create Multiple Violin Plots in Matplotlib

Use one data vector per group with Matplotlib’s violinplot(), then align positions and labels to compare distributions clearly.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pass one data vector per group to Axes.violinplot(), then use matching positions and tick labels to place and identify the violins. For example:

import matplotlib.pyplot as plt

samples = [group_a, group_b, group_c]
positions = [1, 2, 3]

fig, ax = plt.subplots()
parts = ax.violinplot(samples, positions=positions, showmedians=True)
ax.set_xticks(positions, labels=['A', 'B', 'C'])
ax.set_ylabel('Observed value')
ax.set_title('Distribution by group')
plt.show()

Each item in samples should be a one-dimensional vector of observations. Replace the example group variables and labels with your data and category names.

How Matplotlib maps data to violins

Axes.violinplot(dataset, ...) draws one violin for each vector in a sequence. It can also take a two-dimensional array and draw one violin per column; a single one-dimensional array produces one violin. Non-finite and masked values are ignored. See the Axes.violinplot API documentation.

The default positions are 1 through the number of datasets. Set positions when you need different coordinates, and set ticks at those same coordinates so the category names stay aligned.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set up category spacing and labels

For vertical violins, positions are x coordinates; for horizontal violins, they are y coordinates. You can leave gaps to separate groups visually, as in Matplotlib’s violin plot gallery example:

positions = [1, 2, 4, 5, 7, 8]
parts = ax.violinplot(samples, positions=positions, showmedians=True)
ax.set_xticks(positions, labels=['A1', 'A2', 'B1', 'B2', 'C1', 'C2'])

Use one label for every position. For horizontal plots, put the labels on the y axis instead:

positions = [1, 2, 3]
parts = ax.violinplot(
    samples,
    positions=positions,
    orientation='horizontal',
    showmedians=True,
)
ax.set_yticks(positions, labels=['A', 'B', 'C'])
ax.set_xlabel('Observed value')

Use orientation='horizontal' for horizontal violins. The older vert parameter has been deprecated since Matplotlib 3.10, so new code should use orientation. Check the API documentation for the parameters supported by your installed version.

Choose summary marks to display

Matplotlib can overlay summary markers on each violin. By default, extrema are shown, while means and medians are hidden. Enable the marks you need with the corresponding options:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
parts = ax.violinplot(
    samples,
    showmeans=True,
    showextrema=True,
    showmedians=True,
)

Quantiles can also be supplied through the API’s quantiles parameter. These markers describe aspects of the data alongside its density shape; choose them to suit the comparison rather than adding every mark automatically.

Adjust density rendering carefully

A violin’s outline is a kernel density estimate. The bw_method parameter controls its bandwidth, and points controls the number of evaluation points used to draw the estimate. The API accepts 'scott', 'silverman', a float, or a callable for bw_method. Matplotlib’s gallery illustrates different bandwidth and point-count choices, but it does not establish one setting as right for every dataset. Inspect the resulting shape against the observations and use a consistent approach when comparing groups.

Violin width represents density by default, not the number of observations. A wider shape should not be interpreted as a larger sample unless sample size is encoded separately.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Style the returned violins

violinplot() returns a dictionary of collections, including bodies for the filled shapes and collections for summary marks. You can style the bodies after plotting:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for body in parts['bodies']:
    body.set_edgecolor('black')
    body.set_linewidth(1)
    body.set_alpha(0.7)

The official customization example also draws quartiles and whiskers over the violin bodies. Matplotlib 3.11 documentation includes facecolor and linecolor arguments; check your installed version before using these newer arguments. The returned-body styling approach lets you customize the filled shapes through their collection objects.

Choose between raw data and precomputed statistics

Use violinplot() when you have raw sample vectors and want Matplotlib to calculate the density representation. If you already have violin statistics, Axes.violin() accepts dictionaries with coords, vals, mean, median, min, and max, with optional quantiles. See Matplotlib’s violin plot comparison example and the violinplot API.

When a violin plot is useful

A violin plot shows a density trace and can reveal distribution shape. In Matplotlib’s comparison example, box plots identify points beyond 1.5 times the interquartile range as outliers, whereas the violin plots show the full data range. Choose the form that makes the features relevant to your data easiest to inspect; a violin’s shape does not replace an explicit sample-size indicator.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.