How to Remove Frames from Figures in Matplotlib


By default, plots in Matplotlib have frames around them on all sides.

However, sometimes you may want to remove the frame from a plot for a variety of reasons.

The easiest way to do so is by turning off the frames in the Matplotlib settings with the axes.spines arguments.

You can use the following syntax to remove the entire frame from a Matplotlib plot:

import matplotlib.pyplot as plt

plt.rcParams['axes.spines.left'] = False
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.bottom'] = False

This particular example will remove the frame around a plot generated using Matplotlib.

By explicitly setting each axes.spines location to False, we are able to remove the left, right, top and bottom frames around the plot.

The following example shows how to use this syntax in practice.

Example: How to Remove Frames from Figures in Matplotlib

Suppose that we would like to create a simple bar chart in Matplotlib.

The following code shows how to create a bar chart using the default settings in Matplotlib:

#import necessary packages
import matplotlib.pyplot as plt
import numpy as np

#initialize figure
plt.figure()

#define x and y values to be used in plot
xvals = list('ABCDE')
yvals = np.array(range(1, 6))

#create bar chart
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)

This syntax produces the following bar chart in Matplotlib:

Notice that the plot has a frame around it on all sides by default.

Suppose that we would instead like to create this exact plot but without the frame around it.

We can use the following syntax to do so:

#import necessary packages
import matplotlib.pyplot as plt
import numpy as np

#initialize figure
plt.figure()

#remove frame from each side of plot
plt.rcParams['axes.spines.left'] = False
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.bottom'] = False

#define x and y values to be used in plot
xvals = list('ABCDE')
yvals = np.array(range(1, 6))

#create bar chart
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)

This syntax produces the following bar chart in Matplotlib:

Matplotlib remove frame

Notice that this produces the same bar chart as before but the entire frame around the plot has been removed.

Note that in some cases you may want to keep the frame on the left and bottom sides of the plot so that the tick marks and labels don’t look  out of place.

In this case, you can specify a value of True for the left and bottom spines:

#import necessary packages
import matplotlib.pyplot as plt
import numpy as np

#initialize figure
plt.figure()

#remove only right and top frames from plot
plt.rcParams['axes.spines.left'] = True
plt.rcParams['axes.spines.right'] = False
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.bottom'] = True

#define x and y values to be used in plot
xvals = list('ABCDE')
yvals = np.array(range(1, 6))

#create bar chart
position = np.arange(len(xvals))
mybars = plt.bar(position, yvals, align='center', linewidth=0)

This syntax produces the following chart:

Notice that the bottom and left frames of the plot remain in place while the top and right frames have been removed, just as we specified.

Note: You can find the complete documentation for Matplotlib axis spines here.

Additional Resources

The following tutorials explain how to perform other common tasks in Matplotlib:

How to Remove Ticks from Matplotlib Plots
How to Remove a Legend in Matplotlib
How to Change Font Sizes on a Matplotlib Plot

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *