luvv to helpDiscover the Best Free Online Tools
Topic 28 of 30

Basic Plotting With Pandas

Learn Basic Plotting With Pandas for free with explanations, exercises, and a quick test (for Data Analyst).

Published: December 20, 2025 | Updated: December 20, 2025

Who this is for

Beginner to intermediate data analysts who use pandas and want quick, clear visualizations directly from DataFrames and Series without switching tools.

Prerequisites

  • Basic Python (variables, lists, functions)
  • Basic pandas (DataFrame/Series, selecting columns, simple aggregations)
  • Matplotlib installed (pandas plotting uses matplotlib under the hood)

Why this matters

In real analysis work, you will:

  • Check data quality quickly (spot outliers, missing time periods)
  • Share KPIs with stakeholders (monthly trends, category comparisons)
  • Explore relationships (e.g., ad spend vs sales)

Pandas plotting lets you do all of that in seconds using the data you already have in memory.

Concept explained simply

Every pandas Series and DataFrame has a .plot(...) method that produces a chart. You choose the chart type with kind= (or a convenience method like .plot.line, .plot.bar, .plot.scatter). The x-axis is the index by default, and the y-axis is the column values. The result is a Matplotlib Axes you can further customize.

Mental model

  • DataFrame/Series => .plot => Matplotlib Axes
  • Index becomes x-axis by default
  • One Series = one line/bar/curve; multiple columns = multiple lines/bars
  • Customize via parameters in .plot or on the Axes it returns
Quick reference: common plot types
  • Line: df.plot() or df.plot.line()
  • Bar: df.plot.bar() or df.plot(kind='bar')
  • Horizontal bar: df.plot.barh()
  • Histogram: df['col'].plot.hist(bins=20)
  • Box plot: df.plot.box()
  • Scatter: df.plot.scatter(x='col1', y='col2')
  • Area: df.plot.area()

Quick start: 3 lines you will use often

import pandas as pd
import matplotlib.pyplot as plt

# 1) Line
s = pd.Series([10, 12, 9, 14], index=['Q1','Q2','Q3','Q4'])
s.plot(title='Quarterly Sales')
plt.show()

# 2) Bar
cats = pd.DataFrame({'category':['A','B','C'],'revenue':[120, 300, 180]}).set_index('category')
cats.plot.bar(title='Revenue by Category', legend=False)
plt.show()

# 3) Scatter
kpi = pd.DataFrame({'ad_spend':[1.2,1.5,1.1,1.8], 'sales':[12, 15, 11, 17]})
kpi.plot.scatter(x='ad_spend', y='sales', title='Ad Spend vs Sales')
plt.show()

Worked examples

Example 1: Line chart of monthly trend

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'month': pd.period_range('2024-01', periods=6, freq='M').astype(str),
    'sales': [120, 135, 128, 150, 160, 155]
}).set_index('month')

ax = df['sales'].plot.line(color='royalblue', marker='o', title='Monthly Sales')
ax.set_xlabel('Month')
ax.set_ylabel('Sales (k)')
ax.grid(True, alpha=0.3)
plt.show()

Why it works: the index (month) becomes the x-axis. Marker helps spot individual points.

Example 2: Bar chart for category comparison

import pandas as pd
import matplotlib.pyplot as plt

cats = pd.DataFrame({
    'category': ['A','B','C','D'],
    'revenue': [320, 280, 410, 150]
}).set_index('category')

ax = cats.plot.bar(color=['#1f77b4'], legend=False, title='Revenue by Category')
ax.set_ylabel('Revenue (k)')
for p in ax.patches:
    ax.annotate(f"{int(p.get_height())}", (p.get_x()+p.get_width()/2, p.get_height()),
                ha='center', va='bottom', fontsize=9)
plt.show()

Why it works: bars are ideal for categorical comparisons. Simple annotation adds values.

Example 3: Scatter plot to explore a relationship

import pandas as pd
import matplotlib.pyplot as plt

data = pd.DataFrame({
    'ad_spend': [1.0, 1.2, 1.5, 1.7, 2.0, 2.2],
    'sales':    [10, 11, 14, 15, 17, 18]
})

