Matplotlib: Difference between revisions

1,540 bytes removed ,  6 years ago
(24 intermediate revisions by 10 users not shown)
Very nice site!
== Project ==
Very nice site!
Learn how to plot data with the matplotlib plotting library. Ditch Excel forever!
== Goals ==
* practice reading data from a file
* practice using the matplotlib Python plotting library to analyze data and generate graphs
== Project setup ==
==== Mac OS X users only ====
If you do not already have a C compiler installed, you'll need one to install matplotlib. You have several options depending on your situation:
# Download and install Xcode (1.5 GB) from
# Download and install Command Line Tools for Xcode (175 MB) from This requires an Apple Developer account (free, but you have to sign up).
# Download and install kennethreitz's gcc installer (requires 10.6 or 10.7) from
Please wave over a staff member and we'll help you pick which option is best for you computer.
=== Install the project dependencies ===
Please follow the official matplotlib installation instructions at
The dependencies vary across operating systems. summarizes what you'll need for your operating system.
A universal dependency is the NumPy scientific computing library. NumPy has download and installation instructions at
Installing matplotlib and its dependencies is somewhat involved; please ask for help if you get stuck or don't know where to start!
=== Download and un-archive the Matplotlib project skeleton code ===
Un-archiving will produce a <code>Matplotlib</code> folder containing several Python and text files.
=== Test your setup ===
Run the <code></code> script in your <code>Matplotlib</code> directory. A window with a graph should pop up.
Very nice site!
== Project steps ==
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
and re-run the script. What changed?
y_values = [0, 2, 4, 8, 16, 32]
pyplot.plot(x_values, y_values, "o-")</pre>
and re-run the script. What changed?
Note how matplotlib automatically resizes the graph to fit all of the points in the figure for you.
Read about how to generate random integers on
Then, instead of hard-coding x values and y values in <code></code>, generate a list of random y values and plot them.
An example plot using random y values might look like this:
* What does matplotlib pick as the x values if you don't supply them yourself?
* What options would you pass to <code>pyplot.plot</code> to generate a plot with red triangles and dotted lines?
=== 2. Plotting the world population over time ===
<pre>pyplot.plot(my_data_1, "mo-", label="my data 1")
pyplot.plot(my_data_2, "bo-", "label="my data 2")</pre>
will plot <code>my_data_1</code> in magenta and <code>my_data_2</code> in blue on the same figure.
Supply labels for your plots, like above. Then use <code>pyplot.legend</code> to give your graph a legend. Just plain <code>pyplot.legend()</code> will work, but providing more options may give a better effect.
Your graph should look something like this:
Examples of legends:
## On line 44, we use <code></code> instead of our usual <code>pyplot.plot</code>. What are the 3 arguments passed to <code></code>?
# We've included a mystery text file <code>mystery.txt</code>: an excerpt from an actual novel. Alter <code></code> to process the data in <code>mystery.txt</code> instead of <code>constitution.txt</code>, and re-run the script. What do you notice that is odd about this file? You can read more about this odd novel [ here].
=== 2. Tour the matplotlib gallery ===
You've read, modified, and created scripts that plot and analyze data using matplotlib. Keep practicing!
Anonymous user