Plotting from Pandas

Python · Data Visualization

Theory

pandas DataFrames and Series have a built-in .plot that calls matplotlib for you. The index becomes the x-axis and each numeric column becomes a series, so df.plot() on a table indexed by month draws one line per column, with a legend. Pick the chart with kind= or the accessor style: df.plot.bar(), df.plot.barh(), df.plot.hist(bins=10), df.plot.box(), df.plot.scatter(x='a', y='b') and df.plot.pie(y='col').

The usual pattern is aggregate first, then plot: orders.groupby('city')['amount'].sum().sort_values().plot.barh() turns raw rows into a ranked bar chart in one line. stacked=True stacks bars, subplots=True gives one panel per column, and title=, figsize= and color= pass straight through. .plot returns a matplotlib Axes, so you can keep customising it, and ax=ax draws into an Axes you created with plt.subplots.

Why we use it

Most analysis data already lives in DataFrames. df.plot turns a groupby result or a pivot table into a chart in one line, which is ideal for quick exploration before polishing a final figure.

When to use it

Use df.plot for fast exploratory charts straight from tables and groupby results. Switch to matplotlib calls on the returned Axes for fine-tuning, and to seaborn when you want grouped statistical charts with little code.

How it works

  1. DataFrame of raw rows
  2. Already one row per x value? Yes: Plot directly. No: groupby / pivot_table to aggregate first.
  3. Sort if it is a ranking
  4. Call .plot.bar(), .plot(), .plot.hist() ...
  5. Customise the returned Axes (labels, limits)
  6. Quick chart from the table

Syntax

df.plot(x='col', y=['a', 'b'])
df.plot.bar(stacked=True)  df.plot.barh()  df.plot.hist(bins=10)
df.plot.scatter(x='a', y='b')
ax = s.plot.bar(title='...'); ax.set_ylabel('...')

Example

totals = orders.groupby('city')['amount'].sum()
ax = totals.sort_values().plot.barh(title='Sales by city')
ax.set_xlabel('Rs')

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic