10000 pass different colors (+ additional line/polycollection kwargs) to vi… · matplotlib/matplotlib@34bc624 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34bc624

Browse files
committed
pass different colors (+ additional line/polycollection kwargs) to violinplot
1 parent ca10a34 commit 34bc624

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6870,7 +6870,8 @@ def matshow(self, Z, **kwargs):
68706870

68716871
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
68726872
showmeans=False, showextrema=True, showmedians=False,
6873-
points=100, bw_method=None):
6873+
points=100, bw_method=None, color='y', line_kw={},
6874+
**fill_kw):
68746875
"""Make a violin plot.
68756876
68766877
Call signature::
@@ -6922,6 +6923,18 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
69226923
callable, it should take a `GaussianKDE` instance as its only
69236924
parameter and return a scalar. If None (default), 'scott' is used.
69246925
6926+
color : color or array_like of colors, optional, default: 'y'
6927+
Color spec or sequence of color specs, one per violin plot.
6928+
6929+
line_kw : dict
6930+
:class:`matplotlib.collections.LineCollection` properties, applied
6931+
to the lines representing the means, extrema and/or medians.
6932+
6933+
fill_kw : dict
6934+
:class:`matplotlib.collections.PolyCollection` properties, applied to
6935+
the filled area of the plot(s). Note that any keyword arguments not
6936+
recognized above will be automatically included here.
6937+
69256938
Returns
69266939
-------
69276940
@@ -6967,16 +6980,19 @@ def _kde_method(X, coords):
69676980
vpstats = cbook.violin_stats(dataset, _kde_method, points=points)
69686981
return self.violin(vpstats, positions=positions, vert=vert,
69696982
widths=widths, showmeans=showmeans,
6970-
showextrema=showextrema, showmedians=showmedians)
6983+
showextrema=showextrema, showmedians=showmedians,
6984+
color=color, line_kw=line_kw, **fill_kw)
69716985

69726986
def violin(self, vpstats, positions=None, vert=True, widths=0.5,
6973-
showmeans=False, showextrema=True, showmedians=False):
6987+
showmeans=False, showextrema=True, showmedians=False,
6988+
color='y', line_kw={}, **fill_kw):
69746989
"""Drawing function for violin plots.
69756990
69766991
Call signature::
69776992
69786993
violin(vpstats, positions=None, vert=True, widths=0.5,
6979-
showmeans=False, showextrema=True, showmedians=False):
6994+
showmeans=False, showextrema=True, showmedians=False,
6995+
colors='y'):
69806996
69816997
Draw a violin plot for each column of `vpstats`. Each filled area
69826998
extends to represent the entire data range, with optional lines at the
@@ -7026,6 +7042,18 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
70267042
showmedians : bool, default = False
70277043
If true, will toggle rendering of the medians.
70287044
7045+
color : color or array_like of colors, optional, default: 'y'
7046+
Color spec or sequence of color specs, one per violin plot.
7047+
7048+
line_kw : dict
7049+
:class:`matplotlib.collections.LineCollection` properties, applied
7050+
to the lines representing the means, extrema and/or medians.
7051+
7052+
fill_kw : dict
7053+
:class:`matplotlib.collections.PolyCollection` properties, applied to
7054+
the filled area of the plot(s). Note that any keyword arguments not
7055+
recognized above will be automatically included here.
7056+
70297057
Returns
70307058
-------
70317059
result : dict
@@ -7089,6 +7117,16 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
70897117
elif len(widths) != N:
70907118
raise ValueError(datashape_message.format("widths"))
70917119

7120+
# Validate colors
7121+
if np.isscalar(color):
7122+
color = [color] * N
7123+
elif len(color) != N:
7124+
raise ValueError(datashape_message.format("color"))
7125+
7126+
# original default values for line color and alpha
7127+
line_color = line_kw.pop('colors', 'r')
7128+
alpha = fill_kw.pop('alpha', 0.3)
7129+
70927130
# Calculate ranges for statistics lines
70937131
pmins = -0.25 * np.array(widths) + positions
70947132
pmaxes = 0.25 * np.array(widths) + positions
@@ -7105,16 +7143,17 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
71057143

71067144
# Render violins
71077145
bodies = []
7108-
for stats, pos, width in zip(vpstats, positions, widths):
7146+
for stats, pos, width, c in zip(vpstats, positions, widths, color):
71097147
# The 0.5 factor reflects the fact that we plot from v-p to
71107148
# v+p
71117149
vals = np.array(stats['vals'])
71127150
vals = 0.5 * width * vals / vals.max()
71137151
bodies += [fill(stats['coords'],
71147152
-vals + pos,
71157153
vals + pos,
7116-
facecolor='y',
7117-
alpha=0.3)]
7154+
facecolor=c,
7155+
alpha=alpha,
7156+
**fill_kw)]
71187157
means.append(stats['mean'])
71197158
mins.append(stats['min'])
71207159
maxes.append(stats['max'])
@@ -7123,20 +7162,22 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
71237162

71247163
# Render means
71257164
if showmeans:
7126-
artists['cmeans'] = perp_lines(means, pmins, pmaxes, colors='r')
7165+
artists['cmeans'] = perp_lines(means, pmins, pmaxes,
7166+
colors=line_color, **line_kw)
71277167

71287168
# Render extrema
71297169
if showextrema:
7130-
artists['cmaxes'] = perp_lines(maxes, pmins, pmaxes, colors='r')
7131-
artists['cmins'] = perp_lines(mins, pmins, pmaxes, colors='r')
7132-
artists['cbars'] = par_lines(positions, mins, maxes, colors='r')
7170+
artists['cmaxes'] = perp_lines(maxes, pmins, pmaxes,
7171+
colors=line_color, **line_kw)
7172+
artists['cmins'] = perp_lines(mins, pmins, pmaxes,
7173+
colors=line_color, **line_kw)
7174+
artists['cbars'] = par_lines(positions, mins, maxes,
7175+
colors=line_color, **line_kw)
71337176

71347177
# Render medians
71357178
if showmedians:
7136-
artists['cmedians'] = perp_lines(medians,
7137-
pmins,
7138-
pmaxes,
7139-
colors='r')
7179+
artists['cmedians'] = perp_lines(medians, pmins, pmaxes,
7180+
colors=line_color, **line_kw)
71407181

71417182
return artists
71427183

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3420,7 +3420,7 @@ def triplot(*args, **kwargs):
34203420
@_autogen_docstring(Axes.violinplot)
34213421
def violinplot(dataset, positions=None, vert=True, widths=0.5, showmeans=False,
34223422
showextrema=True, showmedians=False, points=100, bw_method=None,
3423-
hold=None):
3423+
color='y', line_kw={}, hold=None, **fill_kw):
34243424
ax = gca()
34253425
# allow callers to override the hold state by passing hold=True|False
34263426
washold = ax.ishold()
@@ -3431,7 +3431,8 @@ def violinplot(dataset, positions=None, vert=True, widths=0.5, showmeans=False,
34313431
ret = ax.violinplot(dataset, positions=positions, vert=vert,
34323432
widths=widths, showmeans=showmeans,
34333433
showextrema=showextrema, showmedians=showmedians,
3434-
points=points, bw_method=bw_method)
3434+
points=points, bw_method=bw_method, color=color,
3435+
line_kw=line_kw, **fill_kw)
34353436
draw_if_interactive()
34363437
finally:
34373438
ax.hold(washold)

0 commit comments

Comments
 (0)
0