Plotting Prelude

AAanalysis provides some utility plotting functions to make publication-ready visualizations with a view extra lines of code.

Let us first make all imports and create some example data:

import matplotlib.pyplot as plt
import seaborn as sns

data = {'Classes': ['Class A', 'Class B', 'Class C'], 'Values': [23, 27, 43]}

A default seaborn barplot can be created as follows:

# Barplot with seaborn default settings
sns.barplot(x='Classes', y='Values', data=data)
sns.despine()
plt.title("Seaborn default")
plt.tight_layout()
plt.show()
../_images/plotting_prelude_1_output_3_0.png

Import aaanalysis and adjust plots using our aa.plot_setting() function:

import aaanalysis as aa

# Barplot with aaanalysis default settings
aa.plot_settings()
sns.barplot(x='Classes', y='Values', data=data)
sns.despine()
plt.title("Adjusted by AAanalysis")
plt.tight_layout()
plt.show()
../_images/plotting_prelude_2_output_5_0.png

Get a list of colors optimized for an appealing comparison of distinct categories via the aa.plot_get_clist() function:

colors = aa.plot_get_clist()

# Barplot with AAanalysis default color set for three categories
aa.plot_settings()
sns.barplot(x='Classes', y='Values', data=data, hue="Classes", palette=colors)
sns.despine()
plt.title("Adjusted by AAanalysis (optimized colors)")
plt.tight_layout()
plt.show()
../_images/plotting_prelude_3_output_7_0.png

Adjust the n_color parameter to select 2 to 9 distinct colors, or chose a value above 9 for a continuous spectrum of human-friendly Hue, Saturation, Lightness (HUSL) colors:

for n in [3, 5, 10]:
    colors = aa.plot_get_clist(n_colors=n)
    sns.palplot(colors)
    plt.show()
../_images/plotting_prelude_4_output_9_0.png ../_images/plotting_prelude_5_output_9_1.png ../_images/plotting_prelude_6_output_9_2.png

The aa.plot_settings() functions offers a variety of options to modify main plot elements such as ticks and grids:

data = {'Classes': ['Class A', 'Class B', 'Class C', "Class D", "Class E"], 'Values': [23, 27, 43, 9, 14]}
colors = aa.plot_get_clist(n_colors=5)

# Adjust grid and ticks
aa.plot_settings(no_ticks_x=True, short_ticks_y=True, grid=True, grid_axis="y")
sns.barplot(x='Classes', y='Values', hue="Classes", data=data, palette=colors)
sns.despine()
plt.title("Adjusted by AAanalysis (grid, no x ticks)")
plt.tight_layout()
plt.show()
../_images/plotting_prelude_7_output_11_0.png

To de- or increase the fontsize consistently, you can get the current fontsize using the aa.plot_gcfs() function:

data = {'Classes': ['Class A', 'Class B', 'Class C', "Class D", "Class E"], 'Values': [23, 27, 43, 9, 14]}
aa.plot_settings(no_ticks_x=True, short_ticks_y=True)
colors = aa.plot_get_clist(n_colors=5)
ax = sns.barplot(x='Classes', y='Values', hue="Classes", data=data, palette=colors)

# Set fontsize
fontsize = aa.plot_gcfs()
plt.title("Adjusted by AAanalysis (much bigger title)", size=fontsize+5)

plt.tight_layout()
sns.despine()
plt.show()
../_images/plotting_prelude_8_output_13_0.png

Create an independent legend by using the aa.plot_legend() function. For example, add bar hatches (i.e., patterns) and adjust the legend accordingly:

ax = sns.barplot(x='Classes', y='Values', hue="Classes", data=data, palette=colors)

# Set different hatches for each bar
hatches = ["/", "/", "/", ".", "."]
for bar, hatch in zip(ax.patches, hatches):
    bar.set_hatch(hatch)
sns.despine()

plt.title("Adjusted by AAanalysis (hatches with legend)")

dict_color = {"Group 1": "black", "Group 2": "black"}
aa.plot_legend(dict_color=dict_color, ncol=1, x=0.7, y=0.9, hatch=["/", "."])
plt.tight_layout()
plt.show()
../_images/plotting_prelude_9_output_15_0.png

See our Utility Functions API for a detailed documentation of all plotting functions, which are indicated by the plot_ prefix.