Line, bar, and scatter charts from one DataFrame

df.plot: Plot a pandas DataFrame in Python

Use df.plot to turn a pandas DataFrame into a chart with one line of Python. You can start with the default index-based chart, then choose specific columns, switch between line, bar, and scatter kinds, and refine the result through Matplotlib.

The examples below use one small DataFrame, so you can run them in the same Python session and see how each argument changes the chart.

Create a chart with df.plot

First, create a complete DataFrame with a text column for months and three numeric columns:

import pandas as pd

df = pd.DataFrame({

    “month”: [“Jan”, “Feb”, “Mar”, “Apr”, “May”],

    “sales”: [120, 150, 170, 160, 190],

    “expenses”: [80, 95, 110, 100, 115],

    “customers”: [30, 36, 42, 40, 48]

})

Call df.plot() to create the default line chart:

df.plot()

By default, pandas uses the DataFrame index for the x-axis. In this example, the index is 0 through 4, so the chart displays those numbers instead of the month names. Pandas plots each suitable numeric column as a separate series, which means sales, expenses, and customers appear as lines with a legend. The text column is not plotted as a numeric series.

Choose columns and a kind for a DataFrame plot

Pass the column name to x when you want labels from a DataFrame column rather than its index. Pass one column or a list of columns to y. This explicit DataFrame plot uses months on the x-axis and sales as the only y series:

df.plot(x=”month”, y=”sales”, kind=”line”, marker=”o”)

The kind argument selects the chart type. Common choices include “line”, “bar”, “barh”, “scatter”, “hist”, and “box”. For example, compare sales and expenses with grouped bars:

df.plot(x=”month”, y=[“sales”, “expenses”], kind=”bar”)

A scatter chart needs numeric values for both axes. This example compares sales and customers, with each row becoming one point:

df.plot(x=”sales”, y=”customers”, kind=”scatter”)

If you omit x and y, pandas falls back to the index and all eligible columns. If you supply them, you control exactly what the chart represents. Use a list for y when you want several series in one line or bar chart.

Customize a DataFrame.plot with Matplotlib

You can pass display options directly to DataFrame.plot. For example, set the chart size, title, grid, line style, and x-axis rotation in one call:

ax = df.plot(x=”month”, y=[“sales”, “expenses”], kind=”line”, figsize=(8, 4), title=”Monthly performance”, grid=True, style=[“-“, “–“], rot=45)

figsize uses inches as width and height. The title adds a heading, grid=True makes values easier to read, style sets line patterns, and rot=45 rotates crowded tick labels. You can also pass options such as color, linewidth, alpha, and legend, depending on the chart kind.

For more control, adjust the Matplotlib object returned by the plot:

ax.set_xlabel(“Month”)

ax.set_ylabel(“Units”)

ax.set_title(“Sales and expenses by month”)

Work with the returned axes from a pandas plot

A pandas plot returns a Matplotlib Axes object. Store that object in ax whenever you need to modify the chart after pandas creates it:

ax = df.plot(x=”month”, y=”sales”, kind=”bar”, color=”steelblue”, figsize=(7, 4))

ax.set_title(“Monthly sales”)

ax.set_xlabel(“Month”)

ax.set_ylabel(“Sales”)

ax.tick_params(axis=”x”, rotation=45)

For layout adjustments, get the figure that contains the axes and apply Matplotlib’s automatic spacing:

fig = ax.get_figure()

fig.tight_layout()

This returned-axes pattern lets you combine pandas’ convenient data selection with Matplotlib’s labels, annotations, tick settings, legends, and layout controls.