ax = data.plot.scatter(x='ad_spend', y='sales', color='tomato', alpha=0.8,
                       title='Ad Spend vs Sales')
ax.grid(True, alpha=0.3)
plt.show()

Why it works: scatter shows whether higher spend tends to correlate with higher sales.

Customization essentials

  • Size: df.plot(figsize=(8,4))
  • Colors: color='steelblue' or list like color=['#...','#...']
  • Style: style='--o', linewidth=2, alpha=0.8
  • Titles/labels: title=, or use ax.set_title, ax.set_xlabel, ax.set_ylabel
  • Legend: legend=True/False; rename columns for clean legend
  • Grid: ax.grid(True, alpha=0.3)
  • Subplots: df.plot(subplots=True, layout=(2,2))
  • Secondary axis: df.plot(secondary_y='colB') when scales differ
  • Rotate ticks: plt.xticks(rotation=45)
Tip: saving your chart

The Axes belongs to a Figure. Use plt.gcf().savefig('chart.png', dpi=150, bbox_inches='tight') after plotting.

Choosing the right chart

  • Trend over time: line
  • Compare categories: bar or barh
  • Distribution: histogram or box
  • Relationship: scatter

Common mistakes and self-check

  • Using the wrong x-axis: ensure your intended x is the index or pass x= for scatter.
  • Unreadable labels: rotate or shorten category names.
  • Too many lines: limit to the most important columns or use subplots.
  • Mixed scales on one axis: use secondary_y for a second scale.
  • Misleading aspect: always label units and include a title that clarifies context.
Self-check mini-list
  • Can a stakeholder understand the message in 5 seconds?
  • Are axes labeled and units clear?
  • Is the chosen chart type aligned with the question?
  • Are colors consistent and accessible?

Exercises (practice now)

These mirror the tasks in the Exercises section below. Do them in order. A checklist is provided for each.

Exercise 1: Monthly line plot

  • Create a DataFrame with month and sales.
  • Set month as index and plot a line with markers.
  • Add title, x/y labels, and a grid.

Exercise 2: Top categories bar chart

  • Create a DataFrame with category and revenue.
  • Sort by revenue, take top 5, and plot a bar chart.
  • Hide legend and annotate bar values.

Exercise 3: Scatter of ad spend vs sales

  • Create a DataFrame with ad_spend and sales.
  • Plot a scatter with semi-transparent points.
  • Add a clear title and grid.

Practical projects

  • Weekly web traffic: plot sessions, users, conversions over 12 weeks on subplots; annotate a campaign start date.
  • Store performance: bar chart of top 10 stores by revenue; color stores by region using a color list.
  • Customer behavior: scatter average order value vs number of orders; size points by customer tenure (use s=), and discuss patterns.

Learning path

  • Before: importing/cleaning data in pandas, basic aggregations
  • Now: basic plotting with pandas (this subskill)
  • Next: advanced formatting, multi-axis charts, and customizing Matplotlib objects

Next steps

  • Refactor plotting code into reusable functions (e.g., plot_top_categories(df, n=5))
  • Create a small plotting style guide for your team (colors, fonts, defaults)
  • Take the Quick Test at the end of this page to check your understanding

Note: The Quick Test is available to everyone. If you are logged in, your progress will be saved.

Mini challenge

Given a DataFrame with daily orders and returns, create a two-panel subplot: left is daily orders (line with markers), right is returns rate (%) as a bar chart. Add a horizontal line at the average returns rate.

Practice Exercises

3 exercises to complete

Instructions

Create a line chart of monthly sales with clear labels.

  • Build a DataFrame with columns month and sales for 6 months.
  • Set month as the index and plot a line with markers.
  • Add a title, axis labels, and a faint grid.
Expected Output
A line chart with 6 points connected in order of month, titled 'Monthly Sales', with readable month labels and a light grid.

Basic Plotting With Pandas — Quick Test

Test your knowledge with 8 questions. Pass with 70% or higher.

8 questions70% to pass

Have questions about Basic Plotting With Pandas?

AI Assistant

Ask questions about this tool