Usage & Examples

Basic functionalities

1. Applying the style sheet: lizard_style()

The package includes a built-in style sheet, designed to standardize and enhance the visual appeal of your plots. This style sheet configures various elements, ranging from fonts and font sizes to axis and grid settings, and even the default colors for multiple lines in a plot. Applying this style, which is also possible in the R package with + lizard_style() provides a consistent look, whether you’re using ggplot in R or matplotlib/seaborn in Python.

You can apply the Biolizard-look by placing lizard_style() at the top of your scripts after importing BioLizardStylePython. Please note that lizard_style() serves as a starting template and may not be the ideal style for every plot. You can easily customize individual style settings by placing your overrides after calling the lizard_style() function. Use plt.style.use('default') to return back to the default style.

Here’s a demonstration of how a simple line plot would look before and after applying the lizard_style():

import matplotlib.pyplot as plt
import numpy as np
from BioLizardStylePython import *
np.random.seed(42)

# Function to create random lineplot
def create_line_plot():
    x = np.linspace(0, 10, 10)
    y1 = np.sin(x) + np.random.normal(0, 0.1, size=x.shape)
    y2 = np.cos(x) + np.random.normal(0, 0.1, size=x.shape)
    y3 = x + np.random.normal(0, 0.5, size=x.shape)
    plt.figure()
    plt.plot(x, y1, label='Line 1')
    plt.plot(x, y2, label='Line 2')
    plt.plot(x, y3, label='Line 3')
    plt.title('Random Plot with 3 Lines')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.legend()
    plt.show()

create_line_plot()
Lato font found! Ready to start visualizing in BLZ style!
_images/42347cb44762cf256eab64bef7799ac910a925808bcb68d2a0b1da40d9fd6b44.png
lizard_style()
create_line_plot()
_images/e0eb9efbbb5d6c2edfa6e2e5eae32a1095e863a1e7ea7882ab17bc688ead946a.png

2. Using the qualitative color palette

The biolizard_qualitative_pal() function retrieves a predefined qualitative colormap. For instance, biolizard_qualitative_pal().colors returns a list of all 12 colors in hex code. Calling biolizard_qualitative_pal()(0) fetches the first color in the palette, while biolizard_qualitative_pal()(range(5)) retrieves the first five colors.

This palette is inspired by Martin Krzywinski’s “12-Color Palette for Color Blindness,” yet also incorporates the signature colors of BioLizard for its first three shades. Specifically designed to be inclusive, it is suitable for individuals with the most common form of color blindness: Deuteranopia (Red-Green Color Blindness).

Here’s two examples illustrating how to use this function:

#Example 1
import seaborn as sns
# Sample data
data = sns.load_dataset("iris")
# Using the colormap in Seaborn's swarmplot
sns.swarmplot(x="species", y="sepal_length", hue ="species", data=data, palette=biolizard_qualitative_pal().colors)
plt.title('A Flower Plot')
plt.show()
/tmp/ipykernel_761208/3626689180.py:6: UserWarning: The palette list has more values (12) than needed (3), which may not be intended.
  sns.swarmplot(x="species", y="sepal_length", hue ="species", data=data, palette=biolizard_qualitative_pal().colors)
_images/6c4670c6cc47869a1b1bdd06a0dbb63d14cde8b230382bee7dbf249322dce7da.png
#Example 2
# Data for the bar plot
categories = ['A'+str(i) for i in range(12)]
values = [4, 7, 1, 8]*3

# Generate colors from the qualitative palette
palette= biolizard_qualitative_pal()
bar_colors = [palette(i) for i in range(len(categories))]

# Create the bar plot
plt.figure(figsize=(8, 6))
bars = plt.bar(categories, values, color=bar_colors)

# Add title and labels
plt.title('Example Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')

plt.show()
_images/e4d4d95c9b8c8eebb77f7f7bc9a2e9a3a7fbfdf094d7369e155a4ca19f5dc640.png

3. Using the sequential & divergent color map

When you load this package, it automatically registers two new colormaps in matplotlib: a Sequential colormap and a Divergent colormap: “biolizard_sequential_pal” and “biolizard_divergent_pal”. Both strings are now recognized my matplotlib.

Sequential

The Sequential Biolizard Color Map is tailored to depict underlying numerical values via a uniform progression in luminance. It employs the distinctive BioLizard green as its base hue. The palette uses HCL gradients for better perceptual uniformity compared to RGB, and uses a triangular chroma progression to distinguish middle-range values from extremes.

Divergent

This colormap is specifically designed for scenarios where the color corresponds to numerical values that have a natural midpoint. It utilizes a triangular luminance sequence with differing hues in each “arm” of the palette. Chroma and luminance are balanced between the two arms, and the neutral central value has zero chroma. The palette employs hues 291 and 170, the latter being Biolizard’s distinctive green. This hue combination ensures that the palette is accessible for all major forms of color blindness.

