Han
Han

Reputation: 735

matplotlib - making labels for violin plots

I usually make labels for bars in the following manner using parameter 'label' in the method 'bar'.

axes[0].bar(x, y, bar_width, label='abc')
axes[0].legend()

Now I'd like to plot violin plots and make label for each collection as follows, but it doesn't work since 'violinplot' doesn't have the parameter 'label'.

axes[0].violinplot(data1, label='abc1')
axes[1].violinplot(data2, label='abc2')

Can anyone help me out to make a label for each collection?

Upvotes: 20

Views: 25543

Answers (5)

Francisco Sahli
Francisco Sahli

Reputation: 101

There is an even simpler solution than @Ian Hincks code, without using mpatches

import matplotlib.pyplot as plt
import numpy as np

positions = np.arange(3,13,3)
data = np.random.randn(1000, len(positions))
vp1 = plt.violinplot(data, positions) 

positions = np.arange(1, 10, 2)
data = np.random.randn(1000, len(positions)) + positions
vp2 = plt.violinplot(data, positions)

positions = np.arange(2, 11, 1)
data = np.random.randn(1000, len(positions)) + positions ** 2 / 4
vp3 = plt.violinplot(data, positions)

plt.legend([vp1['bodies'][0],vp2['bodies'][0], vp3['bodies'][0]], ['flat', 'linear', 'quadratic'], loc=2)

To use the lines instead of the bodies replace vp1['bodies'][0] by vp1['cbars']

Demo:

violin plot with labels

Upvotes: 10

Amit
Amit

Reputation: 11

A leaner and clear solution can be:

plt.style.use('seaborn')
import matplotlib.pyplot as plt
labels = ['df=9' , 'df=99', 'df=999', 'df=9999', 'N($\mu=0$, $\sigma=2)$']
colors = ['orange', 'lightblue', 'lightgreen', 'yellow', 'green']
def make_violinplot(x, labels, colors):
    parts = plt.violinplot(x, showmeans=True, showmedians=True)
    for body, color in zip(parts['bodies'],colors):
        body.set_facecolor(color)
        parts['cmeans'].set_color('red')
        parts['cmedians'].set_color('blue')
    plt.legend(label[enter image description here][1]s, loc='upper left')
    plt.xticks(np.arange(1, len(labels)+1), labels)
    plt.title("probability density plots t- vs normal distribution")
    plt.rcParams["figure.figsize"] = [8,8]
    return
make_violinplot(t_dists, labels, colors)

Upvotes: 1

Ian Hincks
Ian Hincks

Reputation: 4128

Here is my solution for multple violin plots. Note that it grabs the patch color from the first shaded area of the given violin plot---this could be changed to do something else if there are multiple colors, or you could instead grab the color of the vertical bar with violin["cbars"].get_color().flatten().

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

labels = []
def add_label(violin, label):
    color = violin["bodies"][0].get_facecolor().flatten()
    labels.append((mpatches.Patch(color=color), label))


positions = np.arange(3,13,3)
data = np.random.randn(1000, len(positions))
add_label(plt.violinplot(data, positions), "Flat")    

positions = np.arange(1, 10, 2)
data = np.random.randn(1000, len(positions)) + positions
add_label(plt.violinplot(data, positions), "Linear")

positions = np.arange(2, 11, 1)
data = np.random.randn(1000, len(positions)) + positions ** 2 / 4
add_label(plt.violinplot(data, positions), "Quadratic")

plt.legend(*zip(*labels), loc=2)

enter image description here

Upvotes: 22

merletta
merletta

Reputation: 444

As it was mentioned in comment, some plots in matplotlib don't support legends. Documentation still provides a simple way to add custom legends for them: http://matplotlib.org/users/legend_guide.html#proxy-legend-handles

Main idea : add 'fake' objects, which can be not shown in the plot, then use it to form a handles list for legend method.

    import random
    import numpy as np
    import matplotlib.pyplot as pl
    import matplotlib.patches as mpatches
    from itertools import repeat

    red_patch = mpatches.Patch(color='red')
    # 'fake' invisible object

    pos   = [1, 2, 4, 5, 7, 8]
    label = ['plot 1','plot2','ghi','jkl','mno','pqr']
    data  = [np.random.normal(size=100) for i in pos]

    fake_handles = repeat(red_patch, len(pos))

    pl.figure()
    ax = pl.subplot(111)
    pl.violinplot(data, pos, vert=False)
    ax.legend(fake_handles, label)
    pl.show()

violinplot with custom legend

Upvotes: 10

Bart
Bart

Reputation: 10308

edit: sorry, I now see that you wanted to add a legend, not axis labels...

You can manually set the tick locations and then overwrite their labels:

import numpy as np
import matplotlib.pyplot as pl

pos   = [1, 2, 4, 5, 7, 8]
label = ['abc','def','ghi','jkl','mno','pqr']
data  = [np.random.normal(size=100) for i in pos]

pl.figure()
ax = pl.subplot(111)
pl.violinplot(data, pos, vert=False)
ax.set_yticks(pos)
ax.set_yticklabels(label)

enter image description here

Upvotes: 7

Related Questions