Module 7 · Charts with pandas · Lesson 37 of 66
Built-in pandas charts
Draw a chart straight from a DataFrame with df.plot, and restyle everything with style sheets.
In this lesson · about 16 minutes
- Draw histograms, bar, line, area, scatter, box, hexbin and density charts with df.plot
- Change the look of every chart with Matplotlib style sheets
- Pass Matplotlib options such as color, size and figsize through df.plot
Every DataFrame and Series has a .plot attribute that draws with Matplotlib for you. It's the fastest way to look at your data while you work: no need to pull out columns or label the axes yourself.
We'll use three small tables of random numbers. df1 has four columns A to D and one row per day; df2 has ten rows and columns a to d.
Style sheets
Matplotlib has ready-made looks called style sheets. plt.style.use('ggplot') changes every chart drawn after it. Try 'bmh', 'fivethirtyeight', 'dark_background' or 'seaborn-v0_8', and see the full list with plt.style.available.
Chart types
Pick a chart with a method on .plot: df.plot.area, .bar, .barh, .hist, .line, .scatter, .box, .hexbin, .kde (also called .density) and .pie. You can also write df.plot(kind='bar'). Any extra argument goes to Matplotlib, so color, alpha, figsize, lw and ls all work.
Bars, one group per row
Exercise
Stacked bars
Draw df2 as a bar chart with the four columns stacked on top of each other in each bar.
Code that runs before yours
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.read_csv('df1.csv', index_col=0, parse_dates=True)
df2 = pd.read_csv('df2.csv')Line charts and scatter plots
A line chart uses the index for x unless you name another column. For a scatter plot, name both columns. c= colors the points by a third column (with cmap picking the colors) and s= sets their size, which can also come from a column.
Exercise
Color by a third column
Make a scatter plot of df1 with A on the x axis and B on the y axis, colored by column C using the 'coolwarm' color map.
Code that runs before yours
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.read_csv('df1.csv', index_col=0, parse_dates=True)
df2 = pd.read_csv('df2.csv')Box, hexbin and density
df.plot.box() draws one box per column. hexbin is a scatter plot for lots of points: it counts the points in each hexagon and colors it by the count. kde draws a smooth curve of each column's distribution.
Exercises solved: 0 of 2
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.