Built-in Plotting
Published Jan 21, 2025
Contribute to Docs
Pandas provides a convenient and powerful way to visualize data directly from DataFrames and Series with built-in plotting capabilities. Using the plot()
function, Pandas makes it easy to generate various types of plots, such as line plots, bar plots, histograms, and more. The plot()
function uses Matplotlib behind the scenes to render these plots.
Syntax
DataFrame.plot(kind='line', x=None, y=None, ax=None, **kwargs)
kind
: The type of plot to create (e.g.,'line'
,'bar'
,'hist'
, etc.).x
(Optional): The column to use as the x-axis.y
(Optional): The column(s) to plot.ax
(Optional): A MatplotlibAxes
object to draw the plot on. If not specified, a new figure and axes are created.**kwargs
: Additional arguments passed to the underlying Matplotlib function (e.g.,color
,linewidth
,title
).
Example
Here’s an example that demonstrates how to use the built-in plotting functionality in Pandas to create a simple line plot from a DataFrame:
import pandas as pdimport matplotlib.pyplot as plt# Create a DataFramedata = {'Year': [2019, 2020, 2021, 2022, 2023],'Sales': [150, 200, 250, 300, 350]}df = pd.DataFrame(data)# Plotting the datadf.plot(kind='line', x='Year', y='Sales', title='Sales Over Years', xlabel='Year', ylabel='Sales')# Show the plotplt.show()
The above code will generate the output as follows:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pandas on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours