Skip to main content

Easily set themes that automatically apply to matplotlib, seaborn, and pandas plots

Project description

pyplot-themes

Themes you can see that apply to matplotlib, seaborn, and pandas plots.

This package was inspired by the ggthemes package in R, and the code influenced from the seaborn package in python (specifically rcmod.py).

Installing

Install from PyPI

pip install pyplot-themes

Or directly from GitHub

pip install git+https://github.com/raybuhr/pyplot-themes.git

Usage

Environment

import sys
sys.version
'3.7.1 (default, Dec 14 2018, 19:28:38) \n[GCC 7.3.0]'
import matplotlib.pyplot as plt
from seaborn import palplot # only used to show off palettes 

from string import ascii_uppercase
import numpy as np


def example_scatter_plot(num_cats=6):
    for i in range(num_cats):
        cat = ascii_uppercase[i]
        x = np.random.random(100)
        y = np.random.random(100) + i
        plt.scatter(x, y, marker='o', label=cat)
    plt.legend(loc='best')


def example_bar_plot(num_cats=6):
    bar_width = 1 / num_cats + 1
    for i in range(num_cats):
        cat = ascii_uppercase[i]
        x = np.arange(11) + 5 * i
        y = np.array([0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]) + np.random.random(1)
        plt.bar(x, y, label=cat, width=bar_width)
    plt.legend(loc='best')


def example_plots(num_cats=6):
    example_scatter_plot(num_cats)
    plt.show()
    example_bar_plot(num_cats)
    plt.show()

Default Maplotlib Theme

example_plots()

png

png

As you can see, the default theme has good contrast in colors, but leaves a bit to be desired in the sige of the chart (i.e. figure size aka figsize) and font.

Usage

import pyplot_themes as themes
themes.__version__
'0.2.0'
themes.theme_minimal()

This updates the global theme settings for matplotlib with a nice minimal style using colorblind safe colors.

palplot(themes.palettes.Colorblind.colors)

png

example_plots()

png

png

As you can see, our plots are much larger now, have accessible colors, and have some light gridlines to make identifying values a bit easier.

There are a few parameters available in all themes:

  • grid: toggles grid lines on/off
  • ticks: toggles tick marks on/off
  • figsize: sets the default size of plots (you can still change each plot in an ad hoc manner if needed)
  • fontsize: sets the default font size to be used

Some themes will allow you to pass in whatever colors you want, others you have to pick a color scheme from available options, some only let you reverse the order of the default color palette, and some don't let you mess with the colors at all. Experiment and find out what you like.

themes.theme_minimal(grid=False, ticks=False, fontsize=18)
example_scatter_plot()
plt.title("Look Mom, no lines!")
Text(0.5, 1.0, 'Look Mom, no lines!')

png

Themes

themes.theme_dark()
example_plots()

png

png

themes.theme_tableau()
example_plots()

png

png

palplot(themes.palettes.Solarized.dark)

png

themes.theme_solarized(scheme="dark")
example_plots()

png

png

palplot(themes.palettes.Solarized.light)

png

themes.theme_solarized(scheme="light")
example_plots()

png

png

palplot(themes.palettes.PaulTolColorSchemes.colors)

png

themes.theme_paul_tol()
example_plots(12)

png

png

themes.theme_paul_tol(reverse_colors=True, grid=False)
example_plots(num_cats=12)

png

png

palplot(themes.palettes.Few.light)
palplot(themes.palettes.Few.medium)
palplot(themes.palettes.Few.dark)

png

png

png

themes.theme_few(scheme="light")
example_plots()

png

png

themes.theme_few(scheme="medium", figsize=[5, 5])
example_scatter_plot()

png

themes.theme_few(scheme="dark")
example_bar_plot()

png

themes.theme_ucberkeley(figsize=[10, 5])
example_plots(num_cats=4)

png

png

themes.theme_ucberkeley(scheme="all", figsize=[12, 6])
example_plots(num_cats=16)

png

png

Themes that come with matplotlib

These next themes actually come with matplotlib and you can use them without the pyplot-themes package. The functions here are basically thin wrappers for calling the matplotlib defined styles, but use a bigger figsize by default.

themes.theme_fivethirtyeight()
example_plots()

png

png

themes.theme_ggplot2(figsize=[10, 5])
example_plots()

png

png

bmh stands for Bayesian Methods for Hackers

themes.theme_bmh()
example_scatter_plot()

png

