A Guide to Figure Annotation in MatPlotLib: Topics in MatPlotLib

Introduction to Figure Annotation in MatPlotLib

The MatPlotLib series has explored a wide variety of topics in the MatPlotLib library. In one regard, we’ve spent a great deal of time elaborating on plot types. In particular, we have discussed in great depths the plotting of simple line plots, scatter plots, plotting density and contour plots, as well as a variety of histogram types. Furthermore, we have devoted significant attention towards customization features with plots. In this capacity, we focused explored on customizing plot legends and customizing plot colorbars in MatPlotLib. In this article, we proceed with our thread of customization to discuss figure annotation in MatPlotLib. Particularly, we present the annotation of figures with text and symbols.

Figure Annotation in MatPlotLib

As was patently the case when exploring customization of legends and colorbars for figures, the impetus was to facilitate interpretation and organization of information. This too is the motivation for annotating plots with text and symbolic labels; specifically the precise specification of data elements in a plot. Let’s attempt to apply some figure annotations with our own code.

Suppose we are attempting to mathematically model the random walking of a molecule in space. We don’t actually have this data available, but being a random phenomenon, we can represent this using a random sample. Suppose we take a random sample of ten samples and pass each one into a function, and plot these values. The code for doing so appears as follows:

Now, executing this code reveals a plot that looks like this:

This plot faithfully represents the random walks of molecules prompted by thermal agitation. However, we can clean this graph up a bit by adding some axis labels and a chart title:

That’s better. Take note of the fact that the graph has changed because we are taking a random sample each time we run the code. One issue is that we do not know which terminus is the starting position and which position is the end. To make this determination, we can include text annotations specific for x[0] and x[9], labeling these positions as start and end positions, respectively.

Encoding textual annotations into our figure revolves around utilizing the ax.text function. This makes sense, as these figure annotations are actually an instance of the axis. This also makes intuitive sense because when creating these labels, we specify their position relative to the ‘x’ and ‘y’ axis. The code for procuring these text annotations appear as follows:

Executing this code produces the following plot with its textual annotations:

Performing this code efficaciously produced a plot with the start and end positions clearly labeled.

Arrow Labels

In the previous plot, while we did include text annotations, these aren’t necessarily all that close to the actual terminus on the graph. However, we could be more explicit about our annotations by including arrows. In fact, by using arrows, we could make it so our annotations wouldn’t even need to be extremely close to the position. For encoding arrows, we can use the plt.annotate function to encode both the text and the arrow. Let’s take a look at the code:

Now, executing this code produces a graph that follows the form:

Customizing Annotations

The previous sections efficaciously provided the basic functions that govern chart annotations with text and arrows. However, these functions have many attributes associated with them which may be controlled by the user. For exploring these attributes, lets begin by plotting a new function utilizing the following code:

The function ‘I’ represents a multivariate function of the form z=f(x,y). This code produces the following plot:

Using this plot, we can explore some of the unique properties of plot annotations. As we did with our graph of the random walk, let’s first add some chart labels and a title.

Adding Annotations

That’s a bit better. Now, lets add some annotations listing global and local minima and maxima. We can do this as we did in the previous graph, using the plt.text function and specifying the label. The code for doing so looks like:

By annotating the plot in this fashion, we achieve a graph that appears as follows:

Despite these annotations, we might notice that the graph has a great deal of unused white space that might be utilized to free up some of the clutter. We can move out the local minima and maxima labels, and include arrows to improve the appearance of this graph. This may be performed using the following code:

This code readily procures the following plot:

Customizing Arrows

Now, from a personal point of view, I am not a huge fan of the thick arrows. Especially due to the thinness of the actual function line, I think that the thick arrows detract from the plot. Fortunately, MatPlotLib offers the ‘arrowstyle’ attribute which allows us to change the appearance of the arrow. This attribute takes a symbolic representation to denote the style of the arrow. To procure a more narrow looking arrow, we can specify the ‘arrowstyle’ attribute as ‘->’. Let us first observe what this looks like with our code:

Here, we have specified the style of the arrow we desire. Now, let’s take a look at how this altered the graph we encoded:

Notice that both of the arrows now appear much thinner, and as a result, the graph looks a great deal cleaner.

Specifying Arrow Structure

One other attribute of arrows is worthy of note. With MatPlotLib, we can alter the direction of the arrow in space. This may be done using the connection style attribute. Utilizing this function allows us to stipulate a unique design to our plot. Let’s attempt to include this with our local maxima label. Here is the code for executing this function:

Running the code above produces the following plot:

Note the curve in arrow for the local maxima. The inclusion of this structure makes our plot look a bit more sophisticated.

The Take Away

Plot annotations are one of the many features we may include into our plots to improve the design of our data model. Making this inclusion promotes enhanced interpretation of the data by attracting the viewer to take note of certain positions of particular importance. Furthermore, figure annotation in MatPlotLib can take your figures to the next level in terms of their professionalism. Overall, they are much like icing to the cake of exceptional data modeling. If you seek to explore this subject in greater depth, consider checking out the MatPlotLib online manual, which may be found here. Nevertheless, our next article proceeds on the subject of plot customization with customizing plot tick marks.

Leave a Reply

%d bloggers like this: