In Altair, we can create custom and shareable themes to apply to different charts. So, several Python packages are available with these themes ready to go. Considering four examples, let's see how other people package their Altair themes. For more information before we talk about them, check the Altair Themes section available in the documentation.
As a first example, we have the altair-latimes package. Here we can find the Los Angeles Times theme for Altair. More specifically, in addition to the theme()
function that contains some constants and returns a dictionary with the configuration for the theme, there is a color dictionary (palette
). This color dictionary can also be imported and used directly.
For a theme based on the University of Washington brand, there is husky_theme. To leverage it, we need to run the respective file as a script or add it to a given project (and import the husky_theme()
function), as opposed to installing and importing a Python package. Like the first example, the husky_theme()
function contains some constants and returns a dictionary.
In the hueniversitypy (great name!) package, we can find different themes based on the visual identities of Canadian universities. Each theme is defined in its file, similar to the previous examples (a function with some constants and which returns a dictionary). The main difference between this package and the previous examples is that it brings together more than one theme that can be used separately (instead of being dedicated to a single theme).
To use any of the themes available in the three examples mentioned above globally, we need to alt.themes.register()
and alt.themes.enable()
them. In the case of the carbonplan-styles package, which provides a light theme and a dark theme for Altair and Matplotlib, we just need to enable the theme (and we don't need to import any extras). In other words, there is no need to register the theme, as it is added to the available themes "automatically" from the entry points defined in the setup.py
file (or equivalent):
from setuptools import setup
ENTRY_POINTS = {
"altair.vegalite.v4.theme": [
"carbonplan_dark = carbonplan_styles.altair:dark",
"carbonplan_light = carbonplan_styles.altair:light",
],
}
setup(name="carbonplan-styles", entry_points=ENTRY_POINTS)
As for the Altair themes themselves, there is a color dictionary for each one (colors.py
file), importable constants for the fonts, and a function for each theme, defined from a more general function (altair.py
file).
Finally, if you have any questions or suggestions, feel free to leave a comment below!
Top comments (0)