Skip to main content

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

Metadata

Release files for fybdthemes 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for fybdthemes 0.1.2
File Interpreter ABI Platform
fybdthemes-0.1.2-py3-none-any.whl Python 3 none any Details

Release files / fybdthemes-0.1.2-py3-none-any.whl

Download URL fybdthemes-0.1.2-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdeaccc3104014cfbe086f1321446e8f32451cdfac1b11f56eaba980fb11cdd6
BLAKE2b-256 checksum
How to use checksums
529e1d3cf825062f705b6ff445df773773a22258977253c9eea81647f0f88df0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.13

Security for fybdthemes 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page