Search⌘ K
AI Features

Plot Types and Customizations

Explore how to customize various plot elements in Matplotlib such as line color, width, style, markers, and axis limits. Learn to create multiple plot types like scatter, histograms, and box plots to enhance your data visualization skills in Python.

Line colors

Matplotlib provides several options to customize plots. These methods are introduced throughout the course. This lesson covers a subset of these methods.

The color and other graphical elements in a plot can be defined in several 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.

Python 3.5
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 specified by name or by RGB hex code. 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).

Python 3.5
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.

Python 3.5
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")

Next, extend plot customization to improve figure clarity and presentation. This section covers 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.

Python 3.5
# 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:

Python 3.5
# 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. This library supports creating a wide range of plots with concise code.

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

Python 3.5
# Scatter plot
plt.scatter(x,y)

The following examples demonstrate additional plot types. These examples build familiarity with different plot types.

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.

Python 3.5
# Histogram
from random import sample
data = sample(range(1, 1000), 100)
plt.hist(data)

Box plot

We can also create boxplots using Matplotlib.

Let’s learn this with an example:

Python 3.5
# 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);