The Axes
label describes what the data in the figure represents and is quite important for the viewer's understanding of the figure itself. By providing labels to the axes background, we help the viewer comprehend the information in an appropriate way.
Before we dive into the code, it is important to understand how matplotlib organizes our figures.
At the top level, there is a Figure
instance containing all that we see and some more (that we don't see). The figure contains, among other things, instances of the Axes
class as a Figure.axes
field. The Axes
instances contain almost everything we care about: all the lines, points, ticks, and labels. So, when we call plot()
, we are adding a line (matplotlib.lines.Line2D
) to the Axes.lines
list. If we plot a histogram (hist()
), we are adding rectangles to the list of Axes.patches
("patches" is the term inherited from MATLAB®, and it represents the "patch of color" concept).
An instance of Axes
also holds references to the XAxis
and YAxis
instances, which in turn refer to the x axis and y axis, respectively. The XAxis
and YAxis
instances manage the drawing of the axis, labels, ticks, tick labels, locators, and formatters. We can reference these through Axes.xaxis
and Axes.yaxis
, respectively. We don't have to go all the way down to XAxis
or YAxis
instances to get to the labels as matplotlib gives us a helper method (practically a shortcut) that enables iterations via these labels: matplotlib.pyplot.xlabel()
and matplotlib.pyplot.ylabel()
.
We will now create a new figure, in which we will:
title
and axes
labels.import matplotlib.pyplot as plt from matplotlib import patheffects import numpy as np data = np.random.randn(70) fontsize = 18 plt.plot(data) title = "This is figure title" x_label = "This is x axis label" y_label = "This is y axis label" title_text_obj = plt.title(title, fontsize=fontsize, verticalalignment='bottom') title_text_obj.set_path_effects([patheffects.withSimplePatchShadow()]) # offset_xy -- set the 'angle' of the shadow # shadow_rgbFace -- set the color of the shadow # patch_alpha -- setup the transparency of the shadow offset_xy = (1, -1) rgbRed = (1.0,0.0,0.0) alpha = 0.8 # customize shadow properties pe = patheffects.withSimplePatchShadow(offset_xy = offset_xy, shadow_rgbFace = rgbRed, patch_alpha = alpha) # apply them to the xaxis and yaxis labels xlabel_obj = plt.xlabel(x_label, fontsize=fontsize, alpha=0.5) xlabel_obj.set_path_effects([pe]) ylabel_obj = plt.ylabel(y_label, fontsize=fontsize, alpha=0.5) ylabel_obj.set_path_effects([pe]) plt.show()
We already know all the familiar imports, parts that generate data, and basic plotting techniques, so we will skip those. If you are not able to decipher the first few lines of the example, please refer to Chapter 2, Knowing Your Data, and Chapter 3, Drawing Your First Plots and Customizing Them, where these concepts are already explained.
After we have plotted the dataset, we are ready to add titles and labels, and to customize their appearance.
First, we add the title. Then, we define the font size and vertical alignment of the title text to be bottom aligned. The default shadow effect is added to the title if we are using matplotlib.patheffects.withSimplePatchShadow()
with no parameters. The default values for the parameters are: offset_xy=(2,-2)
, shadow_rgbFace=None
, and patch_alpha=0.7
. The other values are center
, top
, and baseline
, but we choose bottom
as the text will have some shadowing. In the next line, we add the shadow effect. The path effects are part of the matplotlib module matplotlib.patheffects
that supports matplotlib.text.Text
and matplotlib.patches.Patch
.
We now want to add different settings of the shadow to both the x and y axes. First, we customize the position (offset) of the shadow to the parent object, and then we set the color of the shadow. The color is here represented in triples (3-tuple) of float values between 0.0 and 1.0, for each of the RGB channels. For example, our red color is represented as (1.0, 0.0, 0.0
) (all red, no green and no blue).
The transparency (or alpha) is set up as a normalized value, and we also want to set this up here to be different from the default.
With all the settings present, we instantiate matplotlib.patheffects.withSimplePatchShadow
and hold the reference to it in the variable pe
to reuse it few lines later.
To be able to apply the shadow effect, we need to get to the label
object. This is simple enough because matplotlib.pyplot.xlabel()
returns a reference to the object (matplotlib.text.Text
) that we then use to call set_path_effects([pe])
.
We finally show the plot and can feel proud of our work.
If you are not satisfied with the effects that matplotlib.patheffects
currently offers, you can inherit the matplotlib.patheffects._Base
class and override the draw_path
method. Take a look at the code and comments on how to do this here:
https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/patheffects.py#L47