Python 3 function to assign compact letter display letters and for plotting them with matplotlib/seaborn
Project description
Cld4py is a Python3 package that allows to assign compact letter display and can add them to seaborn/matplotlib figures
Function to apply compact letter display for pairwise contrasts.
Groups with no significant differences share a letter.
It also can be used for plotting letters to seaborn/matplotlib figures.
Installation
pip install cld4py
Package content
Cld4py currently includes two functions:
- assign_letters(): this function will assign CLD letters to the contrasts
- plot_letters(): this function can add CLD letters to matplotlib/seaborn figures
Parameters
assign_letters()
"""
Function to apply compact letter display for pairwise contrasts.
Groups with no significant differences share a letter.
Parameters:
Required:
df - dataframe with contrasts (pairwise comparisons).
G1, G2 - columns in contrasts df with compared groups.
P - column in contrasts df with p-value (adjusted, right?).
Optional:
alpha - sigificance level (default 0.05).
order - None (default), list or ['ascending', 'descending'].
This parameter will define the order of assigned letters.
None - alphabetical order will be applied.
List - order of groups will be defined by that list.
String 'ascending' or 'descending' requires parameters
'data', 'values' and 'group' to order groups by the mean.
data - dataframe with values that were compared to get contrasts.
vals - column in data with compared values.
group - column in data with group information.
param - wether sort by mean (True; default) or median (False).
"""
plot_letters()
"""
Function to plot CLD letters for sns boxplot, violinplot, barplot or swarmplot.
Groups with no significant differences share a letter.
Parameters:
Required:
cld - dataframe or dictionary with groups and letters. If df then groups
should be in the index and letters in the "Letters" column.
data - dataframe with values that were plotted.
vals - column in data with compared values.
group - column in data with group information.
figax - matplotlib or sns figure or ax with plot.
Optional:
axis - axis with plotted groups: "x" or "y" (default "x").
plot - plot type: "boxplot", "violinplot", "barplot" or "swarmplot"
(default "boxplot").
pos - letters position: "upper", "lower", "top" or "bottom"
(default: "upper"). If axis = "y", "upper" and "top" will be
plotted on the right side, "lower" and "bottom" - on the left.
pad - distance (% of data range) to the plotted group object (default 1).
c - color of letters.
fs - fontsize of letters.
lim - increase axes limits, expressed in "pad" (see above) values (default 0)
"""
Usage examples
import cld4py
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
cont = pd.read_csv('tests/contrasts.tsv', sep='\t')
data = pd.read_csv('tests/data.tsv', sep='\t')
order = sorted(data.Sample.unique().tolist())
cld = cld4py.assign_letters(cont, 'Group1', 'Group2', 'P-adj', order='descending',
data=data, vals='Metric', group='Sample')
cld
| Letters | |
|---|---|
| Group | |
| S3 | a |
| S1 | a |
| S4 | a |
| S7 | b |
| S6 | b |
| S2 | c |
| S5 | d |
Boxplots
#boxplot x upper
fig, ax = plt.subplots(1, 1, )
sns.boxplot(y='Metric', x='Sample', data=data, ax=ax, order=order, showfliers=False)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='x', plot='boxplot', pos='upper',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=1)
plt.savefig(f'tests/Figures/Boxplot_x_upper.png')
#boxplot x top
fig, ax = plt.subplots(1, 1, )
sns.boxplot(y='Metric', x='Sample', data=data, ax=ax, order=order, showfliers=False)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='x', plot='boxplot', pos='top',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=2)
#boxplot y lower
fig, ax = plt.subplots(1, 1, )
sns.boxplot(x='Metric', y='Sample', data=data, ax=ax, order=order, showfliers=False)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='y', plot='boxplot', pos='lower',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=2)
#boxplot y bottom
fig, ax = plt.subplots(1, 1, )
sns.boxplot(x='Metric', y='Sample', data=data, ax=ax, order=order, showfliers=False)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='y', plot='boxplot', pos='bottom',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=2)
plt.savefig(f'tests/Figures/Boxplot_y_bottom.png')
Violinplots
#violinplot x upper
fig, ax = plt.subplots(1, 1, )
sns.violinplot(y='Metric', x='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='x', plot='violinplot', pos='upper',
vals='Metric', group='Sample', pad=2, c='black', fs=None, lim=2)
plt.savefig(f'tests/Figures/Violinplot_x_upper.png')
#violinplot y top
fig, ax = plt.subplots(1, 1, )
sns.violinplot(x='Metric', y='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='y', plot='violinplot', pos='top',
vals='Metric', group='Sample', pad=2, c='black', fs=None, lim=2)
Barplots
#barplot x upper
fig, ax = plt.subplots(1, 1, )
sns.barplot(y='Metric', x='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='x', plot='barplot', pos='upper',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=2)
plt.savefig(f'tests/Figures/Barplot_x_upper.png')
#barplot y top
fig, ax = plt.subplots(1, 1, )
sns.barplot(x='Metric', y='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='y', plot='barplot', pos='top',
vals='Metric', group='Sample', pad=1, c='black', fs=None, lim=2)
#swarmplot x upper
fig, ax = plt.subplots(1, 1, )
sns.swarmplot(y='Metric', x='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='x', plot='swarmplot', pos='upper',
vals='Metric', group='Sample', pad=2, c='black', fs=None, lim=2)
#swarmplot y bottom
fig, ax = plt.subplots(1, 1, )
sns.swarmplot(x='Metric', y='Sample', data=data, ax=ax, order=order,)
cld4py.plot_letters(cld=cld, data=data, figax=ax, axis='y', plot='swarmplot', pos='bottom',
vals='Metric', group='Sample', pad=2, c='black', fs=None, lim=2)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cld4py-1.1.2.tar.gz.
File metadata
- Download URL: cld4py-1.1.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ede0520a2d5882c525e05fe0af116c923a9203ec9adf8bfcf6ca8e7b4f30af60
|
|
| MD5 |
ec1c00c082ce48a8c33b495834056b7e
|
|
| BLAKE2b-256 |
e1fe4f4103c9a559c012e75b0246d8034fef13ff357e44996a5db56dcd8de88a
|
File details
Details for the file cld4py-1.1.2-py3-none-any.whl.
File metadata
- Download URL: cld4py-1.1.2-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3842af25a08c6cb4b7bfe7de88e1286f7fe2cb41a84c0104ca56e13b58f3942
|
|
| MD5 |
0a595735ca73379c4712c4eed1b2de14
|
|
| BLAKE2b-256 |
7e9d395caa829867c5258cd0e5c7de60a97e31af31349f7877f8c3046ac6af2b
|