alphapepttools.pl.Plots#
- class alphapepttools.pl.Plots(config={'axes': {'label_size': 10, 'tick_size': 10, 'title_size': 10}, 'default_font': 'Arial', 'font_family': 'sans-serif', 'font_sizes': {'large': 12, 'medium': 10, 'small': 8}, 'highlight_colors': {'general': '#5ec962', 'high': '#9ecae1', 'low': '#fdae6b'}, 'legend': {'font_size': 10, 'title_size': 10}, 'linewidths': {'large': 1.25, 'medium': 0.5, 'small': 0.25}, 'marker_sizes': {'large': 15, 'medium': 10, 'small': 5}, 'na_color': (0.8274509803921568, 0.8274509803921568, 0.8274509803921568, 1.0), 'na_identifiers': ['nan'], 'preset_sizes': {'0.25': 22.5, '0.5': 45, '1': 89, '1.5': 135, '2': 183}, 'resolution': {'dpi': 300}})#
Class for creating figures with matplotlib
Basic configuration for matplotlib plots is loaded from a YAML file and set to generate consistent plots.
Methods table#
|
Plot a bar chart from a DataFrame or AnnData object |
|
Plot a box plot from a DataFrame or AnnData object |
|
Plot a histogram from a DataFrame or AnnData object |
|
Plot the PCs of a PCA analysis using the scatter method |
|
Plot the gene loadings of a PC using the scatter method |
|
Plot the gene loadings of a PC using the scatter method |
|
Plot the ranked protein median intensities across all samples using the scatter method |
|
Plot a scatterplot from a DataFrame or AnnData object |
|
Plot the eigenvalues of each of the PCs using the scatter method |
|
Plot a violin plot from a DataFrame or AnnData object |
Methods#
- classmethod Plots.barplot(ax, data, grouping_column=None, value_column=None, direct_columns=None, color=(0.21299500192233756, 0.5114186851211072, 0.730795847750865, 1.0), color_dict=None)#
Plot a bar chart from a DataFrame or AnnData object
Creates a bar plot showing means with error bars (standard deviation) for grouped data. Each bar represents the mean of values within a group, with error bars showing the standard deviation. Bars have semi-transparent fill with opaque black outlines.
- Parameters:
ax (plt.Axes) – Matplotlib axes object to plot on.
data (ad.AnnData | pd.DataFrame) – Data containing grouping and value columns or direct columns to plot.
grouping_column (list[str] | None, optional) – Column containing the groups to compare. By default None.
value_column (list[str] | None, optional) – Column whose values should be plotted. By default None.
direct_columns (list[str] | None, optional) – Overrides grouping_column and value_column. Each column becomes a separate bar group. By default None.
color (tuple, optional) – Default color for all bars. By default BaseColors.get(“blue”).
color_dict (dict | None, optional) – Dictionary mapping group labels to specific colors. Overrides the color parameter for specified groups. By default None.
- Return type:
- Returns:
None
- classmethod Plots.boxplot(ax, data, grouping_column=None, value_column=None, direct_columns=None, color=(0.21299500192233756, 0.5114186851211072, 0.730795847750865, 1.0), color_dict=None)#
Plot a box plot from a DataFrame or AnnData object
Creates a box plot showing the distribution of values for grouped data. Each box shows the median, quartiles, and outliers for values within a group. Boxes have semi-transparent fill with opaque black outlines, medians, whiskers, and caps.
- Parameters:
ax (plt.Axes) – Matplotlib axes object to plot on.
data (ad.AnnData | pd.DataFrame) – Data containing grouping and value columns or direct columns to plot.
grouping_column (list[str] | None, optional) – Column containing the groups to compare. By default None.
value_column (list[str] | None, optional) – Column whose values should be plotted. By default None.
direct_columns (list[str] | None, optional) – Overrides grouping_column and value_column. Each column becomes a separate box plot. By default None.
color (tuple, optional) – Default color for all boxes. By default BaseColors.get(“blue”).
color_dict (dict | None, optional) – Dictionary mapping group labels to specific colors. Overrides the color parameter for specified groups. By default None.
- Return type:
- Returns:
None
- classmethod Plots.histogram(data, value_column, color_map_column=None, bins=10, ax=None, color='blue', palette=None, color_dict=None, legend=None, hist_kwargs=None, legend_kwargs=None, xlim=None, ylim=None)#
Plot a histogram from a DataFrame or AnnData object
- Parameters:
data (pd.DataFrame | ad.AnnData) – Data to plot, must contain the value_column and optionally the color_column.
value_column (str) – Column in data to plot as histogram. Must contain numeric data.
color_map_column (str, optional) – Column in data to use for color encoding. These values are mapped to the palette or the color_dict (see below). Its values cannot contain NaNs, therefore color_map_column is coerced to string and missing values replaced by a default filler string. Overrides color parameter. By default None.
bins (int, optional) – Number of bins to use for the histogram. By default 10.
color (str, optional) – Color to use for the histogram. By default “blue”.
ax (plt.Axes, optional) – Matplotlib axes object to plot on, if None a new figure is created. By default None.
palette (list[tuple], optional) – List of colors to use for color encoding, if None a default palette is used. By default None.
color_dict (dict[str, str | tuple], optional) – Supercedes palette, a dictionary mapping levels to colors. By default None. If provided, palette is ignored.
legend (str | mpl.legend.Legend, optional) – Legend to add to the plot, by default None. If “auto”, a legend is created from the color_column. By default None.
hist_kwargs (dict, optional) – Additional keyword arguments for the matplotlib hist function. By default None.
legend_kwargs (dict, optional) – Additional keyword arguments for the matplotlib legend function. By default None.
xlim (tuple[float, float], optional) – Limits for the x-axis. By default None.
ylim (tuple[float, float], optional) – Limits for the y-axis. By default None.
- Return type:
- Returns:
None
- classmethod Plots.plot_pca(data, x_column=1, y_column=2, color='blue', color_map_column=None, color_column=None, dim_space='obs', embeddings_name=None, label=False, label_column=None, ax=None, palette=None, color_dict=None, legend=None, scatter_kwargs=None)#
Plot the PCs of a PCA analysis using the scatter method
- Parameters:
adata (ad.AnnData) – AnnData to plot.
ax (plt.Axes) – Matplotlib axes object to plot on.
pc_x (int) – The PC principal component index to plot on the x axis, by default 1. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).
pc_y (int) – The principal component index to plot on the y axis, by default 2. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).
dim_space (str, optional) – The dimension space used in PCA. Can be either “obs” (default) for sample projection or “var” for feature projection. By default “obs”.
embeddings_name (str | None, optional) – The custom embeddings name used in PCA (given as input for
pcafunction inembeddings_name). If None, uses default naming convention. By default None.label (bool,) – Whether to add labels to the points in the scatter plot. by default False.
label_column (str | None = None,) – Column in data.obs to use for labeling the points. If None, and label is True, data.obs.index labels are added. By default None.
color (str, optional) – Color to use for the scatterplot. By default “blue”.
color_map_column (str, optional) – Column in data to use for color encoding. These values are mapped to the palette or the color_dict (see below). Its values cannot contain NaNs, therefore color_map_column is coerced to string and missing values replaced by a default filler string. Overrides color parameter. By default None.
color_column (str, optional) – Column in data to plot the colors. This must contain actual color values (RGBA, hex, etc.). Overrides color and color_map_column parameters. By default None.
palette (list[str | tuple], optional) – List of colors to use for color encoding, if None a default palette is used. By default None.
color_dict (dict[str, str | tuple], optional) – Supercedes palette, a dictionary mapping levels to colors. By default None. If provided, palette is ignored.
legend (str | mpl.legend.Legend, optional) – Legend to add to the plot, by default None. If “auto”, a legend is created from the color_column. By default None.
scatter_kwargs (dict, optional) – Additional keyword arguments for the matplotlib scatter function. By default None.
- Return type:
- Returns:
None
- classmethod Plots.plot_pca_loadings(data, ax, dim_space='obs', embeddings_name=None, dim=1, nfeatures=20, scatter_kwargs=None)#
Plot the gene loadings of a PC using the scatter method
- Parameters:
data (ad.AnnData) – AnnData to plot.
ax (plt.Axes) – Matplotlib axes object to plot on.
dim_space (str, optional) – The dimension space used in PCA. Can be either “obs” (default) for sample projection or “var” for feature projection. By default “obs”.
embeddings_name (str | None, optional) – The custom embeddings name used in PCA. If None, uses default naming convention. By default None.
dim (int) – The PC number from which to get loadings, by default 1 (1-indexed, i.e. the first PC is 1, not 0).
nfeatures (int) – The number of top absolute loadings features to plot, by default 20
scatter_kwargs (dict, optional) – Additional keyword arguments for the matplotlib scatter function. By default None.
- Return type:
- Returns:
None
- classmethod Plots.plot_pca_loadings_2d(data, ax, dim_space='obs', embeddings_name=None, pc_x=1, pc_y=2, nfeatures=20, *, add_labels=True, add_lines=False, scatter_kwargs=None)#
Plot the gene loadings of a PC using the scatter method
- Parameters:
data (ad.AnnData) – AnnData to plot.
ax (plt.Axes) – Matplotlib axes object to plot on.
dim_space (str, optional) – The dimension space used in PCA. Can be either “obs” (default) for sample projection or “var” for feature projection. By default “obs”.
embeddings_name (str | None, optional) – The custom embeddings name used in PCA. If None, uses default naming convention. By default None.
pc_x (int) – The PC principal component index to plot on the x axis, by default 1. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).
pc_y (int) – The principal component index to plot on the y axis, by default 2. Corresponds to the principal component order, the first principal is 1 (1-indexed, i.e. the first PC is 1, not 0).
nfeatures (int) – The number of top absolute loadings features to label from each component, by default 20
add_labels (bool) – Whether to add feature labels of the top
nfeaturesloadings. by defaultTrue.add_lines (bool) – If True, draw lines connecting the origin (0,0) to the points representing the top
nfeaturesloadings. Default isFalse.scatter_kwargs (dict, optional) – Additional keyword arguments for the matplotlib scatter function. By default None.
- Return type:
- Returns:
None
- classmethod Plots.rank_median_plot(data, ax, layer='X', color='blue', color_map_column=None, color_column=None, palette=None, color_dict=None, legend=None, scatter_kwargs=None)#
Plot the ranked protein median intensities across all samples using the scatter method
- Parameters:
data (ad.AnnData) – AnnData to plot.
ax (plt.Axes) – Matplotlib axes object to plot on, add labels and logscale the y-axis.
layer (str) – The AnnData layer to calculate the median value (intensities) across sample. Default is “X”
color (str, optional) – Color to use for the scatterplot. By default “blue”.
color_map_column (str, optional) – Column in data to use for color encoding. These values are mapped to the palette or the color_dict (see below). Its values cannot contain NaNs, therefore color_map_column is coerced to string and missing values replaced by a default filler string. Overrides color parameter. By default None.
color_column (str, optional) – Column in data to plot the colors. This must contain actual color values (RGBA, hex, etc.). Overrides color and color_map_column parameters. By default None.
palette (list[str | tuple], optional) – List of colors to use for color encoding, if None a default palette is used. By default None.
color_dict (dict[str, str | tuple], optional) – A dictionary mapping levels to colors. By default None. If provided, palette is ignored.
legend (str | mpl.legend.Legend, optional) – Legend to add to the plot, by default None. If “auto”, a legend is created from the color_column. By default None.
scatter_kwargs (dict, optional) – Additional keyword arguments for the matplotlib scatter function. By default None.
- Return type:
- Returns:
None
- classmethod Plots.scatter(data, x_column, y_column, color=None, color_map_column=None, color_column=None, ax=None, palette=None, color_dict=None, legend=None, scatter_kwargs=None, legend_kwargs=None, xlim=None, ylim=None)#
Plot a scatterplot from a DataFrame or AnnData object
Coloring works in three ways, with the following order of precedence: 1. color_column, 2. color_map_column, 3. color. If a color_column is provided, its values are interpreted directly as colors, i.e. they have to be something matplotlib can understand (e.g. RGBA, hex, etc.). If a color_map_column is provided, its values are mapped to colors in combination with palette or color_dict (see color mapping logic below). If neither color_column nor color_map_column is provided, the color parameter is used to color all points the same (defaults to blue).
Color mapping logic#
- color_map_column is non-numeric:
If color_dict is not None: Use color_dict to assign levels of color_map_column to colors (unmapped levels default to grey).
If color_dict is None, and palette is not None: Use palette to automatically assign colors to each level.
If color_dict is None and palette is None: Use a repeating default palette to assign colors to each level.
- color_map_column is numeric:
If palette is a matplotlib colormap: Numerically map values to colors using the colormap. This means that e.g. 1 and 3 will be closer in color than 1 and 10.
If palette is not a matplotlib colormap: Treat numeric values as categorical and color as described above.
- Examples:
color_column=”my_colors”: Points colored by values in “my_colors” column (must contain valid colors)
color_map_column=”cell_type”: Categorical mapping of cell types to colors
color_map_column=”expression”, palette=plt.cm.viridis: Continuous gradient based on expression values
- type data:
DataFrame|AnnData- param data:
Data to plot, must contain the x_column and y_column and optionally the color_column or color_map_column.
- type data:
pd.DataFrame | ad.AnnData
- type x_column:
- param x_column:
Column in data to plot on the x-axis. Must contain numeric data.
- type x_column:
str
- type y_column:
- param y_column:
Column in data to plot on the y-axis. Must contain numeric data.
- type y_column:
str
- type color:
- param color:
Color to use for the scatterplot. By default “blue”.
- type color:
str, optional
- type color_map_column:
- param color_map_column:
Column in data to use for color encoding. These values are mapped to the palette or the color_dict (see below). Its values cannot contain NaNs, therefore color_map_column is coerced to string and missing values replaced by a default filler string. Overrides color parameter. By default None.
- type color_map_column:
str, optional
- type color_column:
- param color_column:
Column in data to plot the colors. This must contain actual color values (RGBA, hex, etc.). Overrides color and color_map_column parameters. By default None.
- type color_column:
str, optional
- type ax:
Optional[Axes] (default:None)- param ax:
Matplotlib axes object to plot on, if None a new figure is created. By default None.
- type ax:
plt.Axes, optional
- type palette:
- param palette:
List of colors to use for color encoding, if None a default palette is used. By default None.
- type palette:
list[str | tuple] | matplotlib.colors.Colormap, optional
- type color_dict:
- param color_dict:
Supercedes palette, a dictionary mapping levels to colors. By default None. If provided, palette is ignored.
- type color_dict:
dict[str, str | tuple], optional
- type legend:
- param legend:
Legend to add to the plot, by default None. If “auto”, a legend is created from the color_column. By default None.
- type legend:
str | mpl.legend.Legend, optional
- type scatter_kwargs:
- param scatter_kwargs:
Additional keyword arguments for the matplotlib scatter function. By default None.
- type scatter_kwargs:
dict, optional
- type legend_kwargs:
- param legend_kwargs:
Additional keyword arguments for the matplotlib legend function. By default None.
- type legend_kwargs:
dict, optional
- type xlim:
- param xlim:
Limits for the x-axis. By default None.
- type xlim:
tuple[float, float], optional
- type ylim:
- param ylim:
Limits for the y-axis. By default None.
- type ylim:
tuple[float, float], optional
- rtype:
- returns:
None
- classmethod Plots.scree_plot(adata, ax, n_pcs=20, dim_space='obs', color='blue', embeddings_name=None, scatter_kwargs=None)#
Plot the eigenvalues of each of the PCs using the scatter method
- Parameters:
adata (ad.AnnData) – AnnData to plot.
ax (plt.Axes) – Matplotlib axes object to plot on.
n_pcs (int,) – number of PCs to plot, by default 20
dim_space (str, optional) – The dimension space used in PCA. Can be either “obs” (default) for sample projection or “var” for feature projection. By default “obs”.
color (str, optional) – Color to use for the scatterplot. By default “blue”.
embeddings_name (str | None, optional) – The custom embeddings name used in PCA. If None, uses default naming convention. By default None.
scatter_kwargs (dict, optional) – Additional keyword arguments for the matplotlib scatter function. By default None.
- Return type:
- Returns:
None
- classmethod Plots.violinplot(ax, data, grouping_column=None, value_column=None, direct_columns=None, color=(0.21299500192233756, 0.5114186851211072, 0.730795847750865, 1.0), color_dict=None)#
Plot a violin plot from a DataFrame or AnnData object
Creates a violin plot showing the distribution density of values for grouped data. Each violin shows the kernel density estimation of the distribution, along with medians, quartiles, and min/max whiskers. Violins have semi-transparent fill with opaque black outlines and internal statistical markers.
- Parameters:
ax (plt.Axes) – Matplotlib axes object to plot on.
data (ad.AnnData | pd.DataFrame) – Data containing grouping and value columns or direct columns to plot.
grouping_column (list[str] | None, optional) – Column containing the groups to compare. By default None.
value_column (list[str] | None, optional) – Column whose values should be plotted. By default None.
direct_columns (list[str] | None, optional) – Overrides grouping_column and value_column. Each column becomes a separate violin plot. By default None.
color (tuple, optional) – Default color for all violins. By default BaseColors.get(“blue”).
color_dict (dict | None, optional) – Dictionary mapping group labels to specific colors. Overrides the color parameter for specified groups. By default None.
- Return type:
- Returns:
None