Customizing in matplotlib#

matplotlib includes an overwhelming number of options for customizing plots.

This section provides a starting example and a cookbook with other syntax.

Title and Axis Labels#

We can start by adding a plot title, and axis labels to our square root line plot. As we’ll see later, this basic title and axis label syntax applies across different types of matplotlib plots.

%matplotlib inline
import matplotlib.pyplot as plt # import statement

squares = [1,4,9,16,25] # dataset for y axis
inputs = [1,2,3,4,5] # dataset for x axis

fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares) # generate plot

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

plt.show() # show output

Cookbook#

Font Size & Line Thickness#

fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares, linewidth=3) # generate plot, set line width

ax.set_title("Square Numbers", fontsize=24) # set plot title and size
ax.set_xlabel("Value", fontsize=14) # set x axis label and size
ax.set_ylabel("Square of Value", fontsize=14) # set y axis label and size

plt.show() # show output

Tick Marks & Labels#

fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares) # generate plot

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

ax.set_xticks([0, 1, 2, 3, 4, 5]) # set x axis tick locations
ax.set_xticklabels(['zero', 'one', 'two', 'three', 'four', 'five']) # set x axis tick labels
ax.set_yticks([0, 5, 10, 15, 20, 25, 30]) # set y axis tick locations
ax.set_yticklabels(['zero', 'five', 'ten', 'fifteen', 'twenty', 'twenty five', 'thirty']) # set y axis tick labels

plt.show() # show output

We could further modify that example to rotate the labels and adjust their size.

fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares) # generate plot

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

ax.set_xticks([0, 1, 2, 3, 4, 5]) # set x axis tick locations
ax.set_xticklabels(['zero', 'one', 'two', 'three', 'four', 'five'], rotation=30, fontsize='small') # set x axis tick labels
ax.set_yticks([0, 5, 10, 15, 20, 25, 30]) # set y axis tick locations
ax.set_yticklabels(['zero', 'five', 'ten', 'fifteen', 'twenty', 'twenty five', 'thirty'], rotation=30, fontsize='small') # set y axis tick labels

plt.show() # show output

Colors, Markers & Line Styles#

# custom line color
fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares, color='green') # generate plot with custom line color

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

plt.show() # show output
# custom line style
fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares, linestyle='--') # generate plot with custom line style

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

plt.show() # show output
# customizing color and style
fig, ax = plt.subplots() # figure for new plot
ax.plot(inputs, squares, 'g--') # generate plot with custom line color and style

ax.set_title("Square Numbers") # set plot title
ax.set_xlabel("Value") # set x axis label
ax.set_ylabel("Square of Value") # set y axis label

plt.show() # show output

Additional Resources#

For more on customizing line styles:

For more on colors in matplotlib: