Matplotlib: Difference between revisions

2,321 bytes removed ,  7 years ago
imported>Jesstess
 
(31 intermediate revisions by 10 users not shown)
Line 1:
[[File:grid.png|right|300px]]
 
Very nice site!
== Project ==
 
Very nice site!
Learn how to plot data with the matplotlib plotting library. Ditch Excel forever!
 
Very nice site!
== 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 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 ===
 
Please follow the official matplotlib installation instructions at http://matplotlib.sourceforge.net/users/installing.html
 
The dependencies vary across operating systems. http://matplotlib.sourceforge.net/users/installing.html#build-requirements 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 http://numpy.scipy.org/
 
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 Jeopardy database project skeleton code ===
 
* http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/JeopardyDatabase.zip
 
Un-archiving will produce a <code>JeopardyDatabase</code> folder containing 3 Python files and one SQL database dump.
 
=== Create a SQLite database from the database dump ===
 
Inside <code>JeopardyDatabase</code> is a file called <code>jeopardy.dump</code> which contains a SQL database dump. We need to turn that database dump into a SQLite database.
 
Once you have SQLite installed, you can create a database from jeopardy.dump with:
 
<pre>sqlite3 jeopardy.db < jeopardy.dump</pre>
 
This creates a sqlite3 database called <code>jeopardy.db</code>
 
=== Test your setup ===
 
At a command prompt, start <code>sqlite3</code> using the <code>jeopardy.db</code> database by running:
 
<pre>sqlite3 jeopardy.db</pre>
 
That should start a sqlite prompt that looks like this:
 
<pre>
SQLite version 3.6.12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite></pre>
 
At that sqlite prompt, type <code>.tables</code> and hit enter. That should display a list of the tables in this database:
 
<pre>sqlite> .tables
category clue
sqlite></pre>
 
From a command prompt, navigate to the <code>JeopardyDatabase</code> directory and run
 
<pre>python jeopardy_categories.py</pre>
 
You should see a list of 10 jeopardy categories printed to the screen. If you don't, let a staff member know so you can debug this together.
 
== Project steps ==
Line 96 ⟶ 29:
 
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
 
and re-run the script. What changed?
</li>
<li>
Add x-values to the data by changing
 
<codepre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</codepre>
 
to
Line 107 ⟶ 42:
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.
Line 113 ⟶ 50:
Read about how to generate random integers on http://docs.python.org/library/random.html#random.randint.
 
Then, instead of hard-coding x values and y values in <code>basic_plot.py</code>, generate a list of random y values and plot them.
 
An example plot using random y values might look like this:
Line 128 ⟶ 65:
* 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 ===
Line 168 ⟶ 104:
 
<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:
Line 189 ⟶ 125:
</li>
<li>
Examples of legends: http://matplotlib.sourceforge.net/examples/pylab_examples/legend_auto.html
</li>
<li>
Line 211 ⟶ 147:
## On line 41, what are the two arguments passed to <code>pyplot.xticks</code>
## On line 44, we use <code>pyplot.bar</code> instead of our usual <code>pyplot.plot</code>. What are the 3 arguments passed to <code>pyplot.bar</code>?
# We've included a mystery text file <code>mystery.txt</code>: an excerpt from an actual novel. Alter <code>constitution.py</code> to process the data in <code>mystery.txt</code> instead of <code>constituationconstitution.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 [http://en.wikipedia.org/wiki/Gadsby_(novel) here].
 
 
=== 2. Tour the matplotlib gallery ===
 
You can truly make any kind of graph with matplotlib. You can even create animated graphs. Check out some of the amazing possibilities, including their source code, at the matplotlib gallery: http://matplotlib.sourceforge.net/gallery.html.
 
[[File:matplotlib_gallery.png|750px]]
 
We've included a mystery text file <code>mystery.txt</code>: an excerpt from an actual novel. Alter <code>constitution.py</code> to process the data in <code>mystery.txt</code> instead of <code>constituation.txt</code>, and re-run the script. What do you notice that is odd about this file?
 
===Congratulations!===
 
You've learnedread, about SQLmodified, and makingcreated databasescripts that plot and queriesanalyze fromdata withinusing Pythonmatplotlib. Keep practicing!
 
[[File:Fireworks.png|150px]]
Anonymous user