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 1 of 26
The ideaTheory

Four lines and a picture

Every chart the class drew in Python is the same four lines. Load the drawing tool, put your numbers in two lists, call one function, show it.

The only thing that changes between a bar chart and a pie chart is the name of that one function. Learn the four lines once, then learn six function names.

The exam is pen and paper, so nobody will run this. You will be asked to write the line and name the setting.