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 ===
=== 1. Look at the layout of the Jeopardy database ===
 
<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
Start sqlite with <code>sqlite3 jeopardy.db</code>. Then look at the tables in your database by running the following commands and the sqlite prompt:
 
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
* <tt>.table</tt>, which will list the tables in the database
</li>
* <tt>.schema category</tt>, which will show the organization of the <tt>category</tt> table, including the fields and the data types they store.
<li>
Add x-values to the data by changing
 
<code>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</code>
It should look like this:
 
to
<pre>sqlite> .schema category
CREATE TABLE "category" (
id INTEGER PRIMARY KEY,
name VARCHAR(255) NOT NULL,
game INTEGER NOT NULL,
boardPosition INTEGER
);</pre>
 
<pre>x_values = [3, 4, 7, 20, 22, 25]
This tells us that the <code>category</code> table has 4 fields: <code>id</code>, <code>name</code>, <code>game</code>, and <code>boardPosition</code>.
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.
* <tt>.schema clue</tt>
</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
* What is SQL? http://www.w3schools.com/sql/sql_intro.asp
* 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
* What is a database schema? http://wiki.answers.com/Q/What_is_a_database_schema
 
<b>Check your understanding</b>:
* What does matplotlib pick as the x values if you don't supply them yourself?
* What tables are in the database?
* What options would you pass to <code>pyplot.plot</code> to generate a plot with red triangles and dotted lines?
* What is a schema?
* What fields are in the <code>category</code> table?
* What fields are in the <code>clue</code> table?
 
 
Anonymous user