Skip to content

Commit 97dcf09

Browse files
committed
Add quantile line aesthetics to geom_violin
Adds quantile_color, quantile_size, and quantile_linetype parameters so the quantile lines drawn by draw_quantiles can be styled independently of the violin outline. closes #1050
1 parent 53d1967 commit 97dcf09

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

plotnine/geoms/geom_violin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class geom_violin(geom):
4545
'left-right' # Alternate (left first) half violins by the group
4646
'right-left' # Alternate (right first) half violins by the group
4747
```
48+
quantile_color : str | tuple, default=None
49+
Color of the quantile lines.
50+
quantile_size : str, default="o"
51+
The linewidth of the quantile lines.
52+
quantile_linetype : float, default=1.5
53+
The linetype of the quantile lines.
4854
4955
See Also
5056
--------
@@ -64,6 +70,10 @@ class geom_violin(geom):
6470
"stat": "ydensity",
6571
"position": "dodge",
6672
"draw_quantiles": None,
73+
"quantile_color": None,
74+
"quantile_colour": None,
75+
"quantile_size": None,
76+
"quantile_linetype": None,
6777
"style": "full",
6878
"scale": "area",
6979
"trim": True,
@@ -175,6 +185,8 @@ def draw_panel(
175185
)
176186

177187
if quantiles is not None:
188+
from plotnine.mapping._atomic import broadcast_ae_value
189+
178190
# Get dataframe with quantile segments and that
179191
# with aesthetics then put them together
180192
# Each quantile segment is defined by 2 points and
@@ -187,6 +199,19 @@ def draw_panel(
187199
[make_quantile_df(df, quantiles), aes_df], axis=1
188200
)
189201

202+
color = params["quantile_colour"] or params["quantile_color"]
203+
n = len(segment_df)
204+
if color:
205+
segment_df["color"] = broadcast_ae_value(color, "color", n)
206+
if linetype := params["quantile_linetype"]:
207+
segment_df["linetype"] = broadcast_ae_value(
208+
linetype,
209+
"linetype",
210+
n,
211+
)
212+
if size := params["quantile_size"]:
213+
segment_df["size"] = size
214+
190215
# plot quantile segments
191216
geom_path.draw_group(
192217
segment_df,
22.6 KB
Loading

tests/test_geom_violin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,15 @@ def test_overlap():
140140
)
141141

142142
assert p == "overlap"
143+
144+
145+
def test_quantile_aesthetics():
146+
p = ggplot(data, aes("x")) + geom_violin(
147+
aes(y="y"),
148+
size=0.5,
149+
draw_quantiles=[0.25, 0.5, 0.75],
150+
quantile_size=1,
151+
quantile_color=(0.9, 0.2, 0.8),
152+
quantile_linetype=(0, (4, 4, 1, 4)),
153+
)
154+
assert p == "quantile_aesthetics"

0 commit comments

Comments
 (0)