Pandas Plotting

The DataFrame.plot() method turns any DataFrame or Series into a matplotlib chart with a single method call.

Getting Started with df.plot()

Every DataFrame and Series has a .plot() accessor that builds a matplotlib chart directly from your data, so you don't have to hand columns off to matplotlib yourself. It is a convenience wrapper: pandas figures out the axes and data for you, but matplotlib does the actual drawing, which means matplotlib must be installed and every customization option matplotlib exposes is still reachable through the returned Axes object.

Example

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'revenue': [12000, 15500, 14200, 18900, 21000, 19800]
})

df.plot(x='month', y='revenue', kind='line', title='Monthly Revenue')
plt.show()

Choosing a Plot Kind

The kind parameter controls what type of chart gets drawn. It defaults to 'line', which works well for data ordered by an index such as dates, but pandas supports several other kinds suited to different shapes of data.

  • line (default) connects points in order, ideal for trends over time.
  • bar / barh draws vertical or horizontal bars for comparing categories.
  • hist draws a histogram showing the distribution of a single column.
  • box draws a box-and-whisker plot summarizing quartiles and outliers.
  • kde draws a smoothed density curve, an alternative to a histogram.
  • area draws a stacked or filled area chart for cumulative trends.
  • pie shows the proportions of a whole for a single column.
  • scatter plots the relationship between two numeric columns.

Example

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'product': ['Widgets', 'Gadgets', 'Gizmos', 'Doohickeys'],
    'units_sold': [340, 220, 510, 180]
})

df.plot(x='product', y='units_sold', kind='bar', color='steelblue', legend=False)
plt.ylabel('Units Sold')
plt.show()

Customizing Plots

ParameterPurpose
kindChart type, e.g. 'line', 'bar', 'scatter'
figsizeWidth and height of the figure in inches, e.g. (8, 5)
titleText shown above the chart
xlabel / ylabelAxis labels (or use ax.set_xlabel / ax.set_ylabel)
legendWhether to show the legend (True/False)
gridAdds background gridlines when True

Example

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'advertising_spend': [200, 400, 600, 800, 1000, 1200],
    'sales': [2200, 2600, 3100, 3400, 4000, 4300]
})

ax = df.plot(
    x='advertising_spend',
    y='sales',
    kind='scatter',
    figsize=(6, 4),
    title='Advertising Spend vs. Sales',
    grid=True
)
ax.set_xlabel('Advertising Spend ($)')
ax.set_ylabel('Sales ($)')
plt.show()
Note: df.plot() returns a matplotlib Axes object, so you can keep calling methods like ax.set_xlabel() or ax.axhline() on it afterward. In a Jupyter notebook charts usually render inline automatically; in a plain Python script you need plt.show() to open the chart window, as in the examples above.
Note: matplotlib is an optional dependency of pandas, not a required one, so a fresh pandas install may not include it. If df.plot() raises 'ModuleNotFoundError: No module named matplotlib', run pip install matplotlib and try again.

Exercise: Pandas Plotting

Which method is used to create a quick plot directly from a DataFrame?