Here an example illustrating how to use these palettes:

data = np.random.rand(5, 5)  # Example data
plt.imshow(data, cmap='biolizard_sequential_pal')
plt.colorbar()
plt.show()

data = np.random.rand(5, 5)  # Example data
plt.imshow(data, cmap='biolizard_divergent_pal')
plt.colorbar()
plt.show()
_images/5346d1f8a5ddaa1e3053a224d6d4f64671b2f8fa0b4279b4e51b3268214ada54.png _images/33bf448d38c18d5aef32e418d40c5ceafce7090c5f21ee87ee4632790d64e991.png

Examples

1. simple boxplot

sns.boxplot(x="species", y="sepal_length", data=data)
plt.title('A Flower Plot')
plt.show()
_images/ee4d9b21369f8f804d953b040ea44e26df1667d24a7e172d43dce6bcdba29a4c.png

A violin plot is similar to a boxplot, but captures more information on the data distribution:

sns.violinplot(x="species", y="sepal_length", data=data)
plt.title('A Flower Plot')
plt.show()
_images/19308f5bdbc9e63735bdbdb049919fec4293ede53e5dfc1f32afd55501923b38.png

2. Simple density plot

sns.displot(data, x="sepal_length", hue="species", kind="kde", fill=True)
plt.title('A Flower Plot')
plt.show()
_images/5196919949527e58e158422dd3aac6b970c50bb378a1e7498c0435a536339b6a.png

3. Simple scatterplot

sns.scatterplot(data, x='sepal_length', y='petal_length')
plt.title('A Flower Plot')
plt.show()
_images/333c2671c9a22cd7206e31154806dce3965dd8db2564c3797698dea05494befb.png

We can use lmplot or regplot to add a regression line:

import scipy as sp

g = sns.lmplot(data, x='sepal_length', y='petal_length', line_kws=dict(color='#1e2237'))

def annotate(data, **kws):
    r, p = sp.stats.pearsonr(data['sepal_length'], data['petal_length'])
    ax = plt.gca()
    ax.text(.05, .8, '$r^2$={:.2f}, p={:.2g}'.format(r**2, p),
            transform=ax.transAxes)
    
g.map_dataframe(annotate)
plt.title('A Flower Plot')
plt.show()
_images/498a0750907b8420a4f68866aa2431ea84b991d0426e5c70c3cd2d459c98c6ac.png

4. Treemap plots

Treemap plots can be used as an alternative to bar charts, for example when there are too many categories that a bar chart would look cluttered.

biolizard_qualitative_pal()(range(len(labels)))
array([[0.00392157, 0.62745098, 0.5254902 , 1.        ],
       [0.11764706, 0.13333333, 0.21568627, 1.        ],
       [0.91372549, 0.7254902 , 0.25098039, 1.        ],
       [0.        , 0.76078431, 0.97647059, 1.        ],
       [1.        , 0.43137255, 0.22745098, 1.        ],
       [0.        , 0.98823529, 0.81176471, 1.        ]])
import squarify
import random


# Sample data
values = [250, 120, 280, 320, 140, 95]
labels = ['Group 1\n 250', 'Group 2\n 120', 'Group 3\n 280',
          'Group 4\n 320', 'Group 5\n 140', 'Group 6\n 95']
colors = biolizard_qualitative_pal()(range(len(labels)))

# Treemap
squarify.plot(sizes = values, label = labels, color = colors,
              text_kwargs={'color':'white'})

# Remove the axis:
plt.axis("off")

# plt.show()
(0.0, 100.0, 0.0, 100.0)
_images/b8ca0027099c5da56fedc334a1f73357da86c9d46cf062dcc9ca95128c3235bd.png

4. Fun with maps

This example was taken from: https://matplotlib.org/basemap/stable/users/examples.html

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from datetime import datetime
# miller projection 
map = Basemap(projection='mill',lon_0=0)
# plot coastlines, draw label meridians and parallels.
map.drawcoastlines()
map.drawparallels(np.arange(-90,90,30),labels=[1,0,0,0])
map.drawmeridians(np.arange(map.lonmin,map.lonmax+30,60),labels=[0,0,0,1])
map.drawmapboundary(fill_color="#1e2237")
map.fillcontinents(color="#01a086",lake_color="#1e2237")
# shade the night areas, with alpha transparency so the 
# map shows through. Use current time in UTC.
date = datetime.now()
CS=map.nightshade(date)
plt.title('Day/Night Map for %s (UTC)' % date.strftime("%d %b %Y %H:%M:%S"))
plt.show()
_images/2147426cd8cb9356b1f20f52b821c3b9f9567b6c809d01005a168570ade96178.png