Anonymous user
Matplotlib: Difference between revisions
→Project setup
imported>Jesstess |
|||
(24 intermediate revisions by 10 users not shown) | |||
Line 1:
[[File:grid.png|right|300px]]
Very nice site!
Very nice site!
Very nice site!
== Project steps ==
Line 64 ⟶ 29:
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
and re-run the script. What changed?
</li>
<li>
Line 75 ⟶ 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 81 ⟶ 50:
Read about how to generate random integers on http://docs.python.org/library/random.html#random.randint.
Then, instead of hard-coding
An example plot using random y values might look like this:
Line 96 ⟶ 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 136 ⟶ 104:
<pre>pyplot.plot(my_data_1, "mo-", label="my data 1")
pyplot.plot(my_data_2, "bo-",
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 157 ⟶ 125:
</li>
<li>
Examples of legends: http://matplotlib.sourceforge.net/examples/pylab_examples/legend_auto.html
</li>
<li>
Line 180 ⟶ 148:
## 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>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 [http://en.wikipedia.org/wiki/Gadsby_(novel) here].
=== 2. Tour the matplotlib gallery ===
Line 189 ⟶ 158:
===Congratulations!===
You've read, modified, and created scripts that plot and analyze data using matplotlib. Keep practicing!
[[File:Fireworks.png|150px]]
|