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?
A “wide-form” Data Frame helps to maintain each numeric column which can be plotted on the graph. It is possible to use NumPy or Python objects, but pandas objects are preferable because the associated names will be used to annotate the axes. Parameters: x, y, hue: Inputs for plotting long-form data.
A violin plot visualizes a distribution of quantitative values as a continuous approximation of the probability density function, computed using kernel density estimation (KDE). The densities are additionally annotated with the median value and interquartile range, shown as black lines.
A violin plot is a hybrid of a box plot and a kernel density plot, which shows peaks in the data. It is used to visualize the distribution of numerical data. Unlike a box plot that can only show summary statistics, violin plots depict summary statistics and the density of each variable.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With