Example of using the mplstyle package

With the package you have the following possibilities to define your style:

  • plt_style: Set the formattingn; default: default
  • color_style: Set the colors; default: default
  • color_order_style: Set the color order; default: default

In [1]:
%matplotlib inline

In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

Import mplstyle class

when initializing, the default style is automatically set


In [3]:
from mplstyle.enfo import PLTenfo
from mplstyle.tz import PLTtz
enfo_plt = PLTenfo()
tz_plt = PLTtz()

Read the data


In [4]:
ghd_bs = pd.read_csv('data/ghd_bs.csv')
ghd_bs = ghd_bs.set_index('jahr')

In [5]:
ghd_bs.head(6)


Out[5]:
Steinkohle Braunkohle Öl Gas Strom Fernwärme Erneuerbare Energien
jahr
2011 15 2 302 390 507 86 44
2020 4 0 167 269 458 78 127
2025 2 0 127 213 438 70 153
2030 1 0 99 166 421 62 172
2040 0 0 65 102 395 48 197
2050 0 0 46 66 383 39 219

In [6]:
ghd_bs.Gas.describe()


Out[6]:
count      6.000000
mean     201.000000
std      118.101651
min       66.000000
25%      118.000000
50%      189.500000
75%      255.000000
max      390.000000
Name: Gas, dtype: float64

Set plt_style


In [7]:
tz_plt.get_available_styles()


Available styles:

color_style: ['default', 'blue', 'mpl2_colors']
color_order_style: ['default', 'blue', 'mpl2_colors']
plt_style: ['default', 'jupyter-notebook']

In [8]:
tz_plt.set_style(plt_style='jupyter-notebook')

Set color_style


In [9]:
enfo_plt.get_available_styles()


Available styles:

color_style: ['default', 'enfo']
color_order_style: ['default']
plt_style: ['default', 'enfo']

In [10]:
enfo_plt.set_style(color_style='enfo')

In [11]:
colors = enfo_plt.get_colors()

In [12]:
enfo_plt.get_colors().keys()


Out[12]:
dict_keys(['Steinkohle', 'Braunkohle', 'Mineralölprodukte', 'Mineralöle', 'Öl', 'Gas', 'Erdgas', 'Nichterneuerbare Abfälle', 'Strom', 'Fernwärme', 'Erneuerbare Energien', 'Beleuchtung', 'Mechanische Energie', 'IKT', 'Prozesswärme', 'Warmwasser', 'Raumwärme', 'Prozesskälte', 'Klimakälte', 'Kühlen/Lüften/Haustechnik', 'Sonstige', 'Wasserstoff', 'Personenverkehr', 'Güterverkehr', 'Kernenergie', 'Nettoimporte Strom', 'Sonstige Sekundärenergieträger'])

Plot figuere: Energy consumption


In [13]:
fig, (ax0, ax1) = plt.subplots(2, 1, figsize=[9, 6],
                               gridspec_kw={'height_ratios': [1, 0.1]})
ax1.axis('off')

color = [colors[i] for i in ghd_bs.columns]
ghd_bs.plot.bar(ax=ax0, stacked=True, width=0.5, edgecolor=None, 
                zorder=2, rot=0, legend=False, color=color)

handles, labels = ax0.get_legend_handles_labels()
ax0.legend(handles, labels, loc='upper center', ncol=3,
          bbox_to_anchor=(0.5, -0.05))

ax0.set_xlabel('').set_visible(False)
ax0.set_title('Endenergie PJ', fontsize=20, fontweight='bold')


Out[13]:
Text(0.5,1,'Endenergie PJ')

View documentation


In [14]:
enfo_plt.set_style?


Signature: enfo_plt.set_style(*args, **kwargs)
Docstring:
Set plot style, colors and color order.

Args:
    *args: optional string for the uniform style; i.e. non-key worded
        ('default')
    plt_style: optional string for plt style;
        (plt_style='default')
    color_style: optional string for color order;
        (color_style='default')
    color_order_style: optional string for color order style;
            (color_order_style='default')
File:      d:\tzipperle\python-tools\mplstyle\mplstyle\base.py
Type:      method

In [15]:
%whos


Variable   Type           Data/Info
-----------------------------------
PLTenfo    type           <class 'mplstyle.enfo.PLTenfo'>
PLTtz      type           <class 'mplstyle.tz.PLTtz'>
ax0        AxesSubplot    AxesSubplot(0.125,0.243636;0.775x0.636364)
ax1        AxesSubplot    AxesSubplot(0.125,0.11;0.775x0.0636364)
color      list           n=7
colors     dict           n=27
enfo_plt   PLTenfo        <mplstyle.enfo.PLTenfo ob<...>ct at 0x000000000AA0CF28>
fig        Figure         Figure(900x600)
ghd_bs     DataFrame            Steinkohle  Braunko<...> 39                   219
handles    list           n=7
labels     list           n=7
tz_plt     PLTtz          <mplstyle.tz.PLTtz object at 0x000000000AA0CEF0>