Module 7 · Charts with pandas · Lesson 38 of 66

pandas chart exercises

Recreate seven charts of df3 with df.plot, including a legend moved outside the chart.

In this lesson · about 16 minutes

  • Choose the df.plot method for a chart
  • Control size, color, bins, line style and legend position

These exercises use df3: 500 rows of random numbers in columns a, b, c and d.

Exercise

1. A wide scatter plot

Draw a scatter plot of b (y axis) against a (x axis) with red points of size 50, on a figure 12 inches wide and 3 inches tall.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

2. Histogram of a

Draw a histogram of column a with df.plot.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

3. Polish it with a style sheet

Switch to the 'ggplot' style sheet and redraw the histogram of a with 25 bins and half-transparent bars (alpha=0.5).

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

4. Compare a and b

Draw a box plot comparing columns a and b.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

5. Density of d

Draw a KDE (density) plot of column d.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

6. A thick dashed line

Redraw the density of d with a line width of 5 and a dashed line style. (You'd rarely dash a density curve, but it's good practice with line options.)

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

7. Area plot of the first rows

Draw an area plot of all four columns for rows 0 through 30, with alpha=0.4.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercise

Bonus: move the legend outside

In the last chart the legend covers part of the data. Draw it again and move the legend just outside the right edge, centered vertically.

Code that runs before yours
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.read_csv('df3.csv')

Exercises solved: 0 of 8

Tip: solve each exercise first. Press Check to test your code.

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.