Graphs style guide¶
This Jupyter notebook gives a series of example set-ups for graph plotting using matplotlib and numpy.
First, we import relevant packages and set up some default plotting parameters using the rcParams in matplotlib.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
# This section sets some default plotting parameters.
mpl.rcParams['figure.autolayout'] = True
mpl.rcParams['lines.linewidth'] = 3
mpl.rcParams['lines.markersize'] = 20
mpl.rcParams['axes.linewidth'] = 2
mpl.rcParams['axes.titlesize'] = 20
mpl.rcParams['figure.figsize'] = 8,6
mpl.rcParams['lines.markersize'] = 4
mpl.rcParams['ytick.major.size'] = 6
mpl.rcParams['ytick.labelsize'] = 18
mpl.rcParams['ytick.major.width'] = 2
mpl.rcParams['ytick.minor.width'] = 2
mpl.rcParams['xtick.major.size'] = 6
mpl.rcParams['xtick.labelsize'] = 16
mpl.rcParams['xtick.major.width'] = 2
mpl.rcParams['xtick.minor.width'] = 2
mpl.rcParams['font.size'] = 20
Colour definitions¶
It's useful to employ a consistent colour scheme throughout papers and reports where possible. In the following cells, we will define a series of colors in arrays:
pretty_colors[5]is an array of five vibrant colours that are useful for plotting. These can also be accessed individually asc1,c2, and so on.warwickDC_colors[11]is an array of 11 colours taken from Warwick District Council's Warwick Colour Palette projectLeam_colors[19]is an array of 19 colours taken from the Leamington Space Colour Palette project
from matplotlib.patches import Circle
# Let's define a set of nice colors.
# These are nice modern colors that are generally useful.
pretty_colors = []
pretty_colors.append('#FE5F55')
pretty_colors.append('#2DD881')
pretty_colors.append('#d3a305')
pretty_colors.append('#01A7C2')
pretty_colors.append('#7346c1')
n = len(pretty_colors)
# Create a figure and axes
fig, ax = plt.subplots()
# Define the center and radius
for ic in range(n):
x = float(ic)
y = 0.0
center = (x, y)
radius = 0.4
# Create a Circle patch object
circle = Circle(center, radius, edgecolor=pretty_colors[ic], facecolor=pretty_colors[ic], linewidth=2, alpha=1.0)
s = "c"+str(ic)
ax.text(x-0.2,y+0.5,s)
# Add the circle to the axes
ax.add_patch(circle)
# Set the limits of the axes:
ax.set_xlim(-1, float(n))
ax.set_ylim(-0.5, 1)
ax.set_aspect('equal', adjustable='box')
ax.set_axis_off()
plt.show()
from matplotlib.patches import Circle
# Let's define a set of nice colors drawn from the Warwick palette here: https://www.warwickdc.gov.uk/info/20356/arts_development/1796/colour_palette_projects/3
warwickDC_colors = ['#931B25','#E88B3C','#619154','#B0C0C9','#E5A411','#899519','#148184','#57A686','#68ACBF','#0870B9','#01437A']
n = len(warwickDC_colors)
# Create a figure and axes
fig, ax = plt.subplots()
# Define the center and radius
for ic in range(n):
x = float(ic)
y = 0.0
center = (x, y)
radius = 0.3
# Create a Circle patch object
circle = Circle(center, radius, edgecolor=warwickDC_colors[ic], facecolor=warwickDC_colors[ic], linewidth=2, alpha=1.0)
s = "wc"+str(ic)
ax.text(x-0.3,y+0.5,s,fontsize=14)
# Add the circle to the axes
ax.add_patch(circle)
# Set the limits of the axes:
ax.set_xlim(-1, float(n))
ax.set_ylim(-0.5, 1)
ax.set_aspect('equal', adjustable='box')
ax.set_axis_off()
plt.show()
# LEAMINGTON SPA COLOUR PALETTE.
from matplotlib.patches import Circle
# Let's define a set of nice colors drawn from the Leamington Spa colour palette here:
# https://www.warwickdc.gov.uk/info/20356/arts_development/1796/colour_palette_projects/2
Leam_colors = ['#C24A3E','#ECD372','#6579AF','#D93832','#1B4544','#235379','#376535','#8AB76A','#CEBA62','#66883C','#93AC64',
'#ABCDC5','#B1322E','#CA7F3D','#F1C845','#6C7682','#3A5776','#CEB652','#5F538F']
n = len(Leam_colors)
# Create a figure and axes
fig, ax = plt.subplots(figsize=(12,3))
# Define the center and radius
for ic in range(n):
x = float(ic)
y = 0.0
center = (x, y)
radius = 0.3
# Create a Circle patch object
circle = Circle(center, radius, edgecolor=Leam_colors[ic], facecolor=Leam_colors[ic], linewidth=2, alpha=1.0)
s = "Lc"+str(ic)
ax.text(x-0.3,y+0.5,s,fontsize=12)
# Add the circle to the axes
ax.add_patch(circle)
# Set the limits of the axes:
ax.set_xlim(-1, float(n))
ax.set_ylim(-0.5, 1)
ax.set_aspect('equal', adjustable='box')
ax.set_axis_off()
plt.show()
Assorted graph examples¶
The following show some examples of different representative graphs; these should serve as useful templates for producing graphs of your own.
Some general guidelines and good practice are as follows:
Always label axes with sensible labels. For example, using p / Bar is OK, but Pressure p / Bar is more informative in defining what the variable p actually is.
Always add appropriate labels to axes, as appropriate. Add labels using either "/" (as in Length / m) or using square brackets (as in Length [m]).
Be careful to use sensible axis proportions. In the examples given below, the ratios between the axis lengths are typically either equal (to be used in the scales on each axis are also equal) or close to something like the golden ratio (~1.62).
Data set-up¶
In the following cells, we're just going to create some artificial data that we can plot.
x1 = np.linspace(0,10,100)
y1 = x1**2
y2 = 0.1 * x1**3
Example 1: Simple function plots.¶
Continuous data can be plotted as continuous lines, as in the following example.
Legend guidelines¶
Strongly preferred to remove the frame from the legend.
Make sure that the legend does not overlap with any of the lines or data-points.
The legend location can be modified by changing the loc parameter, for example plt.legend(frameon=False,loc=4).
plt.plot(x1,y1,color = Leam_colors[3],label = "Experiment 1")
plt.plot(x1,y2,color = warwickDC_colors[9],label = "Experiment 2")
plt.ylabel(r"Speed / m s$^{-1}$")
plt.xlabel("Time / s")
plt.legend(frameon=False)
<matplotlib.legend.Legend at 0x118bc7750>
Readability¶
Always make sure that graphs and plots are as easy to `read' as possible.
Bear in mind that sometimes documents are printed out in black-and-white - so make sure that your graphs are still understandable if printed this way.
This means using different line-types or adding symbols, as in the following examples.
# Example using a dashed line.
plt.plot(x1,y1,color = Leam_colors[3],label = "Experiment 1")
plt.plot(x1,y2,'--',color = warwickDC_colors[9],label = "Experiment 2")
plt.ylabel(r"Speed / m s$^{-1}$")
plt.xlabel("Time / s")
plt.legend(frameon=False)
<matplotlib.legend.Legend at 0x118c83390>
If you're going to add symbols to a line, follow these rules:
In general, you probably don't need a symbol on every data-point, especially if you have a large number of points. You can use the
markeverycommand in theplt.plot()function to increase the spacing between markers.It is strongly preferred to use `empty' markers by setting
markerfacecolor = 'white'The size of markers should be adjusted so that their shape is clearly visible, for example
markersize = 6Make sure that the marker edge widths are comparable to the line-width, as in
markeredgewidth = 3
plt.plot(x1,y1,color = Leam_colors[3],label = "Experiment 1")
plt.plot(x1,y2,'o-',markevery = 5,markersize = 9, markeredgewidth = 3, markerfacecolor = 'white', color = warwickDC_colors[9],label = "Experiment 2")
plt.ylabel(r"Speed / m s$^{-1}$")
plt.xlabel("Time / s")
plt.legend(frameon=False)
<matplotlib.legend.Legend at 0x119b74050>
Another way to clearly discern between lines on a graph is to use shading / alpha to change the visibility, as in the following example. However, it is still reccomended to use different linestyles.
plt.plot(x1,y1,color = Leam_colors[3],label = "Experiment 1")
plt.plot(x1,y2,'--',color = warwickDC_colors[9],label = "Experiment 2", alpha=0.6)
plt.ylabel(r"Speed / m s$^{-1}$")
plt.xlabel("Time / s")
plt.legend(frameon=False)
<matplotlib.legend.Legend at 0x119d66350>