Creating your own styles

A question arises: can we actually make our own style sheet? The answer to that, of course, is yes. The first thing you need to know is where to put these style sheets.

So, when we run mpl.get_configdir, you can see we get a directory where the Matplotlib configuration options are stored and here we can actually place new style sheets:

# Where do we put out style sheets?
mpl.get_configdir()

We will thus generate our simple plot again by using a classic style sheet. Let's build a new style sheet; we will build something simple that will change just a single attribute. To build a new style sheet, we will follow these steps:

  1. We will make a directory called stylelib—this will be where style files are actually stored, and these style sheets will live in this stylelib directory:
$ ls
$ mkdir stylelib
$ cd stylelib
  1. We will make one style sheet and name it bigpoints.mplstyle.
.mplstyle is the file name extension for Matplotlib style sheets.
  1. Insert the marker size and make it 20:
lines.markersize: 20
  1. We will restart the kernel by clicking on the Kernel tab and then on Restart since we have created this new style sheet that only gets loaded in when Matplotlib is actually started up, as shown in the following screenshot:

  1. By going back to the Jupyter Notebook, we will reimport the following packages:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150
  1. Next, we call plt.style.available and we see the addition to all of the big points, as shown in the following output:

  1. By running plt.style.use('bigpoints'), we can see it's changed the size of the points, as shown here:
# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
with plt.style.context('ggplot'):
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

We will get the following output:

  1. So, by typing ko, we get black dots. In the following image after the code snippet, you can see that those are quite big:
# Custom styles
plt.style.use('bigpoints')
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

From the preceding code, we get the following output:

  1. We will go back and edit this and insert 50 points (more than twice what was shown earlier):
lines.markersize: 50
  1. After reimporting matplotlib, run plt.style.use('bigpoints') using a new style sheet; we can see that the points are bigger than they were before.

Styles can also be composed using two sets of styles, so you can combine the attributes of two different style sheets to generate things that have different combinations, and the way to do that is actually to use a list for plt.style.use.

By typing ggplot and composing that with dark_background, all of the subsequent changes that the dark background provides overwrites the changes that ggplot provides:

# Composing styles
plt.style.use(['ggplot', 'dark background'])
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3), 'ko')
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

We will get the following output:

So, in other words, what we have here is a combination of ggplot modules and dark background changes, hence all of the things that ggplot changes, the dark background does not. All of the changes that both ggplot and the dark background change use the changes from the dark background, and all of the changes that ggplot does not make but the dark background does get applied.

It's kind of like an overwrite, wherein the dark background is applied after ggplot is applied. You can make custom style sheets and then compose them so that each style sheet can work together. So, for example, you can have a style sheet that will change line attributes that you can then compose with a style sheet that will change the axis or label attribute, and you can use that to modify the built-in default Matplotlib style sheets.

Taking an example, if you really loved the look of ggplot but you didn't quite like one little attribute of it, you could actually go in and change that, as follows:

# Composing styles
plt.style.use(['ggplot'])
from scipy.ndimage.filters import gaussian_filter
plt.subplot(221)
plt.plot(np.arange(0,1,0.01), np.power(np.arange(0,1,0.01), 3))
plt.ylabel('Axis Label')
plt.subplot(222)
plt.scatter(np.random.normal(size=10), np.random.normal(size=10), c=np.random.normal(size=10))
plt.subplot(223)
plt.hist(np.random.normal(size=1000));
plt.hist(np.random.normal(1, size=1000));
plt.hist(np.random.normal(2, size=500));
plt.ylabel('Axis Label')
plt.xlabel('Axis Label')
plt.subplot(224)
plt.imshow(gaussian_filter(np.random.normal(size=(200,300)), sigma=10))
plt.xlabel('Axis Label')

If you want to turn off these grid labels, you can create a new style sheet, disable grid labels, compose that with the ggplot style sheet, and get a plot that looks exactly the same but without those white grid lines, as follows:

In the next section, we are going to take a deep dive into looking at colors within Matplotlib and how to configure and customize them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset