When 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:
1 2 3 4 5 6 7 8 9 10 11 |
font1 = {'family' : 'sans-serif', 'color' : 'Black', 'weight' : 'normal', 'size' : 12, } font2 = {'family' : 'sans-serif', 'color' : 'Black', 'weight' : 'bold', 'size' : 13, } |
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:
1 2 3 4 5 |
# These ylabels and xlabels are not the "ticks" of the axis plt.xlabel("Data from 1880 to 2015", fontdict=font1) plt.ylabel("Temperature deviation in Degrees Celsius", fontdict=font1) plt.title("World seasonal temperature average deviation from mean 1951-1980. Source: NASA GISTEMP", fontdict=font2) |
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:
1 |
plt.legend(loc=4) |
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
1 2 3 4 5 6 |
#This recovers the locations of the labels and the actual labels locs, labels = plt.xticks() # This rotates the labels plt.setp(labels, rotation=45) |
- Grid:
The grid can be activate with the following statement:
1 |
plt.grid(which="both") |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.parasite_axes import SubplotHost %matplotlib inline font1 = {'family' : 'sans-serif', 'color' : 'Black', 'weight' : 'normal', 'size' : 12, } font2 = {'family' : 'sans-serif', 'color' : 'Black', 'weight' : 'bold', 'size' : 13, } # This is the data source dataset = pd.read_csv('ExcelFormattedGISTEMPDataCSV.csv') fig = plt.figure() plt.figure(figsize=(16,8), dpi=100) x = dataset.Year # Select the data to be displayed and colours plt.plot(dataset.Year, dataset.DJF/100,'b', label="Winter", lw=2) plt.plot(dataset.Year, dataset.MAM/100,'y', label="Spring", lw=2) plt.plot(dataset.Year, dataset.JJA/100,'r', label="Summer", lw=2) plt.plot(dataset.Year, dataset.SON/100,'g', label="Fall", lw=2) plt.xlabel("Data from 1880 to 2015", fontdict=font1) plt.ylabel("Temperature deviation in Degrees Celsius", fontdict=font1) plt.title("World seasonal temperature average deviation from mean 1951-1980. Source: NASA GISTEMP", fontdict=font2) plt.grid(which="both") locs, labels = plt.xticks() plt.setp(labels, rotation=45) # Place the legend in a non disturbing place plt.legend(loc=4) plt.show() |
And here is the result (slightly reduced in size):