Skip to content

Commit

Permalink
Adds violinplot.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirum committed Jul 23, 2024
1 parent b771b32 commit c5fbae9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jetplot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Jetpack is a set of useful utility functions for scientific python."""

__version__ = "0.6.0"
__version__ = "0.6.2"

from . import colors as c, typing
from .chart_utils import *
Expand Down
2 changes: 1 addition & 1 deletion jetplot/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def cmat(
ax.set_xticks(np.arange(num_cols))
ax.set_xticklabels(labels, rotation=90, fontsize=label_fontsize)
ax.set_yticks(np.arange(num_rows))
ax.set_yticklabels(labels, label_fontsize=label_fontsize)
ax.set_yticklabels(labels, fontsize=label_fontsize)

ax.xaxis.set_minor_locator(FixedLocator(np.arange(num_cols) - 0.5))
ax.yaxis.set_minor_locator(FixedLocator(np.arange(num_rows) - 0.5))
Expand Down
83 changes: 76 additions & 7 deletions jetplot/plots.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
"""Common plots."""

import numpy as np
from scipy.stats import gaussian_kde
from sklearn.covariance import EmpiricalCovariance, MinCovDet

from matplotlib.patches import Ellipse
from matplotlib.transforms import Affine2D
from scipy.stats import gaussian_kde
from sklearn.covariance import EmpiricalCovariance, MinCovDet

from .chart_utils import figwrapper, nospines, plotwrapper
from .colors import cmap_colors
from .colors import cmap_colors, neutral
from .typing import Color

__all__ = [
"hist",
"hist2d",
"errorplot",
"violinplot",
"bar",
"lines",
"waterfall",
Expand All @@ -23,6 +23,67 @@
]


@plotwrapper
def violinplot(
data,
xs,
fc=neutral[3],
ec=neutral[9],
mc=neutral[1],
showmedians=True,
showmeans=False,
showquartiles=True,
**kwargs,
):
_ = kwargs.pop("fig")
ax = kwargs.pop("ax")

data = np.atleast_2d(data).T

if isinstance(xs, float) or isinstance(xs, int):
xs = [
xs,
]

parts = ax.violinplot(
data, positions=xs, showmeans=False, showmedians=False, showextrema=False
)

for pc in parts["bodies"]:
pc.set_facecolor(fc)
pc.set_edgecolor(ec)
pc.set_alpha(1.0)

q1, medians, q3 = np.percentile(data, [25, 50, 75], axis=0)

ax.vlines(
xs,
np.min(data, axis=0),
np.max(data, axis=0),
color=ec,
linestyle="-",
lw=1,
zorder=10,
label="Extrema",
)

if showquartiles:
ax.vlines(xs, q1, q3, color=ec, linestyle="-", lw=5, zorder=5)

if showmedians:
ax.scatter(xs, medians, marker="o", color=mc, s=15, zorder=20)

if showmeans:
ax.scatter(
xs,
np.mean(data, axis=0),
marker="s",
color=mc,
s=15,
zorder=20,
)


@plotwrapper
def hist(*args, **kwargs):
"""Wrapper for matplotlib.hist function."""
Expand Down Expand Up @@ -81,7 +142,7 @@ def errorplot(
err_color: Color = "#cccccc",
alpha_fill=1.0,
clip_on=True,
**kwargs
**kwargs,
):
"""Plot a line with error bars."""
ax = kwargs["ax"]
Expand Down Expand Up @@ -129,7 +190,15 @@ def errorplot(

@plotwrapper
def bar(
labels, data, color="#888888", width=0.7, offset=0.0, err=None, capsize=5, capthick=2, **kwargs
labels,
data,
color="#888888",
width=0.7,
offset=0.0,
err=None,
capsize=5,
capthick=2,
**kwargs,
):
"""Bar chart.
Expand Down Expand Up @@ -279,7 +348,7 @@ def ellipse(x, y, n_std=3.0, facecolor="none", estimator="empirical", **kwargs):
width=ell_radius_x * 2,
height=ell_radius_y * 2,
facecolor=facecolor,
**kwargs
**kwargs,
)

# Calculating the stdandard deviation of x from
Expand Down

0 comments on commit c5fbae9

Please sign in to comment.