Plotting with Pandas

Matplotlib is a Python library for creating charts and visualizations — and pandas plugs straight into it, so you can chart a whole DataFrame with a single .plot() call.

Learn Plotting with Pandas in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll learn how pandas wraps Matplotlib, turning columns of data into line and bar charts and then styling them with the familiar pyplot tools.

pandas ships with a .plot() method on both DataFrames and Series. Behind the scenes it calls Matplotlib for you, so a single line draws an entire table of data.

Build a small DataFrame and plot every column at once:

What you'll see: a chart with two lines — one for "North" and one for "South" — sharing the same month labels along the bottom. Pandas read the column names to build the legend in the corner, and the DataFrame index (Jan–May) became the x-axis automatically.

Pass kind='bar' to draw bars instead of lines, and use df['col'] to plot just one column as a Series.

What you'll see: a grouped bar chart with a North bar and a South bar standing side by side for each month. Selecting a single column with df["North"] gives you a Series, whose .plot() draws only that one set of values.

When your data lives in ordinary columns rather than the index, name them explicitly with x= and y= . The .plot() call returns a Matplotlib Axes you can style.

What you'll see: a single line rising from 100 to 240 with a circular marker at each point. Because you captured the return value in ax , you could add a title and axis labels using the same Axes methods you learned earlier in the course.

Replace each ___ to plot a DataFrame as a bar chart and show it.

❌ ModuleNotFoundError: No module named 'pandas'

Install it first with pip install pandas . Matplotlib must also be installed.

The column name passed to x= or y= doesn't exist. Check spelling and capitalization against your DataFrame.

Build a DataFrame of two cities' temperatures, plot both lines, and add a title.

Lesson 16 complete — you can chart a DataFrame in one line!

You learned that pandas .plot() wraps Matplotlib, drew lines and bars from columns, and styled the returned Axes just like a regular plot.

🚀 Up next: Heatmaps & imshow — visualize 2D data as a grid of colors.

Practice quiz

Which library's plotting is df.plot() a thin wrapper around?

  • Seaborn
  • Matplotlib
  • Plotly
  • Bokeh

Answer: Matplotlib. pandas .plot() builds the figure using Matplotlib under the hood.

What object does df.plot() return?

  • A Matplotlib Axes
  • A DataFrame
  • A Figure
  • None

Answer: A Matplotlib Axes. It returns a Matplotlib Axes you can style with set_title, set_xlabel, etc.

Which argument draws bars instead of lines?

  • type='bar'
  • mode='bar'
  • kind='bar'
  • style='bar'

Answer: kind='bar'. df.plot(kind='bar') draws a bar chart; df.plot.bar() is equivalent.

What becomes the x-axis when you call df.plot() with no x argument?

  • The first column
  • The DataFrame index
  • A range 0..n
  • The last column

Answer: The DataFrame index. The DataFrame index is used as the x-axis automatically.

How do you plot a single column named 'North' as a Series?

  • df.plot('North')
  • df.column('North')
  • df.North.col().plot()
  • North

Answer: North. Selecting df['North'] gives a Series whose .plot() draws just that column.

How do you choose which columns are x and y explicitly?

  • df.plot(x='month', y='revenue')
  • df.plot(rows='month')
  • df.plot(axis='month')
  • df.plot(index='month')

Answer: df.plot(x='month', y='revenue'). Pass x= and y= to name the columns used for each axis.

What is df.plot.bar() equivalent to?

  • df.plot(kind='barh')
  • df.plot(kind='line')
  • df.plot(kind='bar')
  • df.bar()

Answer: df.plot(kind='bar'). df.plot.bar() and df.plot(kind='bar') do exactly the same thing.

After capturing ax = df.plot(), how do you set the title?

  • ax.title('X')
  • ax.add_title('X')
  • df.title('X')
  • ax.set_title('X')

Answer: ax.set_title('X'). ax is a Matplotlib Axes, so ax.set_title() works as usual.

What error means a column name passed to y= does not exist?

  • KeyError
  • ValueError
  • TypeError
  • IndexError

Answer: KeyError. A missing column name raises KeyError; check spelling and capitalization.

In a plain script, what still opens the chart window after df.plot()?

  • df.render()
  • plt.draw()
  • plt.show()
  • df.display()

Answer: plt.show(). Call plt.show() after df.plot() to display the chart in a script.