So we also have an alias for the spelled out version to make it easier to discover

themes.theme_bayesian_methods_for_hackers()
example_bar_plot()

png

While this package provides light and dark solarized themes, matplotlib comes with a light version as well. This one is a good choice if you want to keep more contrast in the colors of your plots.

themes.theme_solarized_light2()
example_plots()

png

png

Modifying Themes

In addition to making it easy to find and call the matplotlib themes, pyplot-themes also makes it easier to modify them slightly. For example say you want to use the ggplot2 theme, but you want to use the Paul Tol Color Schemes palette with it.

themes.theme_ggplot2(palette=themes.palettes.PaulTolColorSchemes.colors, figsize=[12, 6])
example_bar_plot(num_cats=12)

png

Or maybe the fivethirtyeight colors

themes.theme_ggplot2(palette=themes.palettes.FiveThirtyEight.colors)
example_bar_plot()

png

Resetting to back to matplotlib defaults

Of course, sometimes when you are trying out different themes, you may find you modified a setting that you didn't quite like, but aren't sure what changed. To aid in debugging, we created a function to reset the theme back to what matplotlib starts with. Of course, you may just like the matplotlib defaults and that's ok.

Note: The default settings for matplotlib can be slightly different depending on if you are using in python files (e.g. scripts) vs. in jupyter notebooks using %matplotlib inline. The reset function assumes you are using a notebook by default, but provides a parameter to toggle that off if you are not:

themes.theme_reset(notebook=False)
themes.theme_reset()  # could also use the alias `themes.theme_matplotlib_default()`
example_bar_plot()

png

Palettes

In addition to the themes above, there are a bunch of color palettes provided. Here are a few to show off.

palplot(themes.palettes.Autumn1.colors)

png

palplot(themes.palettes.Autumn2.colors)

png

palplot(themes.palettes.Canyon.colors)

png

palplot(themes.palettes.Chili.colors)

png

palplot(themes.palettes.Tomato.colors)

png

palplot(themes.palettes.Few.medium)

png

palplot(themes.palettes.FiveThirtyEight.colors)

png

palplot(themes.palettes.Solarized.light)
palplot(themes.palettes.Solarized.dark)

png

png

palplot(themes.palettes.UCBerkeley.primary_colors)
palplot(themes.palettes.UCBerkeley.secondary_colors)

png

png

Sequential Palettes

palplot(themes.palettes.Sequential.blues)
palplot(themes.palettes.Sequential.cyans)
palplot(themes.palettes.Sequential.purples)

png

png

png

palplot(themes.palettes.Sequential.greens)
palplot(themes.palettes.Sequential.oranges)
palplot(themes.palettes.Sequential.reds)

png

png

png

Diverging Palettes

palplot(themes.palettes.Diverging.blueorange)
palplot(themes.palettes.Diverging.orangeblue)

png

png

palplot(themes.palettes.Diverging.bluepurple)
palplot(themes.palettes.Diverging.purpleblue)

png

png

palplot(themes.palettes.Diverging.bluered)
palplot(themes.palettes.Diverging.redblue)

png

png

palplot(themes.palettes.Diverging.greenpurple)
palplot(themes.palettes.Diverging.purplegreen)

png

png

palplot(themes.palettes.Diverging.greenred)
palplot(themes.palettes.Diverging.redgreen)

png

png

Using with Pandas

import pandas as pd
# some made up date
sales = np.random.randint(low=10, high=20, size=30) * [i**2 for i in range(1, 31)]
revenue = np.random.random(30) * sales
months = pd.date_range(start="2010-01-01", periods=30, freq="M")

df = pd.DataFrame({"sales": sales, "revenue": revenue.round(2)}, index=months)
df.head()
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
sales revenue
2010-01-31 12 2.76
2010-02-28 52 45.05
2010-03-31 90 11.80
2010-04-30 208 203.93
2010-05-31 475 337.08
themes.theme_minimal()
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7fdc5285f2b0>

png

themes.theme_dark(palette=themes.palettes.Autumn1.colors)
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7fdc5297e668>

png

Contributing

There are multiple ways you can help out with this project:

  • submit a bug report
  • submit a feature request
  • Fork this git repo, change some code, and submit a Pull Request
    • adding documentation or examples counts as changing code

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

pyplot-themes-0.2.2.tar.gz (16.5 kB view hashes)

Uploaded Source

Built Distribution

pyplot_themes-0.2.2-py2.py3-none-any.whl (12.1 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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