palettable

repository·master·Indexed 21 days ago

https://github.com/jiffyclub/palettable

A Python library providing a wide collection of color palettes, including those from Colorbrewer, CartoColors, cmocean, and Light and Bartlein. It offers a consistent interface to access colors as RGB tuples, hex strings, or matplotlib colormaps, and includes tools for visualizing palettes as blocks or continuous images in IPython environments. The library also supports algorithmic generation of custom Cubehelix color maps.

Tokens
6.6K
Snippets
25
Records
35
Agent score
72%

What's inside palettable

  1. Overview of Palettable

    master
    Palettable (formerly brewer2mpl) is a pure Python library providing a collection of color palettes. It has no external dependencies and is designed to be used for customizing matplotlib plots or providing color data for web applications.
  2. Use Scientific palettes in Palettable

    master

    Scientific palettes in palettable are based on the work of Fabio Crameri. These palettes are designed for scientific visualization and are available with up to 256 discreetly defined colors.

    They are organized into three sub-modules based on their color distribution type:

    • palettable.scientific.diverging: For palettes that transition between two different colors, typically through a neutral midpoint.
    • palettable.scientific.sequential: For palettes that represent data moving in one direction (e.g., low to high).
    • palettable.scientific.qualitative: For palettes used to represent discrete, unordered categories.
  3. How to find and import Palettable palettes

    master

    Palettes are pre-built and loaded at import time. They follow a naming convention of <Name>_<number of colors>. For example, the Colorbrewer2 palette Dark2 with seven colors is named Dark2_7.

    Every palette also has a reversed version with the same name plus the suffix _r (e.g., Dark2_7_r).

    Palettes are organized into modules based on their source (e.g., cartocolors, cmocean, colorbrewer, plotly, tableau) and their type (diverging, qualitative, or sequential).

    from palettable.colorbrewer.qualitative import Dark2_7
  4. Use Palettable with matplotlib

    master

    Palettable integrates deeply with matplotlib through several attributes:

    Set the matplotlib Color Cycle

    To change the default color cycle used when drawing plots, use the .mpl_colors attribute with ax.set_prop_cycle().

    ax.set_prop_cycle('color', palettable.colorbrewer.qualitative.Dark2_8.mpl_colors)

    Use as a Colormap

    For functions that accept a cmap argument (like imshow), use the .mpl_colormap attribute.

    from palettable.colorbrewer.sequential import Blues_8
    ax.imshow(data, cmap=Blues_8.mpl_colormap)

    Create a Discrete Colormap

    Since .mpl_colormap is continuous/interpolated, use matplotlib.colors.ListedColormap with .mpl_colors to create a discrete version.

    from matplotlib.colors import ListedColormap
    cmap = ListedColormap(palettable.colorbrewer.qualitative.Dark2_7.mpl_colors)
  5. Add new Palettable modules to the documentation

    master

    To include a new Palettable module in the documentation, follow these steps:

    1. Create a directory: Create a new directory dedicated to the module's documentation.
    2. Create a template: Create an index.md.tpl file inside the new directory. This file must use Jinja templating to generate the table-of-contents and include palette preview images. You can use matplotlib/index.md.tpl as a reference.
    3. Register the module: Update the MODULES dictionary in the gendocs.py script to map your new module to its corresponding directory.
    4. Build and preview: Run the standard build process (make images, make compile, make build) to verify the changes.
  6. Access cmocean palettes in Palettable

    master

    The cmocean palettes in Palettable are sourced from the original cmocean package. They are organized into two sub-modules based on their color application type:

    • palettable.cmocean.diverging: For diverging color schemes.
    • palettable.cmocean.sequential: For sequential color schemes.
  7. Use Light and Bartlein palettes

    master

    Light and Bartlein palettes are sourced from the Department of Geography at the University of Oregon. They are organized into two distinct sub-modules based on their color scale type:

    • Diverging scales: Use palettable.lightbartlein.diverging for scales that transition from one color to another through a neutral midpoint.
    • Sequential scales: Use palettable.lightbartlein.sequential for scales that represent ordered data through a single color progression.
  8. Access Colorbrewer palettes in Palettable

    master

    Colorbrewer palettes are available in palettable through three sub-modules, categorized by their intended use case:

    • Diverging palettes: palettable.colorbrewer.diverging
    • Qualitative palettes: palettable.colorbrewer.qualitative
    • Sequential palettes: palettable.colorbrewer.sequential
  9. Use CartoColors palettes in Palettable

    master

    CartoColors palettes are available in palettable and are categorized into three sub-modules based on their intended use case:

    • Diverging: For data that has a meaningful midpoint (e.g., temperature, deviation from a mean).
    • Qualitative: For categorical data where there is no inherent order (e.g., different types of land use, different species).
    • Sequential: For data that ranges from low to high (e.g., population density, elevation).

    These palettes are derived from the CartoColors node module.

  10. Build the Palettable documentation

    master

    The Palettable documentation is generated using a combination of hand-curated content and Jinja templates (.md.tpl files). The gendocs.py script compiles these templates into Markdown, which is then converted to HTML by Urubu.

    Prerequisites

    To build the documentation, you must have the following installed:

    • palettable
    • matplotlib
    • Jinja
    • Urubu
    • tservice

    Build Steps

    1. Generate assets and compile templates:
      • Run make images to generate palette preview images.
      • Run make compile to compile the .md.tpl files into Markdown (if images are already up-to-date).
    2. Generate HTML:
      • Urubu configuration is managed via _site.yml.
      • Run make build to trigger the Urubu build process.
    3. Preview:
      • Run make serve to preview the documentation locally.
    make images
    make compile
    make build
    make serve