Anonymous user
Matplotlib: Difference between revisions
no edit summary
imported>Jesstess (Created page with "right|300px == Project == Learn how to plot data with the matplotlib plotting library. Ditch Excel forever! == Goals == * practice reading data from a fi...") |
imported>Jesstess No edit summary |
||
Line 11:
== 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 https://developer.apple.com/xcode/▼
# Download and install Command Line Tools for Xcode (175 MB) from https://developer.apple.com/downloads/index.action. 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 https://github.com/kennethreitz/osx-gcc-installer/▼
Please wave over a staff member and we'll help you pick which option is best for you computer.▼
=== Install the project dependencies ===
Line 21 ⟶ 30:
Installing matplotlib and its dependencies is somewhat involved; please ask for help if you get stuck or don't know where to start!
▲==== 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 https://developer.apple.com/xcode/
▲# Download and install Command Line Tools for Xcode (175 MB) from https://developer.apple.com/downloads/index.action. 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 https://github.com/kennethreitz/osx-gcc-installer/
▲Please wave over a staff member and we'll help you pick which option is best for you computer.
Line 76:
== Project steps ==
=== Create a basic plot ===
<ol>
<li>
Run <code>python basic_plot.py</code>. This will pop up a window with a dot plot of some data.
</li>
<li>
Open <code>basic_plot.py</code>. Read through the code in this file. The meat of the file is in one line:
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o")</pre>
In this example, the first argument to <code>pyplot.plot</code> is the list of y values, and the second argument describes how to plot the data. If two lists had been supplied, <code>pyplot.plot</code> would consider the first list to be the x values and the second list to be the y values.
</li>
<li>Change the plot to display lines between the data points by changing
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o")</pre>
to
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
</li>
<li>
Add x-values to the data by changing
<code>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</code>
to
<pre>x_values = [3, 4, 7, 20, 22, 25]
y_values = [0, 2, 4, 8, 16, 32]
pyplot.plot(x_values, y_values, "o-")</pre>
Note how matplotlib automatically resizes the graph to fit all of the points in the figure for you.
</li>
</ol>
Read these short documents:
* Pyplot tutorial (just this one section; stop before the next section "Controlling line properties"): http://matplotlib.sourceforge.net/users/pyplot_tutorial.html#pyplot-tutorial
* List of line options, including line style, shapes and colors: http://www.thetechrepo.com/main-articles/469-how-to-change-line-properties-in-matplotlib-python
<b>Check your understanding</b>:
* 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?
|