Skip to main content

a tools suite to manage themes in matplotlib

Project description

ThemePy

An open source theme selector / creator and aesthetic manager for Matplotlib. This wrapper is intended to create an open-source community-driven theme library for matplotlib. The aim is to simplify the process of customising matplotlib plots and to enable users who are relatively new to python or matplotlib to move beyond the default plotting params we are given with matplotlib.

Taking a class-based approach, themes can now hold state. This makes it easier to keep track of colors and params that are usually set as variables, hardcoded, or added as args to matplotlib/pyplot functions. This allows us to reduce complexity of setting custom fonts etc... and gives us flexibility in moving between themes, not to mention the reduced typing by breaking down our interaction with matplotlib's rcParams into declarative functions.

Contributing

Contributors are welcome to this pacakge. The key idea behind this is to simplify interaction with matplotlib and have usable themes and modifications through declarative language. To acheive this each function should look to do just one thing and params for each function should be relatively few.

All contributing is handled through PRs (Pull Requests). In order to contri bute users must:

  1. Fork this repo and create branch from master
  2. Clone the environment - conda env create -f environment.yml
  3. Make changes to code or additions to codebase / new themes
  4. Test these to ensure that they function as expected
  5. Commit code and create a pull request outlining changes made

Contributing Themes

The main goal of this package is to have professional, well thought out themes available to all. If you would like to contribute a theme, look in the themes folder for a sample of how they should look. Including fonts in here is okay, but please only include fonts that are free and publicly available. If a font is included, please provide a url to the source in the PR. Similar to the above, to contribute themes:

  1. Fork this repo and create branch from master
  2. Clone the environment
  3. Add you theme .txt file to the themes folder
  4. Test these to ensure that they function as expected
  5. Commit code and create a pull request outlining your new theme, including its name. Do not commit images.

To get started after cloning, in the root of the repo, create a notebooks folder and inside create a new jupyter notebook. Add the following:

import sys
sys.path.append("..")
import themepy

Examples

This is still in early development. A python version >= 3.7 is required (just clone the env). There is an unstable version (0.0.8) available through pip - pip install thempy.

We can get started straight away by instantiating a Theme class. We can do this without passing a specific theme, this will use matplotlib's defaults.

import matplotlib.pyplot as plt
import themepy

theme = themepy.Theme()

if we created a plot it would look like a regular matplotlib plot.

# creating random data
np.random.seed(402)
x = np.random.uniform(0,1,50)
y = np.random.uniform(0,1,50)

# can adjust themes and params here
(theme
 .set_theme() # we could change to a different theme here. Passing no theme resets theme to default matplotlib
 .set_font() # we could set a new font here
 .set_pips() # we could set the tick marks on the x and y axis on or off here
 .set_spines() # we could show or hide the boarders of the plot here
 .set_ticklabel_size() # we could change the size of the x and y tick labels here
)


# create plot
fig, ax = plt.subplots(figsize=(8,8))
ax.set_title("This is a title using {} theme".format(theme.theme_name),
             loc="left", **theme.title_font, fontsize=18, fontweight="bold")


ax.grid(linewidth=.25, zorder=1)

ax.scatter(x,y,
           color=theme.primary_color, # the first colour in cycle, our primary color 
           edgecolors=theme.background, # our background colour (figure.facecolor)
           s=400,
           zorder=2)

ax.set_xlim(0,1)
ax.set_ylim(0,1)

plt.show()

However, we can now make some quick alterations to our plot


# creating random data
np.random.seed(402)
x = np.random.uniform(0,1,50)
y = np.random.uniform(0,1,50)

# adjusting font, pips, spines, and tick size here
(theme
 .set_font("Century Gothic")
 .set_pips(False)
 .set_spines("off", which=["top","right"])
 .set_ticklabel_size(12)
)


# create plot
fig, ax = plt.subplots(figsize=(8,8))
ax.set_title("This is a title using {} theme".format(theme.theme_name),
             loc="left", **theme.title_font, fontsize=18, fontweight="bold")


ax.grid(linewidth=.25, zorder=1)

ax.scatter(x,y,
           color=theme.primary_color, # the first colour in cycle, our primary color 
           edgecolors=theme.background, # our background colour (figure.facecolor)
           s=400,
           zorder=2)

ax.set_xlim(0,1)
ax.set_ylim(0,1)

plt.show()

theme.title_font and theme.body_font take the font passed either through the theme or through theme.set_font, but they can also be set individually.

There are two sample themes included. Dark version shown below:


# creating random data
np.random.seed(402)
x = np.random.uniform(0,1,50)
y = np.random.uniform(0,1,50)

# can adjust themes and params here
(theme
 .set_theme('sample-dark')
 .set_font("Century Gothic")
 .set_pips(False)
 .set_spines("off", which=["top","right"]) 
 .set_ticklabel_size(12)
)


# create plot
fig, ax = plt.subplots(figsize=(8,8))
ax.set_title("This is a title using {} theme".format(theme.theme_name),
             loc="left", **theme.title_font, fontsize=18, fontweight="bold")


ax.grid(linewidth=.25, zorder=1)

ax.scatter(x,y,
           color=theme.primary_color, # the first colour in cycle, our primary color 
           edgecolors=theme.background, # our background colour (figure.facecolor)
           s=400,
           zorder=2)

ax.set_xlim(0,1)
ax.set_ylim(0,1)

plt.show()

Adding Themes

to do

To Do:

  • theme colors should be appended to the start of cycler instead of replacing

  • add add_theme() function

  • add remove_theme() function

  • Docstring on Theme class

  • Add Adding Themes section to readme

  • Testing script

  • Apply code formatting and guidelines for contributors

  • ?? List/set theme(s) function read from git repo with fallback to local themes dir, possibly with the option to pull latest theme files to local themes directory

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

themepy-0.0.9.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

themepy-0.0.9-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file themepy-0.0.9.tar.gz.

File metadata

  • Download URL: themepy-0.0.9.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200712 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8

File hashes

Hashes for themepy-0.0.9.tar.gz
Algorithm Hash digest
SHA256 68a8a3de44f85caf9c543b28f12435957613f0c7dce5b1258569c82fe3829790
MD5 5617fee689cad3eb69a8a3d9dbacfdfc
BLAKE2b-256 9f9536ee21afe80e2463e9b26eb34fafa8e39a7f0fe0300e3c0a0969209424bc

See more details on using hashes here.

File details

Details for the file themepy-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: themepy-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200712 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.8

File hashes

Hashes for themepy-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 5a55fd673499bf6d35c75ea6f8b912a392a9335d2a6ae883a368066c5f47c964
MD5 bf681dc1ecea863231adf07ff83cc6e6
BLAKE2b-256 e777bd3bd813a3dbb905980658e2c8a6488db6c6062b79b45783b443b68fe32d

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