Matplotlib

Customising Plots


Customising plots in matplotlib

This page covers a customising plots tutorial.

Customising plots in Matplotlib involves tweaking various aspects of the visual representation of data. This could be labels, titles, legends, colours, and styles of the plot. As a result, it enhances the visual presentation of the data, which in turn may offer better comprehension and insight of the data.

Within this tutorial, we cover labels and titles, legends, colours, and styles (such as lines and markers).

First, let’s create a sample data plot.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Display the plot
plt.show()
matplotlib-multiplot-chart

Labels and titles

Creating labels and titles with Matplotlib is extremely easy. There are 2 labels , one for the x-axis and one for the y-axis. Therefore, we implement them separately. The method for labelling the x-axis is xlabel(). Likewise, ylabel() for the y-axis. The main title on the other hand, uses the function title().

Let’s look at the implementation code.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Labels and title
plt.xlabel("X-Axis Label")
plt.ylabel("Y-Axis Label")
plt.title("Plot Title")

# Display the plot
plt.show()
matplolib-plot-labels-and-title-1

Legend

In charts, legends show concise data representations. They match the data with the chart by displaying the appropriate data labels and colours. Legends prevent data analysis confusion. As such, making them very beneficial to readers. The implementation code is quite straightforward.

Matplotlib offers the legend() method for such tasks. Note that the function stores the data lines inside parentheses [ ] and each line requires a separate name.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y)
plt.plot(a, b)

# Labels and title
plt.xlabel("X-Axis Label")
plt.ylabel("Y-Axis Label")
plt.title("Plot Title")

# legend
plt.legend(["Data 1", "Data 2"])

# Display the plot
plt.show()
matplotlib-legend

Colours

Management of chart colours can have a large impact on the visual representation of data. The right adjustment of colours helps readers analyse data more effectively. Matplotlib provides a thorough system of changing chart colours. From text and lines to background and markers.

The library recognises multiple colour languages: words (English), letters (English), HEX, RGB, and more.

We use color to apply different colours. Matplotlib offers shortcuts for many of its methods. In this instance, we can also use the letter c.

Let’s look at an example. Colour languages:

  • Words: green
  • Letters: g
  • HEX: #008000

For example: the colour green can be coded as:

  • color or c =”green”
  • color or c =”g”
  • color or c =”008000″

We can change the colour of many parts of the plot. Let’s explore some of them. Below, we apply the changes to both plot lines, both axis labels, and the title.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y, c="green")
plt.plot(a, b, color="aqua")

# Labels and title
plt.xlabel("X-Axis Label", c="#f02500")
plt.ylabel("Y-Axis Label", color="magenta")
plt.title("Plot Title", color="y")

# legend
plt.legend(["Data 1", "Data 2"])

# Display the plot
plt.show()
matplotlib-plot-colours

Styles

In this section, styles refer to the change in appearance of some of the elements inside the chart. Again, this can make the data more appealing. We cover line styles and width as well as markers.

Line style and width

Line styles are the way the lines inside the plot look. By default, they are straight lines. We change that through the linestyle method (or ls as a shortcut).

Some popular styles are:

  • “solid” or “-” (default)
  • “dashed” or “–“
  • “dotted” or “:”
  • “none” or ” “

We can use either the name or the symbol of the style. For more styles, check the original documentation of matplotlib here.

Line size refers to the width of the line. In certain scenarios, we may need to adjust the size of lines in order to create a better visual experience. Matplotlib provides the method linewidth (or lw for short).

Let’s see an example with both tools.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y, c="green", linestyle=":", lw=5.6)
plt.plot(a, b, color="aqua", ls="--", linewidth=3.9)

# Labels and title
plt.xlabel("X-Axis Label", c="#f02500")
plt.ylabel("Y-Axis Label", color="magenta")
plt.title("Plot Title", color="y")

# legend
plt.legend(["Data 1", "Data 2"])

# Display the plot
plt.show()
matplotlib-line-styles-and-width

Markers

In matplotlib, markers are the actual data points. For instance, in the above instance there are 4 values for each axis, x and y.

  • x-axis: [1, 4, 7, 9]
  • y-axis: [2, 3, 7, 6]

These are 4 data points, with coordinates [1, 2] [4, 3] [7, 7] and [9, 6]. The default markers are not visibly displayed. So, the lines look are they are just bent.

We can change the colour, style, and size of the marker.

With markers, there are two techniques to change colours. Markeredgecolor (or mec) applies to the outside of the marker. Likewise, markerfacecolor (or mfc) applies to the inside of the marker.

When it comes to marker styles, there are many. The method is marker. The most common ones are:

  • “o” (circle)
  • “*” (star)
  • “.” (point)
  • “+” (plus)
  • “x” (X)
  • “D” (diamond)
  • “s” (square)

For more on marker styles, check here.

And finally, markersize (or ms) sets the width of the marker.

Let’s explore the three techniques with an example.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y, c="green", linestyle=":", lw=5.6, marker="o", ms=11.3, mec="r", mfc="y")
plt.plot(a, b, color="aqua", ls="--", linewidth=3.9,marker="D", markersize=13.1, markeredgecolor="#33FFFF", markerfacecolor="pink")

# Labels and title
plt.xlabel("X-Axis Label", c="#f02500")
plt.ylabel("Y-Axis Label", color="magenta")
plt.title("Plot Title", color="y")

# legend
plt.legend(["Data 1", "Data 2"])

# Display the plot
plt.show()
matplotlib-styles-colour-marker-lines

fmt parameter

The matplotlib fmt parameter is a very concise tool for setting the basics. It’s a shortcut way of applying formats.

It follows this logic: ” marker line color “

Note that color applies to both the marker and the line. So, instead of writing the previous example so long, we can reduce the amount of code.

import matplotlib.pyplot as plt

# Sample data 1
x = [1, 4, 7, 9]
y = [2, 3, 7, 6]

# Sample data 2
a = [3, 2, 6, 7]
b = [1, 3, 5, 9]

# Plot the data
plt.plot(x, y, "o:r")
plt.plot(a, b, "+--g")

# Labels and title
plt.xlabel("X-Axis Label", c="#f02500")
plt.ylabel("Y-Axis Label", color="magenta")
plt.title("Plot Title", color="y")

# legend
plt.legend(["Data 1", "Data 2"])

# Display the plot
plt.show()
matplotlib-fmt-shortcut-string-notation

Once the basics are set, we can now add more changes if needed (such as mec, lw, etc.).


This is an original customising plots tutorial. Created by aicorr.com.

Next: Grid Lines