Skip to content
forked from smortezah/mplstyle

Matplotlib style sheet based on ggplot2 (in R)

License

Notifications You must be signed in to change notification settings

asilab/mplstyle

 
 

Repository files navigation

Overview

Matplotlib style sheet based on ggplot2.

Currently, the following styles are available:

  • 'ggplot_bw': to mimic 'theme_bw'
  • 'ggplot_classic': to mimic 'theme_classic'

Requirements

  • Matplotlib >= 3.4.0
    • To make previous versions work, 'xtick.labelcolor: 4d4d4d' and 'ytick.labelcolor: 4d4d4d' should be removed from the style sheets.

Installation

Just copy the style file, i.e. 'ggplot_bw.mplstyle' or 'ggplot_classic.mplstyle', where your Python source codes are.

Usage

import matplotlib.pyplot as plt
plt.style.use('ggplot_bw.mplstyle') # or ggplot_classic.mplstyle

or

import matplotlib.pyplot as plt
with plt.style.context('ggplot_bw.mplstyle'):
    # PLOTTING FUNCTION

Example

You can run 'example.py', that basically generates the following results.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

ggplot_palette = sns.palettes.hls_palette(n_colors=1, l=0.65, s=1.0)
sns.set_palette(palette=ggplot_palette)

def plot1(title, filename):
    data = pd.DataFrame({
        'x': np.random.randn(50),
        'y': np.random.randn(50),
        'z': np.random.choice(['pooh', 'rabbit', 'piglet', 'Christopher'], 50),
    })

    fig, ax = plt.subplots()
    ax = sns.scatterplot(data=data, x='x', y='y', hue='z')
    ax.set(xlabel='Label X (0123456789 test)',
           ylabel='Label Y (9876543210)', title=title)
    lgnd = ax.legend(title='Title of legend', loc='center',
                     bbox_to_anchor=(1.15, 0.5))
    lgnd._legend_box.align = "left"
    fig.savefig(filename + '.jpg', bbox_inches='tight', dpi=150)

with plt.style.context('ggplot_bw.mplstyle'):
    plot1('ggplot_bw style', 'example-ggplot_bw')

with plt.style.context('ggplot_classic.mplstyle'):
    plot1('ggplot_classic style', 'example-ggplot_classic')

example-ggplot_bw example-ggplot_classic

About

Matplotlib style sheet based on ggplot2 (in R)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%