Matplotlib - Session 2
Turning Data Into Decisions Bar charts, histograms, scatter plots, subplots, and plotting straight from pandas Previously learned to draw a line — literally. we now know how to create a figure, style it, and save it. But real analyst work rarely stops at trends over time. You'll need to compare categories , understand distributions , spot relationships between variables , and show several views of the data at once . That's exactly what today covers. Grab a coffee — let's turn raw numbers into charts that actually tell a story. 1. Bar Charts: Comparing Categories When to use one Bar charts are your go-to whenever you're comparing discrete categories against each other — regions, products, departments, months. If someone asks "which one is bigger?", a bar chart answers it instantly. The code import matplotlib.pyplot as plt regions = [ " North " , " South " , " East " , " West " ] revenue = [ 420 , 380 , 510 , 290 ] fig , ax = plt . subplots ( figsize = ( 7 , 5 )) ax . bar ( regions , revenue , color = " teal " ) ax . set_title ( " Revenue by Region " ) ax . set_xlabel ( " Region " ) ax . set_ylabel ( " Revenue ($K) " ) plt . show () A useful variant: horizontal bars When category names are long, flip the chart with barh() — it's far easier to read than squeezing labels sideways: fig , ax = plt . subplots ( figsize = ( 7 , 5 )) ax . barh ( regions , revenue , color = " darkorange " ) ax . set_title ( " Revenue by Region " ) ax . set_xlabel ( " Revenue ($K) " ) plt . show () Rule of thumb: categories on the x-axis → bar() . Long labels or many categories → barh() . 2. Histograms: Understanding Distributions Bar chart vs. histogram — don't mix them up This trips up almost every beginner: a bar chart compares separate categories. A histogram shows how continuous numeric data is distributed by grouping values into ranges called bins . There are no gaps between histogram bars by convention, because the x-axis is continuous, not categorical. The code import matplotlib.pyplot