Plotting with Pandas
Pandas ships with a thin plotting layer built directly on top of Matplotlib, exposed through the .plot() accessor available on every Series and DataFrame. This is not a separate charting library with its own grammar — it is a convenience wrapper that inspects your data's shape and index, then calls the appropriate Matplotlib functions for you. The payoff is speed: instead of manually extracting arrays and calling plt.plot(), you can go from a DataFrame straight to a chart in a single line, which makes it the natural first stop for exploratory data analysis (EDA) before you reach for more polished libraries like Seaborn or Plotly for publication-quality output.
Cricket analogy: The .plot() accessor is like a scoreboard operator who already knows how to draw a run-rate graph the moment you hand over the ball-by-ball sheet, sparing you from manually plotting each data point on graph paper yourself.
The .plot() Accessor and Kind Argument
Calling df.plot() with no arguments produces a line plot using the DataFrame's index as the x-axis and one line per numeric column. The kind parameter switches the chart type entirely: 'bar' and 'barh' for bar charts, 'hist' for histograms, 'box' for box plots, 'scatter' for scatter plots (which requires explicit x and y column names), 'pie' for pie charts, and 'area' for stacked area charts. Because kind is just a string dispatch, df.plot(kind='bar') and df.plot.bar() are exactly equivalent — the dot-notation shortcuts (.plot.bar(), .plot.hist(), .plot.scatter()) exist purely for readability and tab-completion discoverability.
Cricket analogy: df.plot() with no arguments draws a run-rate line per innings by default; switching kind to 'bar' compares total sixes per player, 'hist' shows the distribution of scores, and 'scatter' needs explicit x/y like balls-faced versus runs-scored.
import pandas as pd
import matplotlib.pyplot as plt
sales = pd.DataFrame({
'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'north': [120, 135, 150, 142, 160],
'south': [95, 110, 105, 130, 128]
}).set_index('month')
# Line plot: one line per numeric column, index as x-axis
ax = sales.plot(kind='line', title='Regional Sales', figsize=(8, 4))
ax.set_ylabel('Units Sold')
# Bar plot comparing regions per month
sales.plot.bar(rot=0, title='Sales by Region')
# Histogram of a single Series with 10 bins
(sales['north'] - sales['south']).plot.hist(bins=10, alpha=0.7)
plt.show() # required to render in a plain Python scriptCustomization and the Underlying Axes Object
Every pandas plotting call returns a Matplotlib Axes object (or an array of them when subplots=True), which means you are never locked into pandas' defaults. You can chain .set_title(), .set_xlabel(), .legend(), and any other Axes method after the call, or pass an existing ax= argument to draw multiple pandas plots onto the same figure. Common keyword arguments include figsize (tuple of width/height in inches), title, xlim/ylim, grid, legend, colormap, and subplots=True to give each column its own panel. Because the whole system is Matplotlib underneath, any styling trick that works for plt — themes, rcParams, saving with plt.savefig() — works identically here.
Cricket analogy: Every pandas plot returns a Matplotlib Axes, like a groundskeeper handing you the finished pitch to add your own boundary markers - you can chain set_title() to label a series chart or pass ax= to overlay two teams' run rates on one graph.
Think of df.plot() as a translator, not a renderer: it converts your DataFrame's shape into the equivalent ax.plot()/ax.bar()/ax.hist() call and hands you back the live Matplotlib object, so anything you already know about Matplotlib customization transfers directly.
A common pitfall is calling .plot() on a DataFrame with a non-numeric or unsorted index and expecting a clean x-axis — pandas plots in index order, not in a re-sorted order, so always sort_index() or sort_values() first when the visual order matters, especially with date-like string columns that haven't been converted to datetime64.
When to Reach for Something Else
Pandas plotting is optimized for speed during exploration, not for fine-grained aesthetic control or interactivity. Once you need faceted grids, statistical overlays (regression lines, confidence bands), or a consistent visual theme across a report, Seaborn — which itself builds on Matplotlib and understands DataFrames natively via its data= argument — is usually the better tool. For interactive, browser-based dashboards, Plotly or Bokeh are more appropriate. A practical workflow is: use .plot() liberally while exploring a dataset, then re-implement only the two or three charts that matter in a more capable library for the final deliverable.
Cricket analogy: Pandas plotting is fine for a quick net-session review, but for the official match-day broadcast graphics with player-comparison overlays you'd reach for a more polished tool - use .plot() to explore, then rebuild the key chart properly for the report.
.plot()is a wrapper around Matplotlib, not an independent plotting engine.- The
kindargument (or.plot.<kind>()shortcuts) controls chart type: line, bar, hist, box, scatter, pie, area. - The DataFrame's index becomes the x-axis by default for line and bar plots.
- Every call returns a Matplotlib
Axesobject, so full Matplotlib customization remains available. - Sort your index or date column before plotting to avoid misleading, out-of-order x-axes.
- Use pandas plotting for fast EDA; switch to Seaborn/Plotly for polished or interactive output.
Practice what you learned
1. What library does pandas' `.plot()` method use internally to render charts?
2. Which two expressions are functionally equivalent?
3. By default, what does pandas use as the x-axis for `df.plot(kind='line')`?
4. What type of object does a pandas `.plot()` call return?
5. Which keyword argument for `.scatter()` plots is mandatory but not required for `.line()` plots?
Was this page helpful?
You May Also Like
Exporting and Reporting Results
Learn how to write pandas DataFrames back out to CSV, Excel, JSON, and other formats, and how to structure exports for downstream tools and stakeholder-facing reports.
Aggregations and Statistics
NumPy provides fast, axis-aware aggregation functions like sum, mean, std, and var, plus NaN-safe variants for handling missing values in numerical data.
Time Series Basics
Learn pandas' Timestamp, DatetimeIndex, and date_range tools that form the foundation for working with time-indexed data.
GroupBy Basics
Understand pandas' split-apply-combine model via groupby(), including grouping keys, selecting columns, and iterating over groups.