Skip to content

Customizing Plots

Customizing Plots

Matplotlib plots can be customized in many ways. Let's build upon the example in the previous section and add a couple of features to the line plot. These additional plot features include:

  • Axis Labels, Title, and Legend
  • Line Color, Line Width, Line Style, Line Opacity and Marker Options

Axis Labels, Title, and Legend

You can add axis labels, a title, and a legend to line plots constructed with Matplotlib. Each of these features is specified with a different Matplotlib command.

Plot feature Matplotlib command Example
x-axis label plt.xlabel() plt.xlabel('time in seconds')
x-axis limits plt.xlim() plt.xlim([0, 10])
y-axis label plt.ylabel() plt.ylabel('distance in meters')
y-axis limits plt.ylim() plt.ylim([-5, 5])
title plt.title() plt.title('distance vs. time')
legend plt.legend() plt.legend(['run 1'])

Note the words passed into the plt.xlabel(), plt.ylabel() and plt.title() need to be enclosed in quotation marks ' '. In Python, quotation marks denote strings. A string is a Python data type which contains letter, numbers, punctuation, symbols, and spaces. The plt.legend() function expects a list of strings.

A list of strings is similar to a list of numbers. The list is defined using square brackets [ ]. A list of strings needs to have each string enclosed in quotes ' ' and separated by a comma , as in ['string1','string2',string3']. If there is only one line on a plot, only one string needs to be in the list strings passed to plt.legend(). Even if there is only one string in a list, the square brackets used to define the list still need to be included (otherwise a simple string is defined, not a list of strings).

In [1]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = [0, 2, 4, 6] y = [1, 3, 4, 8]

plt.plot(x,y) plt.xlabel('x values') plt.xlim([-1,7]) plt.ylabel('y values') plt.ylim([0,10]) plt.title('plotted x and y values') plt.legend(['line 1'])

plt.show()

Line Color, Line Width, Line Style, Line Opacity and Marker Options

Line color, line width, line style, and markers options can be customized in a Matplotlib plot. Line color, line width, and line style are included as extra keyword arguments in the plt.plot() function call. A keyword argument is a special type of argument passed into a function that must be stated explicitly by name. Regular arguments passed to functions are called positional arguments. Positional arguments need to be passed to the function in order, but positional argument names do not need to be specified. In the code line below x and y are positional arguments and color='r' is a keyword argument.

plt.plot(x, y, color='r')

Examples of some of the keyword arguments that can be specified in a plt.plot() function call are in the table below:

Plot feature plt.plot() keyword argument Example
width of line linewidth plt.plot(x, y, linewidth = 4)
solid, dashed, dotted line linestyle plt.plot(x, y, linestyle = '-')
line color color plt.plot(x, y, color = 'red')
line opacity (transparency) alpha plt.plot(x, y, alpha = 0.5)
marker shape marker plt.plot(x, y, marker = 'o')

The general format of a plt.plot() function call with lots of options specified is:

plt.plot(<x-data>,<y-data>,
         linewideth=<float or int>,
         linestyle='<linestyle abbreviation>',
         color='<color abbreviation>', 
         marker='<marker abbreviation>')

An example plt.plot() line including line width, style, color, opacity and marker type is:

plt.plot(x, y, 
         linewidth=2.0,
         linestyle='+',
         color='b',
         alpha=0.5,
         marker='o')

line width

Below is a list of line widths (many other widths are also available)

linewidth=<float or int> Line Width
0.5 0.5 pixels wide
1 1 pixel wide
1.5 1.5 pixels wide
2 2 pixels wide
3 3 pixels wide

line style

Below is a list of line styles

linestyle='<style abbreviation>' Line Style
'-' or 'solid' solid line (default)
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dot line
':' or 'dotted' dotted line
'None' or ' ' or '' no line

color

Below is a list of color abbreviations. Note 'b' is the abbreviation for blue and 'k' is the abbreviation for black.

color ='<color abbreviation>' Color Name
'b' Blue
'c' Cyan
'g' Green
'k' Black
'm' magenta
'r' Red
'w' White
'y' Yellow

color format

Colors can also be specified in hexadecimal form surrounded by quotation marks like '#FF69B4' or in RGBA (red,green,blue,opacity) color surrounded by parenthesis like (255,182,193,0.5).

color ='<color abbreviation>' Color Format
'#FF69B4' hexadecimal
(255,182,193,0.5) RGBA

alpha (opacity)

Below is a list of alpha (opacity) values (any alpha value between 0.0 and 1.0 is possible)

alpha = <float or int> Opacity
0 transparent
0.5 Half transparent
1.0 Opaque

marker shape

Below is a list of maker styles

marker='<marker abbreviation>' Marker Style
"." point
"o" circle
"v" triangle_down
"^" triangle_up
"s" square
"*" star
"h" hexagon
"+" plus
"P" filled plus
"x" x
"X" filled x
"D" diamond
"d" thin diamond

In addition to marker='<marker style>', marker edge color, marker face color, and marker size can be specified with:

plt.plot(x,y,
         ...
         markeredgecolor='<color abbreviation>',
         markerfacecolor='<color abbreviation>',
         markersize=<float or int>)

Customized Line Plot Example

Next, we will construct a plot with Matplotlib that utilizes a couple of customization options. We will construct a Matplotlib line plot with the following options:

  • line width of 0.5
  • dashed line
  • red line
  • circle markers
  • marker size of 10
  • marker face color mostly transparent (alpha = 0.1)

The one long plt.plot() function call is below. Note how the keyword arguments are separated by commas and are on different lines. Separate lines are not necessary but do improve the readability of the code.

In [2]:
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
%matplotlib inline

x = [0, 2, 4, 6] y = [1, 3, 4, 8]

plt.plot(x, y, linewidth=0.5, linestyle='--', color='r', marker='o', markersize=10, markerfacecolor=(1,0,0,0.1))

plt.show()

The plot produced contains a thin 0.5pt width dashed red line. The plot's line markers are 10pt in size, mostly transparent, and red. This plot looks good. However, to include the plot in a report or presentation, the plot must be saved as an image file. The next section details how Matplotlib plots can be saved in a variety of file formats.