matlab-qualgraphs-notes

MatPlotLib Qualitative Graphing Notes

So, last time was all of the line graphs. Now for all those other silly kinds of graphs.

The good news is most of the labeling and fiddling is the same across any kind of graph, but some labels are more necessary than others. Bar graphs without a labeled x-axis are particularly useless, as are pie charts without legends.

from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]
sales =  [91, 76, 56, 66, 52, 27]

plt.bar(range(len(drinks)), sales)
ax = plt.subplot(1, 1, 1)
ax.set_xticks(range(len(drinks)))
ax.set_xticklabels(drinks)

plt.show()
png

Bar Graphs with more than One Bar

Making a two-bar plot is annoying enough that Excel will sound nice. Still, if it's a large dataset, this is still nicer, it just takes a little extra code. Remember that by default bars are 2u apart, so split bars should be 0.8, etc. The following code has a nice way of breaking down the needed widths so Python can do the really annoying parts of the design.

Stacked bar graphs are a little easier, as seen in the second example.

png
png
png
png
png

Line Graphs with Neat Features

png

Pie Charts

Pie charts are terrible, because they give no historical context. But, people still like them, so here's how to make one.

Note, always use the plt.axis('equal') statement, as otherwise the pie chart will default to weirdly tilted. No one likes tilted pie charts.

png

Last updated

Was this helpful?