Plot Types and Customizations

Let’s learn about setting the color of lines, the width between lines, and the marker styles of lines with matplotlib.

Line colors

There are lots of options available in matplotlib to customize a plot. These methods will keep coming throughout this course. We’lls explore a few of these in this lesson.

The color and other graphical elements in a plot can be defined in a number of ways. The matplotlib library uses MATLAB-like syntax, where 'r' means red, 'g' means green, and so on. The MATLAB API for selecting line styles is also supported in matplotlib. For example, 'r.-' means a red line with dots.

Press + to interact
fig, axes = plt.subplots() # using subplots() here
axes.plot(x, x+1, 'r.-') # red line with dots
axes.plot(x, x+2, 'g--') # green dashed line

The appropriate way to plot graphical elements is to plug in the color value in the following template: color=[parameter].

Colors can be used both by their names and their RGB hex codes. There is another very useful optional parameter, alpha, that can be used along with color to control opacity (this is useful when data points are stacked on each other).

Press + to interact
fig, axes = plt.subplots()
axes.plot(x, x+1, color="blue", alpha=0.5) # alpha = 0.5, half-transparent
axes.plot(x, x+2, color="#8B008B") # RGB hex color code
axes.plot(x, x+3, color="#FF8C00") # RGB hex color code

Line marker, width, and style

Below is another example of plot customization using a range of related parameters to make our plot beautiful.

Press + to interact
fig, axes = plt.subplots(figsize=(10,8))
axes.plot(x, y, color="purple", lw=3, ls='-',
marker='s', markersize=8,
markerfacecolor="yellow",
markeredgewidth=3, markeredgecolor="green")

Let’s move on and explore a little more about how to make our plot figures more attractive. We’ll go over how to:

  • Change the line width with the linewidth or lw keyword argument.
  • Change the line style with linestyle or ls keyword arguments.
  • Set the marker with marker and markersize keyword arguments.

The code and figure below are a good reference for creating attractive plots.

Press + to interact
# using subplots for fig and axes
fig, axes = plt.subplots(figsize=(10,8))
# adding plots of different colors and line widths on axes
axes.plot(x, x+1, color="red", linewidth=0.25)
axes.plot(x, x+2, color="red", linewidth=0.50)
axes.plot(x, x+3, color="red", linewidth=1.00)
axes.plot(x, x+4, color="red", linewidth=2.00)
# possible linestyle options ‘-‘, ‘–’, ‘-.’, ‘:’, ‘steps’
axes.plot(x, x+5, color="green", lw=5, linestyle='-')
axes.plot(x, x+6, color="green", lw=5, ls='-.')
axes.plot(x, x+7, color="green", lw=5, ls=':')
# custom dash
line, = axes.plot(x, x+8, color="black", lw=1.50)
line.set_dashes([5, 10, 15, 10]) # format: line length, space length, ...
# possible marker symbols: marker = '+', 'o', '*', 's', ',', '.', '1', '2', '3', '4', ...
axes.plot(x, x+ 9, color="blue", lw=3, ls='-', marker='+')
axes.plot(x, x+10, color="blue", lw=3, ls='--', marker='o')
axes.plot(x, x+11, color="blue", lw=3, ls='-', marker='s')
axes.plot(x, x+12, color="blue", lw=3, ls='--', marker='1')
# marker size and color
axes.plot(x, x+13, color="purple", lw=3, ls='-', marker='o', markersize=2)
axes.plot(x, x+14, color="purple", lw=3, ls='-', marker='o', markersize=4)
axes.plot(x, x+15, color="purple", lw=3, ls='-', marker='o', markersize=8, markerfacecolor="red")
axes.plot(x, x+16, color="purple", lw=3, ls='-', marker='s', markersize=8,
markerfacecolor="yellow", markeredgewidth=3, markeredgecolor="green");

Matplotlib also allows control over the axes. We can set the x and y limits using set_xlim and set_ylim methods. We can also use axis('tight') to automatically get “tightly fitted” axes ranges.

Let’s see examples of this:

Press + to interact
# subplots again with nrows, ncols and figsize
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 3))
# Default axes range on left plot
axes[0].plot(x, y, y, x)
axes[0].set_title("default axes ranges")
# Tight axes on the middle plot
axes[1].plot(x, y, y, x)
axes[1].axis('tight')
axes[1].set_title("tight axes")
# Custom axes range on the right plot
axes[2].plot(x, y, y, x)
axes[2].set_ylim([0, 50])
axes[2].set_xlim([1, 4])
axes[2].set_title("custom axes range");

Scatter plot

We create a variety of plots while practicing data science. Some of the most commonly used plots are histograms, scatter plots, bar plots, pie charts, and so on. It’s very easy to create all of these plots using this state-of-the-art Python library.

Let’s start with the scatter plot, which we can create with plt.scatter(x,y):

Press + to interact
# Scatter plot
plt.scatter(x,y)

Now, let’s look at some more examples to familiarize ourselves with the other types of plots. With time and practice, you’ll be an expert in all of these plots!

Histograms

We can create histograms using matplotlib. Let’s explore this by taking some random data and using the plt.hist() method to create a histogram.

Press + to interact
# Histogram
from random import sample
data = sample(range(1, 1000), 100)
plt.hist(data)

Boxplot

We can also create boxplots using matplotlib.

Let’s learn this with an example:

Press + to interact
# boxplot - Don't worry, we'will talk about box plot and its usage in details in the coming sections!
# creating data for the plot, recall list comprehension!
data = [np.random.normal(0, std, 100) for std in range(1, 4)]
# rectangular box plot
plt.boxplot(data, vert=True, patch_artist=True);