Python + Matplotlib: Plot accessories

plotAccessories2When producing a plot using Python and matplotlib for publishing purposes, or to submit it as part of a report or for any professional use, some questions come to mind that not usually explained in one easy to retrieve place. True, everything is in the matplotlib documentation or can be easily found on stackoverflow. But the point of this post is to answer a few of these very simple questions that are always overlooked by everybody who explains how the library works. for the purpose of teaching the library, the subjects I am going to deal with are noise, the point is to show the data. In this post, they are the meat. I want a visualization of the data as a derived product of the data. I am not going into the very details, but I am showing the main points, as I have had to search and find them when in need to show some data elements to somebody not familiar with the data.

  • Fonts:

It is always useful to highlight something. Fonts can be declared in python as follows:

The two fonts are very similar, they share the same typeface but font2 is slightly bigger and bold. These can be used in plot labels or titles (or else) as follows:

Here I have used font2 for the plot title.

  • Legend:

The legend can be placed in several pre-defined positions on the plot. These locations are controlled by the “loc” kwarg  as follows:

The possible values for “loc” are:

String Number
upper right 1
upper left 2
lower left 3
lower right 4
right 5
center left 6
center right 7
lower center 8
upper center 9
center 10

In this example I have placed the plot legend out of the way of the data.

  • Tick Orientation:

The axis ticks (which is not the axis legend) can sometimes be too dense to be readable and yet sometimes they are necessary to make some sense. They can be retrieved and then rotated by, for instance 45 degrees

  • Grid:

The grid can be activate with the following statement:

The “which” kwarg controls whether to set the grid on the major, minor ticks or both (in this plot there are no minor ticks).

So, here you can see a full example, where I also use pandas.

And here is the result (slightly reduced in size):

plotAccessories3

Leave a Reply