š2.3: Matplotlib Styling
Table of Contents
- Customizing Legends
- Choosing Elements for the Legend
- Legend for Size of Points
- Customizing Colorbars
- Customizing Colorbars
- Text and Annotation
- Example: Effect of Holidays on US Births
- Transforms and Text Position
- Arrows and Annotation
- Stylesheets
- Plot Customization by Hand
- Changing the Defaults: rcParams
- Stylesheets
Customizing Legends
Plot legends give meaning to a visualization, assigning meaning to the various plot elements. We previously saw how to create a simple legend; here weāll take a look at customizing the placement and aesthetics of the legend in Matplotlib.
The simplest legend can be created with the plt.legend
command, which automatically creates a legend for any labeled plot elements (see the following figure):
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
%matplotlib inline
import numpy as np
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), '-b', label='Sine')
ax.plot(x, np.cos(x), '--r', label='Cosine')
ax.axis('equal')
leg = ax.legend()
But there are many ways we might want to customize such a legend. For example, we can specify the location and turn on the frame (see the following figure):
ax.legend(loc='upper left', frameon=True)
fig
We can use the ncol
command to specify the number of columns in the legend, as shown in the following figure:
ax.legend(loc='lower center', ncol=2)
fig
And we can use a rounded box (fancybox
) or add a shadow, change the transparency (alpha value) of the frame, or change the padding around the text (see the following figure):
ax.legend(frameon=True, fancybox=True, framealpha=1,
shadow=True, borderpad=1)
fig
For more information on available legend options, see the plt.legend
docstring.
Choosing Elements for the Legend
As we have already seen, by default the legend includes all labeled elements from the plot. If this is not what is desired, we can fine-tune which elements and labels appear in the legend by using the objects returned by plot
commands. plt.plot
is able to create multiple lines at once, and returns a list of created line instances. Passing any of these to plt.legend
will tell it which to identify, along with the labels weād like to specify (see the following figure):
y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5))
lines = plt.plot(x, y)
# lines is a list of plt.Line2D instances
plt.legend(lines[:2], ['first', 'second'], frameon=True);
I generally find in practice that it is clearer to use the first method, applying labels to the plot elements youād like to show on the legend (see the following figure):
plt.plot(x, y[:, 0], label='first')
plt.plot(x, y[:, 1], label='second')
plt.plot(x, y[:, 2:])
plt.legend(frameon=True);
Notice that the legend ignores all elements without a label
attribute set.
Legend for Size of Points
Sometimes the legend defaults are not sufficient for the given visualization. For example, perhaps youāre using the size of points to mark certain features of the data, and want to create a legend reflecting this. Here is an example where weāll use the size of points to indicate populations of California cities. Weād like a legend that specifies the scale of the sizes of the points, and weāll accomplish this by plotting some labeled data with no entries (see the following figure):
# Uncomment to download the data
# url = ('https://raw.githubusercontent.com/jakevdp/PythonDataScienceHandbook/'
# 'master/notebooks/data/california_cities.csv')
# !cd data && curl -O {url}
import pandas as pd
cities = pd.read_csv('data/california_cities.csv')
# Extract the data we're interested in
lat, lon = cities['latd'], cities['longd']
population, area = cities['population_total'], cities['area_total_km2']
# Scatter the points, using size and color but no label
plt.scatter(lon, lat, label=None,
c=np.log10(population), cmap='viridis',
s=area, linewidth=0, alpha=0.5)
plt.axis('equal')
plt.xlabel('longitude')
plt.ylabel('latitude')
plt.colorbar(label='log$_{10}$(population)')
plt.clim(3, 7)
# Here we create a legend:
# we'll plot empty lists with the desired size and label
for area in [100, 300, 500]:
plt.scatter([], [], c='k', alpha=0.3, s=area,
label=str(area) + ' km$^2$')
plt.legend(scatterpoints=1, frameon=False, labelspacing=1, title='City Area')
plt.title('California Cities: Area and Population');
The legend will always reference some object that is on the plot, so if weād like to display a particular shape we need to plot it. In this case, the objects we want (gray circles) are not on the plot, so we fake them by plotting empty lists. Recall that the legend only lists plot elements that have a label specified.
By plotting empty lists, we create labeled plot objects that are picked up by the legend, and now our legend tells us some useful information. This strategy can be useful for creating more sophisticated visualizations.
Customizing Colorbars
Plot legends identify discrete labels of discrete points. For continuous labels based on the color of points, lines, or regions, a labeled colorbar can be a great tool. In Matplotlib, a colorbar is drawn as a separate axes that can provide a key for the meaning of colors in a plot. Because the book is printed in black and white, this chapter has an accompanying online supplement where you can view the figures in full color. Weāll start by setting up the notebook for plotting and importing the functions we will use:
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
%matplotlib inline
import numpy as np
As we have seen several times already, the simplest colorbar can be created with the plt.colorbar
function (see the following figure):
x = np.linspace(0, 10, 1000)
I = np.sin(x) * np.cos(x[:, np.newaxis])
plt.imshow(I)
plt.colorbar();
Weāll now discuss a few ideas for customizing these colorbars and using them effectively in various situations.
Customizing Colorbars
The colormap can be specified using the cmap
argument to the plotting function that is creating the visualization (see the following figure):
plt.imshow(I, cmap='Blues');
The names of available colormaps are in the plt.cm
namespace; using IPythonās tab completion feature will give you a full list of built-in possibilities:
plt.cm.<TAB>
But being able to choose a colormap is just the first step: more important is how to decide among the possibilities! The choice turns out to be much more subtle than you might initially expect.
Choosing the Colormap
A full treatment of color choice within visualizations is beyond the scope of this book, but for entertaining reading on this subject and others, see the article āTen Simple Rules for Better Figuresā by Nicholas Rougier, Michael Droettboom, and Philip Bourne. Matplotlibās online documentation also has an interesting discussion of colormap choice.
Broadly, you should be aware of three different categories of colormaps:
- Sequential colormaps: These are made up of one continuous sequence of colors (e.g.,
binary
orviridis
). - Divergent colormaps: These usually contain two distinct colors, which show positive and negative deviations from a mean (e.g.,
RdBu
orPuOr
). - Qualitative colormaps: These mix colors with no particular sequence (e.g.,
rainbow
orjet
).
The jet
colormap, which was the default in Matplotlib prior to version 2.0, is an example of a qualitative colormap. Its status as the default was quite unfortunate, because qualitative maps are often a poor choice for representing quantitative data. Among the problems is the fact that qualitative maps usually do not display any uniform progression in brightness as the scale increases.
We can see this by converting the jet
colorbar into black and white (see the following figure):
from matplotlib.colors import LinearSegmentedColormap
def grayscale_cmap(cmap):
"""Return a grayscale version of the given colormap"""
cmap = plt.cm.get_cmap(cmap)
colors = cmap(np.arange(cmap.N))
# Convert RGBA to perceived grayscale luminance
# cf. http://alienryderflex.com/hsp.html
RGB_weight = [0.299, 0.587, 0.114]
luminance = np.sqrt(np.dot(colors[:, :3] ** 2, RGB_weight))
colors[:, :3] = luminance[:, np.newaxis]
return LinearSegmentedColormap.from_list(
cmap.name + "_gray", colors, cmap.N)
def view_colormap(cmap):
"""Plot a colormap with its grayscale equivalent"""
cmap = plt.cm.get_cmap(cmap)
colors = cmap(np.arange(cmap.N))
cmap = grayscale_cmap(cmap)
grayscale = cmap(np.arange(cmap.N))
fig, ax = plt.subplots(2, figsize=(6, 2),
subplot_kw=dict(xticks=[], yticks=[]))
ax[0].imshow([colors], extent=[0, 10, 0, 1])
ax[1].imshow([grayscale], extent=[0, 10, 0, 1])
view_colormap('jet')
Notice the bright stripes in the grayscale image. Even in full color, this uneven brightness means that the eye will be drawn to certain portions of the color range, which will potentially emphasize unimportant parts of the dataset. Itās better to use a colormap such as viridis
(the default as of Matplotlib 2.0), which is specifically constructed to have an even brightness variation across the range; thus, it not only plays well with our color perception, but also will translate well to grayscale printing (see the following figure):
view_colormap('viridis')
For other situations, such as showing positive and negative deviations from some mean, dual-color colorbars such as RdBu
(RedāBlue) are helpful. However, as you can see in the following figure, itās important to note that the positive/negative information will be lost upon translation to grayscale!
view_colormap('RdBu')
Weāll see examples of using some of these colormaps as we continue.
Color Limits and Extensions
Matplotlib allows for a large range of colorbar customization. The colorbar itself is simply an instance of plt.Axes
, so all of the axes and tick formatting tricks weāve seen so far are applicable. The colorbar has some interesting flexibility: for example, we can narrow the color limits and indicate the out-of-bounds values with a triangular arrow at the top and bottom by setting the extend
property. This might come in handy, for example, if displaying an image that is subject to noise (see the following figure):
# make noise in 1% of the image pixels
speckles = (np.random.random(I.shape) < 0.01)
I[speckles] = np.random.normal(0, 3, np.count_nonzero(speckles))
plt.figure(figsize=(10, 3.5))
plt.subplot(1, 2, 1)
plt.imshow(I, cmap='RdBu')
plt.colorbar()
plt.subplot(1, 2, 2)
plt.imshow(I, cmap='RdBu')
plt.colorbar(extend='both')
plt.clim(-1, 1)
Notice that in the left panel, the default color limits respond to the noisy pixels, and the range of the noise completely washes out the pattern we are interested in. In the right panel, we manually set the color limits and add extensions to indicate values that are above or below those limits. The result is a much more useful visualization of our data.
Discrete Colorbars
Colormaps are by default continuous, but sometimes youād like to represent discrete values. The easiest way to do this is to use the plt.cm.get_cmap
function and pass the name of a suitable colormap along with the number of desired bins (see the following figure):
plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))
plt.colorbar(extend='both')
plt.clim(-1, 1);
The discrete version of a colormap can be used just like any other colormap.
Text and Annotation
Creating a good visualization involves guiding the reader so that the figure tells a story. In some cases, this story can be told in an entirely visual manner, without the need for added text, but in others, small textual cues and labels are necessary. Perhaps the most basic types of annotations you will use are axes labels and titles, but the options go beyond this. Letās take a look at some data and how we might visualize and annotate it to help convey interesting information. Weāll start by setting up the notebook for plotting and importing the functions we will use:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('seaborn-whitegrid')
import numpy as np
import pandas as pd
Example: Effect of Holidays on US Births
Letās return to some data we worked with earlier, in Example: Birthrate Data, where we generated a plot of average births over the course of the calendar year. Weāll start with the same cleaning procedure we used there, and plot the results (see the following figure):
# shell command to download the data:
# !cd data && curl -O \
# https://raw.githubusercontent.com/jakevdp/data-CDCbirths/master/births.csv
from datetime import datetime
births = pd.read_csv('data/births.csv')
quartiles = np.percentile(births['births'], [25, 50, 75])
mu, sig = quartiles[1], 0.74 * (quartiles[2] - quartiles[0])
births = births.query('(births > @mu - 5 * @sig) & (births < @mu + 5 * @sig)')
births['day'] = births['day'].astype(int)
births.index = pd.to_datetime(10000 * births.year +
100 * births.month +
births.day, format='%Y%m%d')
births_by_date = births.pivot_table('births',
[births.index.month, births.index.day])
births_by_date.index = [datetime(2012, month, day)
for (month, day) in births_by_date.index]
fig, ax = plt.subplots(figsize=(12, 4))
births_by_date.plot(ax=ax);
When weāre visualizing data like this, it is often useful to annotate certain features of the plot to draw the readerās attention. This can be done manually with the plt.text
/ax.text
functions, which will place text at a particular x/y value (see the following figure):
fig, ax = plt.subplots(figsize=(12, 4))
births_by_date.plot(ax=ax)
# Add labels to the plot
style = dict(size=10, color='gray')
ax.text('2012-1-1', 3950, "New Year's Day", **style)
ax.text('2012-7-4', 4250, "Independence Day", ha='center', **style)
ax.text('2012-9-4', 4850, "Labor Day", ha='center', **style)
ax.text('2012-10-31', 4600, "Halloween", ha='right', **style)
ax.text('2012-11-25', 4450, "Thanksgiving", ha='center', **style)
ax.text('2012-12-25', 3850, "Christmas ", ha='right', **style)
# Label the axes
ax.set(title='USA births by day of year (1969-1988)',
ylabel='average daily births')
# Format the x-axis with centered month labels
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%h'));
The ax.text
method takes an x position, a y position, a string, and then optional keywords specifying the color, size, style, alignment, and other properties of the text. Here we used ha='right'
and ha='center'
, where ha
is short for horizontal alignment. See the docstrings of plt.text
and mpl.text.Text
for more information on the available options.
Transforms and Text Position
In the previous example, we anchored our text annotations to data locations. Sometimes itās preferable to anchor the text to a fixed position on the axes or figure, independent of the data. In Matplotlib, this is done by modifying the transform.
Matplotlib makes use of a few different coordinate systems: a data point at $(x, y) = (1, 1)$ corresponds to a certain location on the axes or figure, which in turn corresponds to a particular pixel on the screen. Mathematically, transforming between such coordinate systems is relatively straightforward, and Matplotlib has a well-developed set of tools that it uses internally to perform these transforms (these tools can be explored in the matplotlib.transforms
submodule).
A typical user rarely needs to worry about the details of the transforms, but it is helpful knowledge to have when considering the placement of text on a figure. There are three predefined transforms that can be useful in this situation:
ax.transData
: Transform associated with data coordinatesax.transAxes
: Transform associated with the axes (in units of axes dimensions)fig.transFigure
: Transform associated with the figure (in units of figure dimensions)
Letās look at an example of drawing text at various locations using these transforms (see the following figure):
fig, ax = plt.subplots(facecolor='lightgray')
ax.axis([0, 10, 0, 10])
# transform=ax.transData is the default, but we'll specify it anyway
ax.text(1, 5, ". Data: (1, 5)", transform=ax.transData)
ax.text(0.5, 0.1, ". Axes: (0.5, 0.1)", transform=ax.transAxes)
ax.text(0.2, 0.2, ". Figure: (0.2, 0.2)", transform=fig.transFigure);
Matplotlibās default text alignment is such that the ā.ā at the beginning of each string will approximately mark the specified coordinate location.
The transData
coordinates give the usual data coordinates associated with the x- and y-axis labels. The transAxes
coordinates give the location from the bottom-left corner of the axes (here the white box), as a fraction of the total axes size. The transFigure
coordinates are similar, but specify the position from the bottom-left corner of the figure (here the gray box) as a fraction of the total figure size.
Notice now that if we change the axes limits, it is only the transData
coordinates that will be affected, while the others remain stationary (see the following figure):
ax.set_xlim(0, 2)
ax.set_ylim(-6, 6)
fig
This behavior can be seen more clearly by changing the axes limits interactively: if you are executing this code in a notebook, you can make that happen by changing %matplotlib inline
to %matplotlib notebook
and using each plotās menu to interact with the plot.
Arrows and Annotation
Along with tickmarks and text, another useful annotation mark is the simple arrow.
While there is a plt.arrow
function available, I wouldnāt suggest using it: the arrows it creates are SVG objects that will be subject to the varying aspect ratio of your plots, making it tricky to get them right. Instead, Iād suggest using the plt.annotate
function, which creates some text and an arrow and allows the arrows to be very flexibly specified.
Here is a demonstration of annotate
with several of its options (see the following figure):
fig, ax = plt.subplots()
x = np.linspace(0, 20, 1000)
ax.plot(x, np.cos(x))
ax.axis('equal')
ax.annotate('local maximum', xy=(6.28, 1), xytext=(10, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate('local minimum', xy=(5 * np.pi, -1), xytext=(2, -6),
arrowprops=dict(arrowstyle="->",
connectionstyle="angle3,angleA=0,angleB=-90"));
The arrow style is controlled through the arrowprops
dictionary, which has numerous options available. These options are well documented in Matplotlibās online documentation, so rather than repeating them here it is probably more useful to show some examples. Letās demonstrate several of the possible options using the birthrate plot from before (see the following figure):
fig, ax = plt.subplots(figsize=(12, 4))
births_by_date.plot(ax=ax)
# Add labels to the plot
ax.annotate("New Year's Day", xy=('2012-1-1', 4100), xycoords='data',
xytext=(50, -30), textcoords='offset points',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3,rad=-0.2"))
ax.annotate("Independence Day", xy=('2012-7-4', 4250), xycoords='data',
bbox=dict(boxstyle="round", fc="none", ec="gray"),
xytext=(10, -40), textcoords='offset points', ha='center',
arrowprops=dict(arrowstyle="->"))
ax.annotate('Labor Day Weekend', xy=('2012-9-4', 4850), xycoords='data',
ha='center', xytext=(0, -20), textcoords='offset points')
ax.annotate('', xy=('2012-9-1', 4850), xytext=('2012-9-7', 4850),
xycoords='data', textcoords='data',
arrowprops={'arrowstyle': '|-|,widthA=0.2,widthB=0.2', })
ax.annotate('Halloween', xy=('2012-10-31', 4600), xycoords='data',
xytext=(-80, -40), textcoords='offset points',
arrowprops=dict(arrowstyle="fancy",
fc="0.6", ec="none",
connectionstyle="angle3,angleA=0,angleB=-90"))
ax.annotate('Thanksgiving', xy=('2012-11-25', 4500), xycoords='data',
xytext=(-120, -60), textcoords='offset points',
bbox=dict(boxstyle="round4,pad=.5", fc="0.9"),
arrowprops=dict(arrowstyle="->",
connectionstyle="angle,angleA=0,angleB=80,rad=20"))
ax.annotate('Christmas', xy=('2012-12-25', 3850), xycoords='data',
xytext=(-30, 0), textcoords='offset points',
size=13, ha='right', va="center",
bbox=dict(boxstyle="round", alpha=0.1),
arrowprops=dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1));
# Label the axes
ax.set(title='USA births by day of year (1969-1988)',
ylabel='average daily births')
# Format the x-axis with centered month labels
ax.xaxis.set_major_locator(mpl.dates.MonthLocator())
ax.xaxis.set_minor_locator(mpl.dates.MonthLocator(bymonthday=15))
ax.xaxis.set_major_formatter(plt.NullFormatter())
ax.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%h'));
ax.set_ylim(3600, 5400);
The variety of options make annotate
powerful and flexible: you can create nearly any arrow style you wish. Unfortunately, it also means that these sorts of features often must be manually tweaked, a process that can be very time-consuming when producing publication-quality graphics! Finally, Iāll note that the preceding mix of styles is by no means best practice for presenting data, but rather is included as a demonstration of some of the available options.
More discussion and examples of available arrow and annotation styles can be found in the Matplotlib Annotations tutorial.
Stylesheets
While many of the topics covered in previous chapters involve adjusting the style of plot elements one by one, Matplotlib also offers mechanisms to adjust the overall style of a chart all at once. In this chapter weāll walk through some of Matplotlibās runtime configuration (rc) options, and take a look at the stylesheets feature, which contains some nice sets of default configurations.
Plot Customization by Hand
Throughout this part of the book, youāve seen how it is possible to tweak individual plot settings to end up with something that looks a little nicer than the default. Itās also possible to do these customizations for each individual plot. For example, here is a fairly drab default histogram, shown in the following figure:
import matplotlib.pyplot as plt
plt.style.use('classic')
import numpy as np
%matplotlib inline
x = np.random.randn(1000)
plt.hist(x);
We can adjust this by hand to make it a much more visually pleasing plot, as you can see in the following figure:
# use a gray background
fig = plt.figure(facecolor='white')
ax = plt.axes(facecolor='#E6E6E6')
ax.set_axisbelow(True)
# draw solid white gridlines
plt.grid(color='w', linestyle='solid')
# hide axis spines
for spine in ax.spines.values():
spine.set_visible(False)
# hide top and right ticks
ax.xaxis.tick_bottom()
ax.yaxis.tick_left()
# lighten ticks and labels
ax.tick_params(colors='gray', direction='out')
for tick in ax.get_xticklabels():
tick.set_color('gray')
for tick in ax.get_yticklabels():
tick.set_color('gray')
# control face and edge color of histogram
ax.hist(x, edgecolor='#E6E6E6', color='#EE6666');
This looks better, and you may recognize the look as inspired by that of the R languageās ggplot
visualization package. But this took a whole lot of effort! We definitely do not want to have to do all that tweaking each time we create a plot. Fortunately, there is a way to adjust these defaults once in a way that will work for all plots.
Changing the Defaults: rcParams
Each time Matplotlib loads, it defines a runtime configuration containing the default styles for every plot element you create. This configuration can be adjusted at any time using the plt.rc
convenience routine. Letās see how we can modify the rc parameters so that our default plot will look similar to what we did before.
We can use the plt.rc
function to change some of these settings:
from matplotlib import cycler
colors = cycler('color',
['#EE6666', '#3388BB', '#9988DD',
'#EECC55', '#88BB44', '#FFBBBB'])
plt.rc('figure', facecolor='white')
plt.rc('axes', facecolor='#E6E6E6', edgecolor='none',
axisbelow=True, grid=True, prop_cycle=colors)
plt.rc('grid', color='w', linestyle='solid')
plt.rc('xtick', direction='out', color='gray')
plt.rc('ytick', direction='out', color='gray')
plt.rc('patch', edgecolor='#E6E6E6')
plt.rc('lines', linewidth=2)
With these settings defined, we can now create a plot and see our settings in action (see the following figure):
plt.hist(x);
Letās see what simple line plots look like with these rc parameters (see the following figure):
for i in range(4):
plt.plot(np.random.rand(10))
For charts viewed onscreen rather than printed, I find this much more aesthetically pleasing than the default styling. If you disagree with my aesthetic sense, the good news is that you can adjust the rc parameters to suit your own tastes! Optionally, these settings can be saved in a .matplotlibrc file, which you can read about in the Matplotlib documentation.
Stylesheets
A newer mechanism for adjusting overall chart styles is via Matplotlibās style
module, which includes a number of default stylesheets, as well as the ability to create and package your own styles. These stylesheets are formatted similarly to the .matplotlibrc files mentioned earlier, but must be named with a .mplstyle extension.
Even if you donāt go as far as creating your own style, you may find what youāre looking for in the built-in stylesheets. plt.style.available
contains a list of the available styles:
print(plt.style.available)
The standard way to switch to a stylesheet is to call style.use
:
plt.style.use('stylename')
But keep in mind that this will change the style for the rest of the Python session! Alternatively, you can use the style context manager, which sets a style temporarily:
with plt.style.context('stylename'):
make_a_plot()
To demonstrate these styles, letās create a function that will make two basic types of plot:
def hist_and_lines():
np.random.seed(0)
fig, ax = plt.subplots(1, 2, figsize=(11, 4))
ax[0].hist(np.random.randn(1000))
for i in range(3):
ax[1].plot(np.random.rand(10))
ax[1].legend(['a', 'b', 'c'], loc='lower left')
Weāll use this to explore how these plots look using the various built-in styles.
Default Style
Matplotlibās default
style was updated in the version 2.0 release; letās look at this first (see the following figure):
with plt.style.context('default'):
hist_and_lines()
FiveThiryEight Style
The fivethirtyeight
style mimics the graphics found on the popular FiveThirtyEight website. As you can see in the following figure, it is typified by bold colors, thick lines, and transparent axes:
with plt.style.context('fivethirtyeight'):
hist_and_lines()
ggplot Style
The ggplot
package in the R language is a popular visualization tool among data scientists. Matplotlibās ggplot
style mimics the default styles from that package (see the following figure):
with plt.style.context('ggplot'):
hist_and_lines()
Bayesian Methods for Hackers Style
There is a neat short online book called Probabilistic Programming and Bayesian Methods for Hackers by Cameron Davidson-Pilon that features figures created with Matplotlib, and uses a nice set of rc parameters to create a consistent and visually appealing style throughout the book. This style is reproduced in the bmh
stylesheet (see the following figure):
with plt.style.context('bmh'):
hist_and_lines()
Dark Background Style
For figures used within presentations, it is often useful to have a dark rather than light background. The dark_background
style provides this (see the following figure):
with plt.style.context('dark_background'):
hist_and_lines()
Grayscale Style
Sometimes you might find yourself preparing figures for a print publication that does not accept color figures. For this, the grayscale
style (see the following figure) can be useful:
with plt.style.context('grayscale'):
hist_and_lines()
Take some time to explore the built-in options and find one that appeals to you!
Acknowledgement
Content on this page is adapted from Python Data Science Handbook - Jake VanderPlas.