Unverified Commit 8803f91f authored by Maxim Belkin's avatar Maxim Belkin Committed by GitHub

Split the second episode (#771)

Split 02-numpy.md into 02-numpy.md and 03-matplotlib.md
parents ef704425 c005ce50
......@@ -29,16 +29,17 @@ This lesson is also available in [R][R] and [MATLAB][MATLAB].
| # | Episode | Time | Question(s) |
|--:|:---------|:----:|:------------|
| 1 | [Python Fundamentals][episode01] | 30 | What basic data types can I work with in Python?<br>How can I create a new variable in Python?<br>Can I change the value associated with a variable after I create it? |
| 2 | [Analyzing Patient Data][episode02] | 90 | How can I process tabular data files in Python? |
| 3 | [Repeating Actions with Loops][episode03] | 30 | How can I do the same operations on many different values? |
| 4 | [Storing Multiple Values in Lists][episode04] | 30 | How can I store many values together? |
| 5 | [Analyzing Data from Multiple Files][episode05] | 20 | How can I do the same operations on many different files? |
| 6 | [Making Choices][episode06] | 30 | How can my programs do different things based on data values? |
| 7 | [Creating Functions][episode07] | 30 | How can I define new functions?<br>What’s the difference between defining and calling a function?<br>What happens when I call a function? |
| 8 | [Errors and Exceptions][episode08] | 30 | How does Python report errors?<br>How can I handle errors in Python programs? |
| 9 | [Defensive Programming][episode09] | 30 | How can I make my programs more reliable? |
|10 | [Debugging][episode10] | 30 | How can I debug my program? |
|11 | [Command-Line Programs][episode11] | 30 | How can I write Python programs that will work like Unix command-line tools? |
| 2 | [Analyzing Patient Data][episode02] | 60 | How can I process tabular data files in Python? |
| 3 | [Visualizing Tabular Data][episode03] | 50 | How can I visualize tabular data in Python?<br>How can I group several plots together? |
| 4 | [Repeating Actions with Loops][episode04] | 30 | How can I do the same operations on many different values? |
| 5 | [Storing Multiple Values in Lists][episode05] | 30 | How can I store many values together? |
| 6 | [Analyzing Data from Multiple Files][episode06] | 20 | How can I do the same operations on many different files? |
| 7 | [Making Choices][episode07] | 30 | How can my programs do different things based on data values? |
| 8 | [Creating Functions][episode08] | 30 | How can I define new functions?<br>What’s the difference between defining and calling a function?<br>What happens when I call a function? |
| 9 | [Errors and Exceptions][episode09] | 30 | How does Python report errors?<br>How can I handle errors in Python programs? |
|10 | [Defensive Programming][episode10] | 30 | How can I make my programs more reliable? |
|11 | [Debugging][episode11] | 30 | How can I debug my program? |
|12 | [Command-Line Programs][episode12] | 30 | How can I write Python programs that will work like Unix command-line tools? |
## Contributing
......@@ -106,15 +107,16 @@ government. More information can be found [here][cp-about].
[travis_url]: https://travis-ci.org/swcarpentry/python-novice-inflammation
[episode01]: https://swcarpentry.github.io/python-novice-inflammation/01-intro/index.html
[episode02]: https://swcarpentry.github.io/python-novice-inflammation/02-numpy/index.html
[episode03]: https://swcarpentry.github.io/python-novice-inflammation/03-loop/index.html
[episode04]: https://swcarpentry.github.io/python-novice-inflammation/04-lists/index.html
[episode05]: https://swcarpentry.github.io/python-novice-inflammation/05-files/index.html
[episode06]: https://swcarpentry.github.io/python-novice-inflammation/06-cond/index.html
[episode07]: https://swcarpentry.github.io/python-novice-inflammation/07-func/index.html
[episode08]: https://swcarpentry.github.io/python-novice-inflammation/08-errors/index.html
[episode09]: https://swcarpentry.github.io/python-novice-inflammation/09-defensive/index.html
[episode10]: https://swcarpentry.github.io/python-novice-inflammation/10-debugging/index.html
[episode11]: https://swcarpentry.github.io/python-novice-inflammation/11-cmdline/index.html
[episode03]: https://swcarpentry.github.io/python-novice-inflammation/03-matplotlib/index.html
[episode04]: https://swcarpentry.github.io/python-novice-inflammation/04-loop/index.html
[episode05]: https://swcarpentry.github.io/python-novice-inflammation/05-lists/index.html
[episode06]: https://swcarpentry.github.io/python-novice-inflammation/06-files/index.html
[episode07]: https://swcarpentry.github.io/python-novice-inflammation/07-cond/index.html
[episode08]: https://swcarpentry.github.io/python-novice-inflammation/08-func/index.html
[episode09]: https://swcarpentry.github.io/python-novice-inflammation/09-errors/index.html
[episode10]: https://swcarpentry.github.io/python-novice-inflammation/10-defensive/index.html
[episode11]: https://swcarpentry.github.io/python-novice-inflammation/11-debugging/index.html
[episode12]: https://swcarpentry.github.io/python-novice-inflammation/12-cmdline/index.html
[community-initiatives]: https://communityin.org
[cp-about]: https://carpentries.org/about
[swc-about]: https://software-carpentry.org/about/
......
---
title: Analyzing Patient Data
teaching: 60
exercises: 30
teaching: 40
exercises: 20
questions:
- "How can I process tabular data files in Python?"
objectives:
......@@ -10,7 +10,6 @@ objectives:
- "Read tabular data from a file into a program."
- "Select individual values and subsections from data."
- "Perform operations on arrays of data."
- "Plot simple graphs from data."
keypoints:
- "Import a library into a program using `import libraryname`."
- "Use the `numpy` library to work with arrays in Python."
......@@ -18,11 +17,9 @@ keypoints:
- "Use `array[x, y]` to select a single element from a 2D array."
- "Array indices start at 0, not 1."
- "Use `low:high` to specify a `slice` that includes the indices from `low` to `high-1`."
- "All the indexing and slicing that works on arrays also works on strings."
- "Use `# some kind of explanation` to add comments to programs."
- "Use `numpy.mean(array)`, `numpy.max(array)`, and `numpy.min(array)` to calculate simple statistics."
- "Use `numpy.mean(array, axis=0)` or `numpy.mean(array, axis=1)` to calculate statistics across the specified axis."
- "Use the `pyplot` library from `matplotlib` for creating simple visualizations."
---
Words are useful, but what's more useful are the sentences and stories we build with them.
......@@ -496,112 +493,6 @@ print(numpy.mean(data, axis=1))
which is the average inflammation per patient across all days.
## Visualizing data
The mathematician Richard Hamming once said, "The purpose of computing is insight, not numbers," and
the best way to develop insight is often to visualize data. Visualization deserves an entire
lecture of its own, but we can explore a few features of Python's `matplotlib` library here. While
there is no official plotting library, `matplotlib` is the _de facto_ standard. First, we will
import the `pyplot` module from `matplotlib` and use two of its functions to create and display a
heat map of our data:
~~~
import matplotlib.pyplot
image = matplotlib.pyplot.imshow(data)
matplotlib.pyplot.show()
~~~
{: .language-python}
![Heatmap of the Data](../fig/inflammation-01-imshow.svg)
Blue pixels in this heat map represent low values, while yellow pixels represent high values. As we
can see, inflammation rises and falls over a 40-day period. Let's take a look at the average inflammation over time:
~~~
ave_inflammation = numpy.mean(data, axis=0)
ave_plot = matplotlib.pyplot.plot(ave_inflammation)
matplotlib.pyplot.show()
~~~
{: .language-python}
![Average Inflammation Over Time](../fig/inflammation-01-average.svg)
Here, we have put the average per day across all patients in the variable `ave_inflammation`, then
asked `matplotlib.pyplot` to create and display a line graph of those values. The result is a
roughly linear rise and fall, which is suspicious: we might instead expect a sharper rise and slower
fall. Let's have a look at two other statistics:
~~~
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}
![Maximum Value Along The First Axis](../fig/inflammation-01-maximum.svg)
~~~
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}
![Minimum Value Along The First Axis](../fig/inflammation-01-minimum.svg)
The maximum value rises and falls smoothly, while the minimum seems to be a step function. Neither
trend seems particularly likely, so either there's a mistake in our calculations or something is
wrong with our data. This insight would have been difficult to reach by examining the numbers
themselves without visualization tools.
### Grouping plots
You can group similar plots in a single figure using subplots.
This script below uses a number of new commands. The function `matplotlib.pyplot.figure()`
creates a space into which we will place all of our plots. The parameter `figsize`
tells Python how big to make this space. Each subplot is placed into the figure using
its `add_subplot` [method]({{ page.root }}/reference/#method). The `add_subplot` method takes 3
parameters. The first denotes how many total rows of subplots there are, the second parameter
refers to the total number of subplot columns, and the final parameter denotes which subplot
your variable is referencing (left-to-right, top-to-bottom). Each subplot is stored in a
different variable (`axes1`, `axes2`, `axes3`). Once a subplot is created, the axes can
be titled using the `set_xlabel()` command (or `set_ylabel()`).
Here are our three plots side by side:
~~~
import numpy
import matplotlib.pyplot
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)
axes1.set_ylabel('average')
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
~~~
{: .language-python}
![The Previous Plots as Subplots](../fig/inflammation-01-group-plot.svg)
The [call]({{ page.root }}/reference/#function-call) to `loadtxt` reads our data,
and the rest of the program tells the plotting library
how large we want the figure to be,
that we're creating three subplots,
what to draw for each one,
and that we want a tight layout.
(If we leave out that call to `fig.tight_layout()`,
the graphs will actually be squeezed together more closely.)
> ## Slicing Strings
>
......@@ -671,140 +562,6 @@ the graphs will actually be squeezed together more closely.)
> {: .solution}
{: .challenge}
> ## Plot Scaling
>
> Why do all of our plots stop just short of the upper end of our graph?
>
> > ## Solution
> > Because matplotlib normally sets x and y axes limits to the min and max of our data
> > (depending on data range)
> {: .solution}
>
> If we want to change this, we can use the `set_ylim(min, max)` method of each 'axes',
> for example:
>
> ~~~
> axes3.set_ylim(0,6)
> ~~~
> {: .language-python}
>
> Update your plotting code to automatically set a more appropriate scale.
> (Hint: you can make use of the `max` and `min` methods to help.)
>
> > ## Solution
> > ~~~
> > # One method
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> > axes3.set_ylim(0,6)
> > ~~~
> > {: .language-python}
> {: .solution}
>
> > ## Solution
> > ~~~
> > # A more automated approach
> > min_data = numpy.min(data, axis=0)
> > axes3.set_ylabel('min')
> > axes3.plot(min_data)
> > axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Drawing Straight Lines
>
> In the center and right subplots above, we expect all lines to look like step functions because
> non-integer value are not realistic for the minimum and maximum values. However, you can see
> that the lines are not always vertical or horizontal, and in particular the step function
> in the subplot on the right looks slanted. Why is this?
>
> > ## Solution
> > Because matplotlib interpolates (draws a straight line) between the points.
> > One way to do avoid this is to use the Matplotlib `drawstyle` option:
> >
> > ~~~
> > import numpy
> > import matplotlib.pyplot
> >
> > data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
> >
> > fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
> >
> > axes1 = fig.add_subplot(1, 3, 1)
> > axes2 = fig.add_subplot(1, 3, 2)
> > axes3 = fig.add_subplot(1, 3, 3)
> >
> > axes1.set_ylabel('average')
> > axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
> >
> > fig.tight_layout()
> >
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> ![Plot with step lines](../fig/inflammation-01-line-styles.svg)
> {: .solution}
{: .challenge}
> ## Make Your Own Plot
>
> Create a plot showing the standard deviation (`numpy.std`)
> of the inflammation data for each day across all patients.
>
> > ## Solution
> > ~~~
> > std_plot = matplotlib.pyplot.plot(numpy.std(data, axis=0))
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Moving Plots Around
>
> Modify the program to display the three plots on top of one another
> instead of side by side.
>
> > ## Solution
> > ~~~
> > import numpy
> > import matplotlib.pyplot
> >
> > data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
> >
> > # change figsize (swap width and height)
> > fig = matplotlib.pyplot.figure(figsize=(3.0, 10.0))
> >
> > # change add_subplot (swap first two parameters)
> > axes1 = fig.add_subplot(3, 1, 1)
> > axes2 = fig.add_subplot(3, 1, 2)
> > axes3 = fig.add_subplot(3, 1, 3)
> >
> > axes1.set_ylabel('average')
> > axes1.plot(numpy.mean(data, axis=0))
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0))
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> >
> > fig.tight_layout()
> >
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Stacking Arrays
>
> Arrays can be concatenated and stacked on top of one another,
......
---
title: Visualizing Tabular Data
teaching: 30
exercises: 20
questions:
- "How can I visualize tabular data in Python?"
- "How can I group several plots together?"
objectives:
- "Plot simple graphs from data."
- "Group several graphs in a single figure."
keypoints:
- "Use the `pyplot` module from the `matplotlib` library for creating simple visualizations."
---
## Visualizing data
The mathematician Richard Hamming once said, "The purpose of computing is insight, not numbers," and
the best way to develop insight is often to visualize data. Visualization deserves an entire
lecture of its own, but we can explore a few features of Python's `matplotlib` library here. While
there is no official plotting library, `matplotlib` is the _de facto_ standard. First, we will
import the `pyplot` module from `matplotlib` and use two of its functions to create and display a
heat map of our data:
~~~
import matplotlib.pyplot
image = matplotlib.pyplot.imshow(data)
matplotlib.pyplot.show()
~~~
{: .language-python}
![Heatmap of the Data](../fig/inflammation-01-imshow.svg)
Blue pixels in this heat map represent low values, while yellow pixels represent high values. As we
can see, inflammation rises and falls over a 40-day period. Let's take a look at the average inflammation over time:
~~~
ave_inflammation = numpy.mean(data, axis=0)
ave_plot = matplotlib.pyplot.plot(ave_inflammation)
matplotlib.pyplot.show()
~~~
{: .language-python}
![Average Inflammation Over Time](../fig/inflammation-01-average.svg)
Here, we have put the average per day across all patients in the variable `ave_inflammation`, then
asked `matplotlib.pyplot` to create and display a line graph of those values. The result is a
roughly linear rise and fall, which is suspicious: we might instead expect a sharper rise and slower
fall. Let's have a look at two other statistics:
~~~
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}
![Maximum Value Along The First Axis](../fig/inflammation-01-maximum.svg)
~~~
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}
![Minimum Value Along The First Axis](../fig/inflammation-01-minimum.svg)
The maximum value rises and falls smoothly, while the minimum seems to be a step function. Neither
trend seems particularly likely, so either there's a mistake in our calculations or something is
wrong with our data. This insight would have been difficult to reach by examining the numbers
themselves without visualization tools.
### Grouping plots
You can group similar plots in a single figure using subplots.
This script below uses a number of new commands. The function `matplotlib.pyplot.figure()`
creates a space into which we will place all of our plots. The parameter `figsize`
tells Python how big to make this space. Each subplot is placed into the figure using
its `add_subplot` [method]({{ page.root }}/reference/#method). The `add_subplot` method takes 3
parameters. The first denotes how many total rows of subplots there are, the second parameter
refers to the total number of subplot columns, and the final parameter denotes which subplot
your variable is referencing (left-to-right, top-to-bottom). Each subplot is stored in a
different variable (`axes1`, `axes2`, `axes3`). Once a subplot is created, the axes can
be titled using the `set_xlabel()` command (or `set_ylabel()`).
Here are our three plots side by side:
~~~
import numpy
import matplotlib.pyplot
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)
axes1.set_ylabel('average')
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
~~~
{: .language-python}
![The Previous Plots as Subplots](../fig/inflammation-01-group-plot.svg)
The [call]({{ page.root }}/reference/#function-call) to `loadtxt` reads our data,
and the rest of the program tells the plotting library
how large we want the figure to be,
that we're creating three subplots,
what to draw for each one,
and that we want a tight layout.
(If we leave out that call to `fig.tight_layout()`,
the graphs will actually be squeezed together more closely.)
> ## Plot Scaling
>
> Why do all of our plots stop just short of the upper end of our graph?
>
> > ## Solution
> > Because matplotlib normally sets x and y axes limits to the min and max of our data
> > (depending on data range)
> {: .solution}
>
> If we want to change this, we can use the `set_ylim(min, max)` method of each 'axes',
> for example:
>
> ~~~
> axes3.set_ylim(0,6)
> ~~~
> {: .language-python}
>
> Update your plotting code to automatically set a more appropriate scale.
> (Hint: you can make use of the `max` and `min` methods to help.)
>
> > ## Solution
> > ~~~
> > # One method
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> > axes3.set_ylim(0,6)
> > ~~~
> > {: .language-python}
> {: .solution}
>
> > ## Solution
> > ~~~
> > # A more automated approach
> > min_data = numpy.min(data, axis=0)
> > axes3.set_ylabel('min')
> > axes3.plot(min_data)
> > axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Drawing Straight Lines
>
> In the center and right subplots above, we expect all lines to look like step functions because
> non-integer value are not realistic for the minimum and maximum values. However, you can see
> that the lines are not always vertical or horizontal, and in particular the step function
> in the subplot on the right looks slanted. Why is this?
>
> > ## Solution
> > Because matplotlib interpolates (draws a straight line) between the points.
> > One way to do avoid this is to use the Matplotlib `drawstyle` option:
> >
> > ~~~
> > import numpy
> > import matplotlib.pyplot
> >
> > data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
> >
> > fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
> >
> > axes1 = fig.add_subplot(1, 3, 1)
> > axes2 = fig.add_subplot(1, 3, 2)
> > axes3 = fig.add_subplot(1, 3, 3)
> >
> > axes1.set_ylabel('average')
> > axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
> >
> > fig.tight_layout()
> >
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> ![Plot with step lines](../fig/inflammation-01-line-styles.svg)
> {: .solution}
{: .challenge}
> ## Make Your Own Plot
>
> Create a plot showing the standard deviation (`numpy.std`)
> of the inflammation data for each day across all patients.
>
> > ## Solution
> > ~~~
> > std_plot = matplotlib.pyplot.plot(numpy.std(data, axis=0))
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
> ## Moving Plots Around
>
> Modify the program to display the three plots on top of one another
> instead of side by side.
>
> > ## Solution
> > ~~~
> > import numpy
> > import matplotlib.pyplot
> >
> > data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
> >
> > # change figsize (swap width and height)
> > fig = matplotlib.pyplot.figure(figsize=(3.0, 10.0))
> >
> > # change add_subplot (swap first two parameters)
> > axes1 = fig.add_subplot(3, 1, 1)
> > axes2 = fig.add_subplot(3, 1, 2)
> > axes3 = fig.add_subplot(3, 1, 3)
> >
> > axes1.set_ylabel('average')
> > axes1.plot(numpy.mean(data, axis=0))
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0))
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> >
> > fig.tight_layout()
> >
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> {: .solution}
{: .challenge}
{% include links.md %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment