Skip to main content

Color palettes and plot themes

Project description

fybdthemes

Project Status ci version

About

This package contains some of my color palettes for usage in Python. One can choose between qualitative, diverging and sequential color palettes depending on the type of variable to be visualized

Installation

To install from PyPI:

pip install fybdthemes

To install the latest GitHub , just call the following on the command line:

pip install git+https://github.com/dirmeier/fybdthemes@<RELEASE>

Usage

fybdthemes works with both matplotlib or seaborn. You can either manually specify colors or provide matplotlib color maps. Below, we briefly demonstrate how to use the package.

import numpy as np
import numpy.random

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

import fybdthemes
from fybdthemes.plot import plot_palette

Set a custom theme for matplotlib:

fybdthemes.set_theme()

For visualization we use the well-known diamonds data:

diamonds = sns.load_dataset("diamonds")

Sequential colors

Sequential colors are usually chosen for continuous variables. The sequential colors here revolve around the following three colors:

plot_palette(fybdthemes.discrete_sequential_colors())

png

In the visualization above we use three discrete colors, but you can also choose more:

plot_palette(fybdthemes.discrete_sequential_colors(10))

png

plot_palette(fybdthemes.discrete_sequential_colors(10, True))

png

Usually for continuous variables, we want a continous scale though:

plot_palette(fybdthemes.continuous_sequential_colors())

png

In a plot, we use the color scheme like this:

_, ax = plt.subplots(figsize=(8, 4))
sns.scatterplot(
    x="carat", y="price",
    hue="price",
    palette=fybdthemes.continuous_sequential_colors(),
    data=diamonds, ax=ax,
    marker="+"
)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
plt.show()

png

Diverging colors

Diverging colors are usually chosen for continuous variables that can deviate in one of two directions relative to some midpoint. The diverging colors here revolve around the following four colors:

plot_palette(fybdthemes.discrete_diverging_colors())

png

As above, you can specify more colors, too, and reverse them:

plot_palette(fybdthemes.discrete_diverging_colors(10, True))

png

Since the visualized variable is usually continuous again, you want a continous scale again:

plot_palette(fybdthemes.continuous_diverging_colors())

png

For plotting, we use the palette as before. In this case the midpoint would be if a diamond has 3 carats.

_, ax = plt.subplots(figsize=(8, 4))
sns.scatterplot(
    x="carat", y="price",
    hue="carat",
    palette=fybdthemes.continuous_diverging_colors(),
    data=diamonds, ax=ax,
    marker="+"
)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()

png

Qualitative colors

Qualitative colors are usually chosen for categorical variables. The qualitative colors palette in this package has the following colors:

plot_palette(fybdthemes.discrete_qualitative_colors())

png

def sqeuclidean_cdist(X):
    X = X.reshape(-1, 1)    
    X2 = np.sum(np.square(X), 1)    
    dist = -2.0 * np.dot(X, np.transpose(X)) + (
        np.reshape(X2, (-1, 1)) + np.reshape(X2, (1, -1))
    )
    return  np.exp(-0.5 * np.clip(dist, 0.0, np.inf))

X = np.linspace(0, 10, 100)
K = sqeuclidean_cdist(X)

You can, for instance, use it like this:

cols = fybdthemes.discrete_qualitative_colors()

_, ax = plt.subplots(figsize=(8, 4))
for i in range(5):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, label=i)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()

png

If you need less colors, manually specificying the colors to avoid a too colorful figure is usually a good idea. For instance, for plots with three lines you could use these colors:

plot_palette(np.array(cols)[[0, 1, 3]])

png

plot_palette(np.array(cols)[[0, 1, 4]])

png

In that case we merely need to specify the indexes of the colors:

idxs = 0, 1, 4
_, ax = plt.subplots(figsize=(8, 4))
for i in idxs:
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, label=i)
ax.legend(title="lines", bbox_to_anchor=(1.2, 0.5))
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()

png

The palette has a maximum of 6 colors. I've chosen to use only 6, because humans are usually not good at congitively processing more then 4-5 colors in a plot, so 6 is a hard maximum.

For qualitative variables with more than 6 levels, I usually prefer a light/transarent blue sequential scale, since one cannot distinguish the colors effectively any more anyways. Alternatively one can use a greyscale for the variables, and highlight some few with colors.

See the plot below as an example of a blue sequential scale. Note that in the plot below, we don't use a legend, cause we don't want to emphasize/highlight the separate lines, but rather show the general trend.

cols = fybdthemes.discrete_sequential_colors(5)

_, ax = plt.subplots(figsize=(8, 4))
for i in range(5):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=2, alpha=.65)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()

png

A grey color scale to do the same as above can be created from seaborn:

plot_palette(sns.color_palette("light:black", as_cmap=False))

png

If we want to put emphasis on a single line, we plot all other lines, for instance, in grey, and highlight the one we are interest in afterwards.

cols = sns.color_palette("light:black", as_cmap=False, n_colors=9)
highlight_color = fybdthemes.discrete_sequential_colors(3)[1]

_, ax = plt.subplots(figsize=(8, 4))
for i in range(9):
    f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
    plt.plot(X, f, color=cols[i], linewidth=1, alpha=.65)
f = numpy.random.multivariate_normal(np.zeros(X.shape[0]), K) 
plt.plot(X, f, color=highlight_color, linewidth=2)
ax.set_xlabel(r'$x$')
ax.set_ylabel(r'$f(x)$')
plt.show()

png

Author

Simon Dirmeier sfyrbnd @ pm me

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

fybdthemes-0.1.2-py3-none-any.whl (7.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page