Matplotlib: Difference between revisions

4,646 bytes added ,  7 years ago
 
(4 intermediate revisions by 4 users not shown)
Line 1:
[[File:grid.png|right|300px]]
 
Howdy very nice web site!! Guy .. Excellent .. Superb .. I'll bookmark your web site and take the feeds additionallyI am glad to search out so many helpful info here in the put up, we want develop more techniques in this regard, thanks for sharing. eebdadceckddebdg
 
Very nice site!
Line 8 ⟶ 6:
 
Very nice site!
 
== Project steps ==
 
=== 1. 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>
 
and re-run the script. What changed?
</li>
<li>
Add x-values to the data by changing
 
<pre>pyplot.plot([0, 2, 4, 8, 16, 32], "o-")</pre>
 
to
 
<pre>x_values = [0, 4, 7, 20, 22, 25]
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.
</li>
<li>
Read about how to generate random integers on http://docs.python.org/library/random.html#random.randint.
 
Then, instead of hard-coding 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:
<br />
[[File:Basic_plot.png|300px]]
</li>
</ol>
 
<b>Read these short documents</b>:
* 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 and marker shapes and colors: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot
 
<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?
 
=== 2. Plotting the world population over time ===
 
<ol>
<li>
Run <code>python world_population.py</code>. This will pop up a window with a dot plot of the world population over the last 10,000 years.
</li>
<li>
Open <code>world_population.py</code>. Read through the code in this file.
 
In this example, we read our data from a file. Open the data file <code>world_population.txt</code> and examine the format of the file.
</li>
<li>
Find the documentation on http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot for customizing the linewidth of plots. Then change the world population plot to use a magenta, down-triangle marker and a linewidth of 2.
</li>
</ol>
 
<b>World population resources</b>:
<ul>
<li>
File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
</li>
<li>
Splitting sprints into parts based on a delimiter: http://www.hacksparrow.com/python-split-string-method-and-examples.html
</li>
</ul>
 
<b>Check your understanding</b>:
* In <code>world_population.py</code>, what does <code>file("world_population.txt", "r").readlines()</code> return?
* In <code>world_population.py</code>, what does <code>point.split()</code> return?
 
 
=== 3. Plotting life expectancy over time ===
 
In a new file, write code to plot the data in <code>life_expectancies_usa.txt</code>. The format in this file is <year>,<male life expectancy>,<female life expectancy>.
 
You can call <code>pyplot.plot</code> multiple times to draw multiple lines on the same figure. For example:
 
<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:
 
[[File:Life_expectancies.png|300px]]
 
To save your graph to a file instead of or in addition to displaying it, call <code>pyplot.savefig</code>.
 
<b>Life expectancy resources</b>:
<ul>
<li>
File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
</li>
<li>
Splitting sprints into parts based on a delimiter: http://www.hacksparrow.com/python-split-string-method-and-examples.html
</li>
<li>
Examples of legends: http://matplotlib.sourceforge.net/examples/pylab_examples/legend_auto.html
</li>
<li>
Ways to configure your legend: http://matplotlib.sourceforge.net/api/legend_api.html
</li>
<li>
Saving your graph to a file: http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.savefig
</li>
</ul>
 
==Bonus exercises==
Anonymous user