Skip to content

Commit

Permalink
Update figures in NumPy episode. Script to optimize SVG files (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-belkin authored Sep 20, 2019
1 parent 5db005a commit fd30329
Show file tree
Hide file tree
Showing 15 changed files with 803 additions and 10 deletions.
20 changes: 10 additions & 10 deletions episodes/01-numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ that can be called upon when needed.

## Loading data into Python
In order to load our inflammation data, we need to access
([import]({{ page.root }}/reference/#import) in Python terminology) a library called
[NumPy](http://docs.scipy.org/doc/numpy/ "NumPy Documentation") which stands for Numerical Python.
In general you should use this library if you want to do fancy things with numbers,
([import]({{ page.root }}/reference/#import) in Python terminology) a library called
[NumPy](http://docs.scipy.org/doc/numpy/ "NumPy Documentation") which stands for Numerical Python.
In general you should use this library if you want to do fancy things with numbers,
especially if you have matrices or arrays. We can import NumPy using:

~~~
Expand Down Expand Up @@ -270,7 +270,7 @@ when there's nothing interesting after the decimal point.
> However, shortcuts such as `import numpy as np` are frequently used. Importing NumPy this way means that after the
> inital import, rather than writing `numpy.loadtxt(...)`, you can now write `np.loadtxt(...)`. Some
> people prefer this as it is quicker to type and results in shorter lines of code - especially for libraries
> with long names! You will frequently see Python code online using a NumPy function with `np`, and it's
> with long names! You will frequently see Python code online using a NumPy function with `np`, and it's
> because they've used this shortcut. It makes no difference which approach you choose to take, but you must be
> consistent as if you use `import numpy as np` then `numpy.loadtxt(...)` will not work, and you must use `np.loadtxt(...)`
> instead. Because of this, when working with other people it is important you agree on how libraries are imported.
Expand Down Expand Up @@ -737,7 +737,7 @@ matplotlib.pyplot.show()
~~~
{: .language-python}

![Heatmap of the Data](../fig/01-numpy_71_0.png)
![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.
Expand Down Expand Up @@ -768,7 +768,7 @@ matplotlib.pyplot.show()
~~~
{: .language-python}

![Average Inflammation Over Time](../fig/01-numpy_73_0.png)
![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
Expand All @@ -781,15 +781,15 @@ matplotlib.pyplot.show()
~~~
{: .language-python}

![Maximum Value Along The First Axis](../fig/01-numpy_75_1.png)
![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/01-numpy_75_3.png)
![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
Expand Down Expand Up @@ -836,7 +836,7 @@ matplotlib.pyplot.show()
~~~
{: .language-python}

![The Previous Plots as Subplots](../fig/01-numpy_80_0.png)
![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
Expand Down Expand Up @@ -1035,7 +1035,7 @@ the graphs will actually be squeezed together more closely.)
> > matplotlib.pyplot.show()
> > ~~~
> > {: .language-python}
> ![Plot with step lines](../fig/01-numpy_exercise_0.png)
> ![Plot with step lines](../fig/inflammation-01-line-styles.svg)
> {: .solution}
{: .challenge}

Expand Down
Binary file removed fig/01-numpy_71_0.png
Binary file not shown.
Binary file removed fig/01-numpy_73_0.png
Binary file not shown.
Binary file removed fig/01-numpy_75_1.png
Binary file not shown.
Binary file removed fig/01-numpy_75_3.png
Binary file not shown.
Binary file removed fig/01-numpy_80_0.png
Binary file not shown.
Binary file removed fig/01-numpy_exercise_0.png
Binary file not shown.
79 changes: 79 additions & 0 deletions fig/generate_figures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""
Generate figures used in the lesson episodes.
Usage: ./generate_figures.py
"""

try:
import numpy
import matplotlib.pyplot
except ImportError:
print("Failed to load NumPy and/or Matplotlib", file=sys.stderr)
exit(1)

# Configure Matplotlib to not convert text to outlines
# All settings: matplotlib.rcParams or matplotlib.pyplot.rcParams
matplotlib.pyplot.rcParams['svg.fonttype'] = 'none'

# Load data
data = numpy.loadtxt(fname="../data/inflammation-01.csv", delimiter=",")

# Episode 1
## Visualizing data

matplotlib.pyplot.imshow(data)
matplotlib.pyplot.savefig("inflammation-01-imshow.svg")
matplotlib.pyplot.close()

matplotlib.pyplot.plot(numpy.mean(data, axis=0))
matplotlib.pyplot.savefig("inflammation-01-average.svg")
matplotlib.pyplot.close()

matplotlib.pyplot.plot(numpy.max(data, axis=0))
matplotlib.pyplot.savefig("inflammation-01-maximum.svg")
matplotlib.pyplot.close()

matplotlib.pyplot.plot(numpy.min(data, axis=0))
matplotlib.pyplot.savefig("inflammation-01-minimum.svg")
matplotlib.pyplot.close()

## Grouping plots
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.savefig("inflammation-01-group-plot.svg")
matplotlib.pyplot.close(fig)


## Exercise: Drawing Straight Lines
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.savefig("inflammation-01-line-styles.svg")
matplotlib.pyplot.close(fig)
55 changes: 55 additions & 0 deletions fig/inflammation-01-average.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions fig/inflammation-01-group-plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fd30329

Please sign in to comment.