Customization of plotannot annotations

This notebook contains examples of how to customize the look of plotannot annotations.

Load packages and data

[1]:
#Load a few packages for plotting
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
np.random.seed(1) #make notebook reproducible
[2]:
#Load plotannot
import plotannot
[3]:
#Create some fake data
table = pd.DataFrame(np.random.random((100,50)))
table.head()
[3]:
0 1 2 3 4 5 6 7 8 9 ... 40 41 42 43 44 45 46 47 48 49
0 0.417022 0.720324 0.000114 0.302333 0.146756 0.092339 0.186260 0.345561 0.396767 0.538817 ... 0.988861 0.748166 0.280444 0.789279 0.103226 0.447894 0.908596 0.293614 0.287775 0.130029
1 0.019367 0.678836 0.211628 0.265547 0.491573 0.053363 0.574118 0.146729 0.589306 0.699758 ... 0.114746 0.949489 0.449912 0.578390 0.408137 0.237027 0.903380 0.573679 0.002870 0.617145
2 0.326645 0.527058 0.885942 0.357270 0.908535 0.623360 0.015821 0.929437 0.690897 0.997323 ... 0.556240 0.136455 0.059918 0.121343 0.044552 0.107494 0.225709 0.712989 0.559717 0.012556
3 0.071974 0.967276 0.568100 0.203293 0.252326 0.743826 0.195429 0.581359 0.970020 0.846829 ... 0.263297 0.065961 0.735066 0.772178 0.907816 0.931972 0.013952 0.234362 0.616778 0.949016
4 0.950176 0.556653 0.915606 0.641566 0.390008 0.485991 0.604310 0.549548 0.926181 0.918733 ... 0.315245 0.892889 0.577857 0.184010 0.787929 0.612031 0.053909 0.420194 0.679069 0.918602

5 rows × 50 columns

Control the relative width of labels when calculating overlaps with ‘rel_label_size’

[4]:
fig, axarr = plt.subplots(ncols=3, figsize=(15,2))

to_label = range(0,10)
rel_label_sizes = [0.5,1,2]

for i, ax in enumerate(axarr):
    sns.heatmap(table, xticklabels=True, yticklabels=False, cbar=False, ax=ax)

    plotannot.annotate_ticks(ax, axis="xaxis", labels=to_label, rel_label_size=rel_label_sizes[i])
    ax.set_title(f"rel_label_size: {rel_label_sizes[i]}")
../_images/examples_customization_7_0.png

Control extent of axis with ‘expand_axis’

[5]:
fig, axarr = plt.subplots(ncols=2, figsize=(10,2))

ax = sns.heatmap(table, xticklabels=True, yticklabels=False, cbar=False)
to_label = range(0,20)
expand_axis_lst = [0, 0.4]

#Apply annotation
for i, ax in enumerate(axarr):
    sns.heatmap(table, xticklabels=True, yticklabels=False, cbar=False, ax=ax)

    plotannot.annotate_ticks(ax, axis="xaxis", labels=to_label, expand_axis=expand_axis_lst[i])
    ax.set_title(f"expand_axis: {expand_axis_lst[i]}")
../_images/examples_customization_9_0.png

Control the extent of the annotation lines with ‘rel_tick_size’ and ‘perp_shift’

[6]:
#Options for plotannot
to_label = range(0,15)
rel_tick_size_lst = [0.1,0.5,0.9]
perp_shift_lst = [5,10,20]

#Setup plot
fig, axarr = plt.subplots(3,3, figsize=(12,10))
plt.subplots_adjust(hspace=0.7)

#Plot and annotate with different options
for i, perp_shift in enumerate(perp_shift_lst):
    for j, rel_tick_size in enumerate(rel_tick_size_lst):

        ax = axarr[i,j]
        sns.heatmap(table, xticklabels=True, yticklabels=False, cbar=False, ax=ax)

        plotannot.annotate_ticks(ax, axis="xaxis", labels=to_label, rel_tick_size=rel_tick_size, perp_shift=perp_shift)
        _ = ax.set_title(f"rel_tick_size={rel_tick_size} | perp_shift={perp_shift}")
../_images/examples_customization_11_0.png