Welcome to kwplot’s documentation!

KWPlot - The Kitware Plot Module

This module is a small wrapper around matplotlib and seaborn that simplifies develoepr workflow when working with code that might be run in IPython or in a script. This is primarilly handled by the kwplot.auto_backends module, which exposes the functions: kwplot.autompl(), kwplot.autoplt(), and kwplot.autosns() for auto-initialization of matplotlib, pyplot, and seaborn.

A very common anti-pattern in developer code is importing matplotlib.pyplot at the top level of your module. This is a mistake because importing pyplot has side-effects which can cause problems if executed at a module level (i.e. they happen at import time! Anyone using your library will have to deal with these consequences )

To mitigate this we recommend only using pyplot inside of the scope of the functions that need it.

Importing kwplot itself has no import-time side effects, so it is safe to put it as a module level import, however, plotting is often an optional feature of any library, so we still recomend putting that code inside the functions that need it.

The general code flow looks like this, inside your function run:

import kwplot

# Pyplot is now initialized do matplotlib or pyplot stuff

This checks if you are running interactively in IPython, if so try to use a Qt backend. If not, then try to use a headless Agg backend.

You can also do

import kwplot
# These also call autompl in the backend, and return either the seaborn or
# pyplot modules, so you dont have to import them in your code. When
# running seaborn, this will also call ``sns.set()`` for you.
sns = kwplot.autosns()
plt = kwplot.autoplt()

In addition to this auto-backend feature, kwplot also exposes useful helper methods for common drawing operations.

There is also a small CLI that can be used to view multispectral or uint16 images.

Indices and tables