Fast plotting with Pandas
By: Tom Waterman
Tags: Python, Data Science, Data Visualization, Pandas

Fast plotting with Pandas

Quickly visualize your data with these simple built-in functions

One of Pandas’ best features is the built-in plot function available on its Series and DataFrame objects.

But the official tutorial for plotting with Pandas assumes you’re already familiar with Matplotlib, and is relatively unforgiving to beginners.

So I thought an easy overview of plot 's functionality would be useful for anyone wanting to visualize their Pandas data without learning a whole plotting library.

To get started using the plot function, you’ll need to have Matplotlib installed (although you won’t ever need to use Matplotlib directly).

pip install matplotlib

You can also instruct a Jupyter notebook to automatically print the Pandas plots directly in the notebook by using an iPython magic command.

In [1]: %matplotlib inline

Your Jupyter notebook will now automatically generate a chart in the notebook whenever one of Pandas’ plot functions is used.

Plotting a Series object

To automatically visualize the data in a series object, you can call its plot method.

In [2]: import pandas as pd In [3]: series = pd.Series([1,2,3,5], index=['a', 'b', 'c', 'd']) In [4]: series.plot()

A simple plot from a Pandas Series object

Notice how Pandas uses the index of the series for the X-axis, while the values of the series are used for the Y-axis.

Tip: you can export a plot from the notebook by shift right-clicking the image, and then selecting “Save Image As…”

If you use a numerical index for the series instead of a categorical index, Pandas will correctly adjust the scale of the X-axis.

In [5]: series.index = [1, 1.75, 2, 4] In [6]: series.plot()

Pandas automatically scales the X-axis if it’s index is numerical

As you’ve seen already, Pandas will create a line chart when using the plot function. You can create other plot types by using the function plot.[option] . For example, you can use plot.area to create an area chart.

In [7]: series.plot.area()

Pandas supports many types of visuals, including area charts

The full list of available charts you can generate from a Series object is available in the documentation.

Plotting DataFrame objects

Pandas also provides a plot method on the DataFrame object.

In [8]: data = {'a': [2, 1, 2, 9], 'b': [1, 0, 1, 2]} In [9]: df = pd.DataFrame(data, index=[-1, 0, 1, 2]) In [10]: df.plot()

Pandas supports plotting multiple columns at once

Notice how Pandas has plotted both of the columns of the DataFrame on a single Y-axis, and it’s used the DataFrame’s index for the X-axis. It’s also added a label in the top-left corner.

There are other built-in plotting methods that are specially available for DataFrames, like the plot.scatter method.

In [11]: df.plot.scatter(x="a", y="b")

Pandas makes it easy to create scatter plots from a DataFrame

The full list of DataFrame plotting methods are available in the documentation.

Additional plotting methods

Finally, you can use additional plotting functions from the pandas.plotting module. For example, you can create a scatter-matrix from a DataFrame.

In [12]: df['c'] = [3, 6, 4, 1] In [13]: from pandas.plotting import scatter_matrix In [14]: scatter_matrix(df)

Pandas can also quickly generate scatter-matrix plots from a DataFrame

The full list of additional methods is available in the documentation.