Skip to content
Visual AnalyticsMatplotlib and Seaborn

Formulas for this chapter

Grouped bar offsets

position of series k = x + (k - (m + 1) / 2) * width, painted width = m * width

When several series share each category slot. Keep m x width below 1 so the groups stay separated, and restore the category names with plt.xticks.

x
Category centres, usually np.arange(number of categories)
m
Number of series (bars per group)
k
Which series, counting from 1
width
Bar width in x-axis units; Matplotlib's default is 0.8

Histogram bin count from np.arange

edges = np.arange(start, stop, step); bins = number of edges - 1 = ceil((stop - start) / step) - 1

Whenever a histogram question gives bin edges rather than a bin count. Remember np.arange excludes its stop value, so push stop past the last edge you want.

start
First bin edge, included
stop
Excluded upper limit
step
Bin width

Pie slice share and angle

share = value / total; percent printed by autopct = 100 * share; angle = 360 * share

For any pie-chart question, and for the argument against pie charts: convert two slices to degrees and show how close they are.

value
One category's size, as passed to plt.pie
total
Sum of every value in the list
autopct
Format string, e.g. '%1.1f%%', that prints the percentage
Step 2 of 26
The real wordsTheory

The five plotting libraries

The deck opens with a list. Learn it as a list, because a one-mark question can simply ask for it.

  • Matplotlib, the base library everything else sits on
  • Pandas Visualization, charts called straight off a data frame
  • Seaborn, built on Matplotlib, statistical charts with better defaults
  • ggplot, a port of R's grammar of graphics
  • plotly, interactive charts for the browser