Skip to content

Commit c5fbae9

Browse files
author
Niru Maheswaranathan
committed
Adds violinplot.
1 parent b771b32 commit c5fbae9

3 files changed

Lines changed: 78 additions & 9 deletions

File tree

jetplot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Jetpack is a set of useful utility functions for scientific python."""
22

3-
__version__ = "0.6.0"
3+
__version__ = "0.6.2"
44

55
from . import colors as c, typing
66
from .chart_utils import *

jetplot/images.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def cmat(
134134
ax.set_xticks(np.arange(num_cols))
135135
ax.set_xticklabels(labels, rotation=90, fontsize=label_fontsize)
136136
ax.set_yticks(np.arange(num_rows))
137-
ax.set_yticklabels(labels, label_fontsize=label_fontsize)
137+
ax.set_yticklabels(labels, fontsize=label_fontsize)
138138

139139
ax.xaxis.set_minor_locator(FixedLocator(np.arange(num_cols) - 0.5))
140140
ax.yaxis.set_minor_locator(FixedLocator(np.arange(num_rows) - 0.5))

jetplot/plots.py

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""Common plots."""
22

33
import numpy as np
4-
from scipy.stats import gaussian_kde
5-
from sklearn.covariance import EmpiricalCovariance, MinCovDet
6-
74
from matplotlib.patches import Ellipse
85
from matplotlib.transforms import Affine2D
6+
from scipy.stats import gaussian_kde
7+
from sklearn.covariance import EmpiricalCovariance, MinCovDet
98

109
from .chart_utils import figwrapper, nospines, plotwrapper
11-
from .colors import cmap_colors
10+
from .colors import cmap_colors, neutral
1211
from .typing import Color
1312

1413
__all__ = [
1514
"hist",
1615
"hist2d",
1716
"errorplot",
17+
"violinplot",
1818
"bar",
1919
"lines",
2020
"waterfall",
@@ -23,6 +23,67 @@
2323
]
2424

2525

26+
@plotwrapper
27+
def violinplot(
28+
data,
29+
xs,
30+
fc=neutral[3],
31+
ec=neutral[9],
32+
mc=neutral[1],
33+
showmedians=True,
34+
showmeans=False,
35+
showquartiles=True,
36+
**kwargs,
37+
):
38+
_ = kwargs.pop("fig")
39+
ax = kwargs.pop("ax")
40+
41+
data = np.atleast_2d(data).T
42+
43+
if isinstance(xs, float) or isinstance(xs, int):
44+
xs = [
45+
xs,
46+
]
47+
48+
parts = ax.violinplot(
49+
data, positions=xs, showmeans=False, showmedians=False, showextrema=False
50+
)
51+
52+
for pc in parts["bodies"]:
53+
pc.set_facecolor(fc)
54+
pc.set_edgecolor(ec)
55+
pc.set_alpha(1.0)
56+
57+
q1, medians, q3 = np.percentile(data, [25, 50, 75], axis=0)
58+
59+
ax.vlines(
60+
xs,
61+
np.min(data, axis=0),
62+
np.max(data, axis=0),
63+
color=ec,
64+
linestyle="-",
65+
lw=1,
66+
zorder=10,
67+
label="Extrema",
68+
)
69+
70+
if showquartiles:
71+
ax.vlines(xs, q1, q3, color=ec, linestyle="-", lw=5, zorder=5)
72+
73+
if showmedians:
74+
ax.scatter(xs, medians, marker="o", color=mc, s=15, zorder=20)
75+
76+
if showmeans:
77+
ax.scatter(
78+
xs,
79+
np.mean(data, axis=0),
80+
marker="s",
81+
color=mc,
82+
s=15,
83+
zorder=20,
84+
)
85+
86+
2687
@plotwrapper
2788
def hist(*args, **kwargs):
2889
"""Wrapper for matplotlib.hist function."""
@@ -81,7 +142,7 @@ def errorplot(
81142
err_color: Color = "#cccccc",
82143
alpha_fill=1.0,
83144
clip_on=True,
84-
**kwargs
145+
**kwargs,
85146
):
86147
"""Plot a line with error bars."""
87148
ax = kwargs["ax"]
@@ -129,7 +190,15 @@ def errorplot(
129190

130191
@plotwrapper
131192
def bar(
132-
labels, data, color="#888888", width=0.7, offset=0.0, err=None, capsize=5, capthick=2, **kwargs
193+
labels,
194+
data,
195+
color="#888888",
196+
width=0.7,
197+
offset=0.0,
198+
err=None,
199+
capsize=5,
200+
capthick=2,
201+
**kwargs,
133202
):
134203
"""Bar chart.
135204
@@ -279,7 +348,7 @@ def ellipse(x, y, n_std=3.0, facecolor="none", estimator="empirical", **kwargs):
279348
width=ell_radius_x * 2,
280349
height=ell_radius_y * 2,
281350
facecolor=facecolor,
282-
**kwargs
351+
**kwargs,
283352
)
284353

285354
# Calculating the stdandard deviation of x from

0 commit comments

Comments
 (0)