The core charts
Last updated
Five charts answer most questions about a table: a line for change over time, bars for comparing groups, a scatter plot for two numbers, a histogram for how one number is spread, and a box plot for comparing spreads. In this lesson you'll draw all five from the Tindahan data, match questions to charts, and see what a line does to categories that have no order.
By the end of this lesson you can
- Draw a line chart, a bar chart and a scatter plot with ax.plot, ax.bar and ax.scatter
- Show one number's distribution with a histogram and a box plot
- Pick the chart that fits a question
Lines for change over time
A line chart puts time along the x-axis and joins the values in order. The line says "this follows from that": the eye reads its slope as a rise or a fall. Run both cells.
The first cell adds up all items and stores for each day with groupby (4.1), giving one number per date. ax.plot(daily.index, daily.values) draws it: pandas dates on the x-axis work as they are, and matplotlib picks the date labels (6.9 shows how to choose them).
The chart shows what a table of 60 numbers can't: a repeating weekly bump. Lesson 6.7 finds which day it is.
Bars for comparing groups
A bar chart gives each group one bar, and the bar's length is its value. Lengths are easy to compare, as long as every bar starts from zero (lesson 6.8). Run the cells.
ax.bar(names, values)draws upright bars. The data was sorted first withsort_values(ascending=False), so the bars are biggest to smallest and the ranking reads left to right.ax.barhdraws them sideways. Long names fit along the y-axis without being tilted, which makes sideways bars the better choice once there are more than a few groups or the names are long.[::-1]reverses the order so the biggest is on top, becausebarhdraws its first bar at the bottom.
Batangas sold 2,744 kg of rice in the two months, Rosario 1,371 kg.
Scatter plots for two numbers
A scatter plot draws one dot for each row, with one number across and another up. It's how you look at whether two things go together. Here a row is one store on one day: how much rain fell in its town, and how much it sold. The first cell joins the sales to the weather through the stores table (merge, 4.3). Run the cells.
alpha=0.6makes each dot partly see-through, so where dots pile up the colour gets darker.- The first thing you see is a wall of dots at zero rain: the third cell says 69% of store-days were dry. A scatter plot shows you how the data is shaped before any number does.
- On rainy days the dots sit a little lower. The correlation is −0.17: a weak tendency to sell less in the rain, and lots of scatter around it. Stage 8 teaches what that number can and can't tell you.
Histograms and box plots for spread
A histogram shows how one number is spread: it cuts the range into equal bins and draws a bar for how many values fall in each. A box plot sums the same spread up in a few marks, small enough to put several side by side. Run the cells.
- The histogram of 240 store-days (60 days, 4 stores) has a long right tail: most days sell 30 to 55 kg, a few sell up to 91.5. The median, 41.1, is below the mean, 43.8, because the tail pulls the mean up.
bins=20cuts the range into 20; try 5 and 60 and see how the shape changes (lesson 7.2 is about choosing). - The box plot draws one box per store. The box runs from to , the middle half of the days; the line inside is the median; the whiskers reach to the furthest day within 1.5 IQR of the box, the fences from lesson 5.7; and any day beyond is drawn as a single circle. Batangas sells the most and also varies the most: the tallest box. The circles above the boxes are almost all Saturdays, the busiest day of the week, not mistakes. Rosario's box is so narrow that four ordinary Saturdays fall outside its whiskers.
Which chart for which question
The question picks the chart. For each question, choose the chart that answers it best.
0 of 5 placed right
How did Lipa’s daily sugar sales change from January to February?
Which of the four stores sold the most oil?
Do hotter days sell more softdrinks?
Are most baskets small, with a few big ones, or are they all about the same?
Which store has the most unpredictable daily sales?
A rule of thumb for the five: time, a line; groups, bars; two numbers, a scatter; one number's spread, a histogram; spreads of several groups, box plots. Lesson 6.4 adds the cases these don't cover, and why some popular charts are harder to read than they look.
Wrong way: a line through the stores
A line is quick to type, so it's tempting for everything. Here it's used for rice sold by store. Run the first two cells.
The line goes down from Batangas to Lipa, down to Rosario, then back up to Tanauan. A reader sees a trend: falling, then recovering. But there is no "between Lipa and Rosario". The stores aren't in time order, or any order; pandas simply listed them alphabetically. Sort them another way and the "trend" changes shape.
A line says the points come in an order and that the values in between mean something. Stores, items and towns are categories with no order, so they get bars, as in the third cell. The check: before drawing a line, ask what the x-axis is. If it isn't time, or another number where the gaps mean something, don't join the points.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- matplotlib: Plot types: a gallery of every basic chart, with its code
- matplotlib: matplotlib.axes.Axes.bar: bars, their width and colour
- matplotlib: matplotlib.axes.Axes.hist: bins
- matplotlib: matplotlib.axes.Axes.boxplot: the box, the whiskers at 1.5 IQR, and the points beyond
- matplotlib: matplotlib.axes.Axes.scatter: alpha, size and colour