Color palettes and plot themes
Project description
fybdthemes
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())
In the visualization above we use three discrete colors, but you can also choose more:
plot_palette(fybdthemes.discrete_sequential_colors(10))
plot_palette(fybdthemes.discrete_sequential_colors(10, True))
Usually for continuous variables, we want a continous scale though:
plot_palette(fybdthemes.continuous_sequential_colors())
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()
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())
As above, you can specify more colors, too, and reverse them:
plot_palette(fybdthemes.discrete_diverging_colors(10, True))
Since the visualized variable is usually continuous again, you want a continous scale again:
plot_palette(fybdthemes.continuous_diverging_colors())
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()
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())
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()
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]])
plot_palette(np.array(cols)[[0, 1, 4]])
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()
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()
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))
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()
Author
Simon Dirmeier sfyrbnd @ pm me
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file fybdthemes-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: fybdthemes-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdeaccc3104014cfbe086f1321446e8f32451cdfac1b11f56eaba980fb11cdd6 |
|
MD5 | 47a2575360fd49f7ad555fc2096f9f67 |
|
BLAKE2b-256 | 529e1d3cf825062f705b6ff445df773773a22258977253c9eea81647f0f88df0 |