Main Content

Matplotlib - graphing in Python - teaching examples

Archive - Originally posted on "The Horse's Mouth" - 2010-10-10 07:12:06 - Graham Ellis

Matplotlib provides Python with a graph drawing and data representation tool that is extremely flexible - in fact so flexible that it's hard for the newcomer to know where to start.

The following examples are very straightforward, but useful, graphs showing real data sets (from the second example onwards) of accesses to our web server last week, plotted against hour of the day. You'll be able to use almost identical code for your first experiments with matplotlib!

The source code links each open in a separate window so that you can see the program and results at the same time

This is a first "hello matplotlib world" type example that shows fundamental graphing of a data set. [source]


In this second example, we have added some labelling and we're using a real data set. The line style and much of the rest remain at defaults. [source]


Plotting differet data sets on the same axes, using a variety of line styles to tell them apart. [source]


I've now added a second graph to the figure, so that you can see how different graphs can be combined. [source]


Finally, the two graphs have been adjusted so that they're left and right, and you'll now see some very significant differences in the weekend and weekday pattern. Truely, a picture paints a thousand words.[source]


Matplotlib is huge - you can do a lot with it, and it's a daunting task at first to find what you need; the above should help newcomers who already have a grounding in Python programming. There's an excellent reference manual once you get deeper in, and you may find the following useful too:

[link]Matplotlib HowTo FAQ
[link]Matplotlib Colours Interface
[link]PyPlot Tutorial