plotutils

Small plotting-related utility functions.

Three new matplotlib styles are added to the user library upon import:

  • dragons
  • talk
  • white_background

These can be combined as required.

Almost all of the rest of this code is shamelessly copied verbatim from the excellent seaborn plotting package.

dragons.plotutils.axlabel(xlabel, ylabel, ax=None, **kwargs)[source]

Grab current axis and label it.

dragons.plotutils.blend_palette(colors, n_colors=6, as_cmap=False)[source]

Make a palette that blends between a list of colors.

Parameters:
colors : sequence of matplotlib colors
hex, rgb-tuple, or html color name
n_colors : int, optional
number of colors in the palette
as_cmap : bool, optional
if True, return as a matplotlib colormap instead of list
Returns:
palette : list or colormap
dragons.plotutils.color_palette(name=None, n_colors=6, desat=None)[source]

Return matplotlib color codes for a given palette.

Availible seaborn palette names:
deep, muted, bright, pastel, dark, colorblind
Other options:
hls, husl, any matplotlib palette
Params:
name : None, string, or list-ish
name of palette or None to return current color list. if list-ish (i.e. arrays work too), input colors are used but possibly desaturated
n_colors : int
number of colors in the palette
desat : float
desaturation factor for each color
Returns:
palette : list of colors
color palette
dragons.plotutils.dark_palette(color, n_colors=6, reverse=False, as_cmap=False)[source]

Make a palette that blends from a deep gray to color.

Parameters:
color : matplotlib color
hex, rgb-tuple, or html color name
n_colors : int, optional
number of colors in the palette
reverse : bool, optional
if True, reverse the direction of the blend
as_cmap : bool, optional
if True, return as a matplotlib colormap instead of list
Returns:
palette : list or colormap
dragons.plotutils.density_contour(xdata, ydata, bins, ax, label=True, clabel_kwargs={}, **contour_kwargs)[source]

Create a density contour plot.

Code modified from: https://gist.github.com/adrn/3993992#file-density_contour-py

Args:

xdata : ndarray

ydata : ndarray

bins : int or list
Number of bins [nbins_x, nbins_y]. If int then nbins_x=nbins_y=nbins.
ax : matplotlib.axes.AxesSubplot
Axis to draw contours on
Kwargs:
label : bool
Draw labels on the contours? (default: True)
clabel_kwargs : dict
kwargs to be passed to pyplot.clabel() (default: {})
\*\*contour_kwargs : dict
kwargs to be passed to pyplot.contour()
Returns:
contour : matplotlib.contour.QuadContourSet
dragons.plotutils.desaturate(color, prop)[source]

Decrease the saturation channel of a color by some percent.

Parameters:
color : matplotlib color
hex, rgb-tuple, or html color name
prop : float
saturation channel of color will be multiplied by this value
Returns:
new_color : rgb tuple
desaturated color code in RGB tuple representation
dragons.plotutils.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False)[source]

Remove the top and right spines from plot(s).

fig : matplotlib figure
figure to despine all axes of, default uses current figure
ax : matplotlib axes
specific axes object to despine
top, right, left, bottom : boolean
if True, remove that spine
dragons.plotutils.hls_palette(n_colors=6, h=0.01, l=0.6, s=0.65)[source]

Get a set of evenly spaced colors in HLS hue space.

h, l, and s should be between 0 and 1

Parameters:
n_colors : int
number of colors in the palette
h : float
first hue
l : float
lightness
s : float
saturation
Returns:
palette : list of tuples
color palette
dragons.plotutils.husl_palette(n_colors=6, h=0.01, s=0.9, l=0.65)[source]

Get a set of evenly spaced colors in HUSL hue space.

h, s, and l should be between 0 and 1

Parameters:
n_colors : int
number of colors in the palette
h : float
first hue
s : float
saturation
l : float
lightness
Returns:
palette : list of tuples
color palette
dragons.plotutils.init_style(context='default', theme='default')[source]

DEPRECIATED - Initialises the dragons plotutils plotting style.

dragons.plotutils.mpl_palette(name, n_colors=6)[source]

Return discrete colors from a matplotlib palette.

Note that this handles the qualitative colorbrewer palettes properly, although if you ask for more colors than a particular qualitative palette can provide you will fewer than you are expecting.

Parameters:
name : string
name of the palette
n_colors : int
number of colors in the palette
Returns:
palette : list of tuples
palette colors in r, g, b format
dragons.plotutils.saturate(color)[source]

Return a fully saturated color with the same hue.

Parameters:
color : matplotlib color
hex, rgb-tuple, or html color name
Returns:
new_color : rgb tuple
saturated color code in RGB tuple representation
dragons.plotutils.set_hls_values(color, h=None, l=None, s=None)[source]

Independently manipulate the h, l, or s channels of a color.

Parameters:
color : matplotlib color
hex, rgb-tuple, or html color name
h, l, s : floats between 0 and 1, or None
new values for each channel in hls space
Returns:
new_color : rgb tuple
new color code in RGB tuple representation