Pandas DataFrame plot() Method

Rate this post

Preparation

Before any data manipulation can occur, three (3) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Matplotlib library displays a visual graph of a plotted dataset.
  • The Scipy library allows users to manipulate and visualize the data.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install matplotlib

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install scipy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import matplotlib.pyplot as plt
import scipy

DataFrame Plot

The plot() method creates visual graphs based on a dataset of a DataFrame or Series.

httpv://www.youtube.com/watch?v=embed/hKHrnR5IUQ0

The syntax for this method is as follows:

DataFrame.plot(*args, **kwargs)
ParameterDescription
dataThis parameter is a DataFrame/Series dataset.
xThis parameter is a label/position (for a DataFrame only).
kindThis parameter is a string and indicates the type of plot to create:
'line': default is this option
'density': same as ‘KDE’
β€˜bar’: vertical bar chart
'area': area plot
β€˜barh’: horizontal bar chart
'pie': pie plot
β€˜hist’: histogram
'scatter': scatter plot (DataFrame)
β€˜box’: boxplot
'hexbin': hexbin plot (DataFrame)
β€˜kde’: Kernel Density plot
axThis parameter is the Matplotlib axis object.
subplotsThis parameter makes subplots for each column separately.
sharexIf subplots, share x-axis and set some x-axis labels to invisible.
shareyIf subplots, share the y-axis and set some y-axis labels to invisible.
layoutA tuple that determines the row/column layout for subplots.
figsizeThis parameter sets the size (width and height) of the figure.
use_indexUse the index as ticks for the x-axis.
titleThe heading to use for the plot (graph).
gridThese are the axis grid lines.
legendDisplay legend on the axis subplots. Displays by default (True).
styleThe line style per column (matplotlib).
logxUse log/symlog scaling on the x-axis.
logyUse log/symlog scaling on the y-axis.
loglogUse log/symlog scaling on both the x-axis and y-axis.
xticksThe value to use for xticks.
yticksThe value to use for yticks.
xlimSet the x limits of the current axis.
ylimSet the y limits of the current axis.
xlabelName for the x-axis.
ylabelName for the y-axis.
rotThe rotation for ticks (xticks vertical/yticks horizontal).
fontsizeThe size of the font to use for both xticks/yticks.
colormapThis parameter is the color map to select specific colors.
positionThese are the alignments for the bar plot.
tableIf True, create a table using DataFrame data. This data will transpose to the matplotlib default layout.
yerrSee plotting with Error Bars.
xerrSee plotting with Error Bars.
stackedIf set to True, create a stacked plot.
sort_columnsThis parameter sorts the column name(s) for plot ordering.
secondary_yThis parameter determines if it plots on the secondary y-axis.
mark_rightIf set determines if using a secondary_y axis auto marks the column labels with right in the legend.
include_boolIf set to True, Boolean values will be available to plot.
backendThis parameter determines the backend to use instead of the option plotting.backend.
**kwargsThis parameter is the option(s) passed to the matplotlib library.

This example reads in the countries.csv file and plots the Country, Population, and Area columns on a Line chart.

πŸ’‘ Note: Click here to download this file. Move it to the current working directory,

df = pd.read_csv('countries.csv')
ax = plt.gca()

df.plot(kind='line', x='Country', y='Population', 
        title='Sample Countries', fontsize=8, ax=ax)
df.plot(kind='line',x='Country', y='Area', ax=ax)
plt.savefig('plot_line.png')
plt.show()
  • Line [1] reads in a comma-delimited CSV file and saves it to a DataFrame (df).
  • Line [2] gets the current axes (gca()) and saves it to ax.
  • Line [3] does the following:
    • sets the kind parameter to a Line chart
    • sets the columns to Country and Population
    • sets the title and font size
    • sets the ax variable created above
  • Line [4] does the following:
    • sets the kind parameter to a Line chart
    • sets the columns to Country and Area
    • sets the ax variable created above
  • Line [5] saves the Line chart as an image file and places this file in the current working directory.
  • Line [6] displays the Line chart on-screen.

πŸ’‘Β Note: The gca() method gets the current axes for the figure matching **kwargs, or creates a new one.

Output – On-Screen

The buttons on the bottom left can be used to further manipulate the chart.

πŸ’‘Β Note: Another way to create this chart is to use the plot.line() method.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.