statsmodels.graphics.boxplots.violinplot(data, ax=None, labels=None, positions=None, side='both', show_boxplot=True, plot_opts={})
[source]
Make a violin plot of each dataset in the data
sequence.
A violin plot is a boxplot combined with a kernel density estimate of the probability density function per point.
Parameters: |
|
---|---|
Returns: |
fig – If |
Return type: |
Matplotlib figure instance |
See also
beanplot
violinplot
.matplotlib.pyplot.boxplot
The appearance of violins can be customized with plot_opts
. If customization of boxplot elements is required, set show_boxplot
to False and plot it on top of the violins by calling the Matplotlib boxplot
function directly. For example:
violinplot(data, ax=ax, show_boxplot=False) ax.boxplot(data, sym='cv', whis=2.5)
It can happen that the axis labels or tick labels fall outside the plot area, especially with rotated labels on the horizontal axis. With Matplotlib 1.1 or higher, this can easily be fixed by calling ax.tight_layout()
. With older Matplotlib one has to use plt.rc
or plt.rcParams
to fix this, for example:
plt.rc('figure.subplot', bottom=0.25) violinplot(data, ax=ax)
J.L. Hintze and R.D. Nelson, “Violin Plots: A Box Plot-Density Trace Synergism”, The American Statistician, Vol. 52, pp.181-84, 1998.
We use the American National Election Survey 1996 dataset, which has Party Identification of respondents as independent variable and (among other data) age as dependent variable.
>>> data = sm.datasets.anes96.load_pandas() >>> party_ID = np.arange(7) >>> labels = ["Strong Democrat", "Weak Democrat", "Independent-Democrat", ... "Independent-Indpendent", "Independent-Republican", ... "Weak Republican", "Strong Republican"]
Group age by party ID, and create a violin plot with it:
>>> plt.rcParams['figure.subplot.bottom'] = 0.23 # keep labels visible >>> age = [data.exog['age'][data.endog == id] for id in party_ID] >>> fig = plt.figure() >>> ax = fig.add_subplot(111) >>> sm.graphics.violinplot(age, ax=ax, labels=labels, ... plot_opts={'cutoff_val':5, 'cutoff_type':'abs', ... 'label_fontsize':'small', ... 'label_rotation':30}) >>> ax.set_xlabel("Party identification of respondent.") >>> ax.set_ylabel("Age") >>> plt.show()
(Source code, png, hires.png, pdf)
© 2009–2012 Statsmodels Developers
© 2006–2008 Scipy Developers
© 2006 Jonathan E. Taylor
Licensed under the 3-clause BSD License.
http://www.statsmodels.org/stable/generated/statsmodels.graphics.boxplots.violinplot.html