Skip to main content

A plotting package for the Brookings Institution

Project description

pyplotbrookings

Overview

pyplotbrookings is a matplotlib extension which implements the Brookings Institution style guide. It offers several color palettes, a custom theme, and a few helper functions. pyplotbrookings is a python implementation of ggbrookings, an R extension for ggplot.

Installation

pyplotbrookings is now a python package! It downloaded from PyPI with pip (https://pypi.org/project/pyplotbrookings/0.1.0/)!

pip install pyplotbrookings==0.1.0

The accepted alias for pyplotbrookings is ppb. For example,

import pyplotbrookings.pyplotbrookings as ppb

Usage

The pyplotbrookings package has a few simple user facing functions:

  • set_theme() overrides the default matplotlib theme for a custom one which adheres to the Brookings style guide.

  • get_palette() returns the colors for a valid Brookings brand palettes.

  • set_palette() sets the matplotlib color cycler to one of the Brookings brand palettes.

  • get_cmap() returns a continuous palette (or color map) using one of the color Brookings color palettes.

  • view_palette() helper function that previews a color palette showing the color, order, and the appropriate text color that can be applied on top of each color.

  • add_title() adds titles and subtitles to a figure that are consistent with Brookings brand guidelines.

  • add_notes() adds notes to the bottom of a figure also consistent with Brookings brand guidelines.

  • add_logo() adds a program/center logo to a figure.

  • figure() creates a matplotlib figure in one of the standard Brookings sizes.

  • save() saves a figure in the Brookings advised dpi values depending on content type.

Examples

Let's create a figure plot using pyplotbrookings. First we'll need some data

import matplotlib.pyplot as plt
import pyplotbrookings.pyplotbrookings as ppb
import seaborn as sns

# Brookings plot theme for all plots
ppb.set_theme()

# Getting data
penguins = sns.load_dataset("penguins")

Now we can create a figure using the following

# Reversing data to get the correct z-order ordering of plots
ax = sns.histplot(data=penguins.iloc[::-1], 
                  x='bill_depth_mm', 
                  hue='species', 
                  # Setting the plot palette to Brookings brand2
                  palette=ppb.get_palette('brand2'), 
                  bins=20)

# Moving the legend in seaborn to the top
sns.move_legend(ax, "lower center", bbox_to_anchor=(.5, 1.05), ncol=3, title=None, frameon=False)
plt.xlabel('Bill Depth (mm)')

# Adding Brookings titles
ppb.add_title(title='Penguin Bill Depth', 
              subtitle='Bill Depth in mm of Adelie and Gentoo Penguins at Palmer Station LTER', 
              tag='FIGURE 1B')

# Adding notes
ppb.add_notes('Source: Palmer Station Antarctica LTER', 
              'Notes: Figure made using matplotlib')

ppb.add_logo('hc')

pyplotbrookings is designed to work with many different plots. Let's try creating a scatter plot that uses a colormap

# Getting the Brookings sequential2 color map
cmap = ppb.get_cmap('sequential2', reverse=True)
# Creating a scatter plot
plt.scatter(data=penguins, x='bill_length_mm', y='bill_depth_mm', c='flipper_length_mm', cmap=cmap)

# Adding matplotlib legend/labels
plt.xlabel('Bill Length (mm)')
plt.ylabel('Bill Depth (mm)')

# Adding color bar
cbar = plt.colorbar(cmap=cmap)
cbar.set_label('Flipper Length (mm)')
cbar.outline.set_visible(False)

# Adding Brookings titles
ppb.add_title(title='Penguin Bills and Flippers', 
              subtitle='Bill length vs. Bill Depth vs. Flipper Length of Penguins at\nPalmer Station', 
              tag='FIGURE 2')

# Adding notes
ppb.add_notes('Source: Palmer Station Antarctica LTER',
              'Example: Here is some extra long text that\ngoes on and on and needs a linefeed.',
              'Notes: Figure made using matplotlib')

# Adding a CRM logo
ppb.add_logo('crm', scale=0.35, offsets=(-0.1, 0))

We could also create a box plot. Note that titles and notes auto-align to the left margin of the figure.

# Creating a boxplot
sns.boxplot(data=penguins, x='bill_depth_mm', y='island', palette=ppb.get_palette('misc'))


# Adding Brookings titles
ppb.add_title(title='Penguin Bills Depth', 
              subtitle='Length of Penguin Bills on each Island', 
              tag='FIGURE 3')

# Adding notes
ppb.add_notes('Source: Palmer Station Antarctica LTER', 
              'Notes: Figure made using matplotlib')


# Adding matplotlib legend/labels
plt.xlabel('Bill Depth (mm)')
plt.ylabel('Island')

ppb.add_logo('crm', scale=0.35, offsets=(-0.1, 0))

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

pyplotbrookings-0.1.1.tar.gz (3.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyplotbrookings-0.1.1-py3-none-any.whl (2.6 MB view details)

Uploaded Python 3

File details

Details for the file pyplotbrookings-0.1.1.tar.gz.

File metadata

  • Download URL: pyplotbrookings-0.1.1.tar.gz
  • Upload date:
  • Size: 3.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.13

File hashes

Hashes for pyplotbrookings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3e02f97fcb9c7eeaae3a004a2a098937388637a49f691520484be891c1ef58c2
MD5 ebc88f592a537119aa540bcd66f3d58a
BLAKE2b-256 75e19f002f2c0bece19ab1bc57f75289b8365c253b4774f5b4c592186013b3c0

See more details on using hashes here.

File details

Details for the file pyplotbrookings-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pyplotbrookings-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 718c33605a3b80c4ad55ea97dbf7f8518fdba03760439b95c88a8d7008fee729
MD5 b27ee1d4dbe8f8ad479cc42e4b171076
BLAKE2b-256 a552df8d937771384f397bb510a6e1ea4be54dbbfd0e5b649addb8b01278d5d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page