Logomaker
Logomaker is a Python package designed for creating high-quality sequence logos. It provides a flexible API for generating, styling, and customizing logos from sequence matrices (e.g., position-weight matrices or information matrices). The current version is 0.8.7, and the library is actively maintained with releases addressing bugs and adding features.
Common errors
-
AttributeError: module 'logomaker' has no attribute 'Logo'
cause You are trying to access the `Logo` class using `logomaker.Logo` after importing the module as `import logomaker`, but the `Logo` class is intended for direct import.fixChange your import statement from `import logomaker` to `from logomaker import Logo`, and then use `Logo(...)` directly. -
ValueError: Dataframe must have an index of positive integers.
cause The pandas DataFrame provided to the `logomaker.Logo` constructor (or related functions) does not have a numeric index, or the index contains non-positive integers. Logomaker expects the index to represent sequence positions.fixEnsure your input DataFrame has a numeric index (e.g., `0, 1, 2...` or `1, 2, 3...`). You might need to use `df.reset_index(drop=True)` or `df.set_index(df.index + 1)` if your index is not suitable. -
KeyError: 'A'
cause Your input DataFrame (e.g., a position-weight matrix) is missing columns for expected character labels (e.g., 'A', 'C', 'G', 'T' for DNA/RNA). Logomaker expects columns corresponding to the characters you want to represent.fixEnsure your input pandas DataFrame has columns for all the characters you intend to display in the logo. For DNA/RNA, these are typically 'A', 'C', 'G', 'T' (or 'U'). For proteins, this would be the 20 standard amino acid codes.
Warnings
- gotcha Older versions (pre-0.8.2) frequently rebuilt the Matplotlib font cache when loading, leading to slow startup times and potential resource issues, especially in scripts or automated environments.
- gotcha The `stack_order='fixed'` option for `Logo` objects was non-functional in versions prior to 0.8.2. This could lead to incorrect rendering when trying to maintain a specific, fixed order of characters in the logo stacks.
- gotcha Versions prior to 0.8.3 had overly stringent numerical type checking for input matrices. This could cause valid input DataFrames to be rejected if their numerical types (e.g., `int64` vs. `float64`) did not precisely match internal expectations.
Install
-
pip install logomaker
Imports
- Logo
import logomaker; logomaker.Logo
from logomaker import Logo
- get_example_matrix
import logomaker; logomaker.get_example_matrix
from logomaker import get_example_matrix
Quickstart
import logomaker
import matplotlib.pyplot as plt
import pandas as pd
# Load an example matrix (e.g., position-weight matrix)
# This returns a pandas DataFrame
example_df = logomaker.get_example_matrix('ww_matrix')
# Create a Logo object from the DataFrame
logo = logomaker.Logo(example_df)
# Customize the logo (optional)
logo.style_spines(visible=False)
logo.style_xticks(rotation=90, fmt='%d', anchor=0)
logo.highlight_position(p=6, color='gold')
logo.ax.set_ylabel('Information (bits)')
logo.ax.set_xlabel('Position')
logo.ax.set_title('RNA Binding Protein WW Domain Logo')
plt.tight_layout()
plt.show()