Skip to content

Commit 19745cc

Browse files
committed
Improved exercise about why the plots don't show nice step functions
1 parent 06de7a4 commit 19745cc

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

_episodes/01-numpy.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,42 @@ the graphs will actually be squeezed together more closely.)
952952
953953
> ## Drawing Straight Lines
954954
>
955-
> Why are the vertical lines in our plot of the minimum inflammation per day
956-
> not perfectly vertical?
955+
> In the center and right subplots above, we expect all lines to look like step functions, because
956+
> non-integer value are not realistic for the minimum and maximum values. However, you can see
957+
> that the lines are not always vertical or horizontal, and in particular the step function
958+
> in the subplot on the right looks slanted. Why is this?
957959
>
958960
> > ## Solution
959-
> > Because matplotlib interpolates (draws a straight line) between the points
961+
> > Because matplotlib interpolates (draws a straight line) between the points.
962+
> > One way to do avoid this is to use the Matplotlib `drawstyle` option:
963+
> >
964+
> > ~~~
965+
> > import numpy
966+
> > import matplotlib.pyplot
967+
> >
968+
> > data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
969+
> >
970+
> > fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
971+
> >
972+
> > axes1 = fig.add_subplot(1, 3, 1)
973+
> > axes2 = fig.add_subplot(1, 3, 2)
974+
> > axes3 = fig.add_subplot(1, 3, 3)
975+
> >
976+
> > axes1.set_ylabel('average')
977+
> > axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
978+
> >
979+
> > axes2.set_ylabel('max')
980+
> > axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
981+
> >
982+
> > axes3.set_ylabel('min')
983+
> > axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
984+
> >
985+
> > fig.tight_layout()
986+
> >
987+
> > matplotlib.pyplot.show()
988+
> > ~~~
989+
> > {: .python}
990+
> ![Plot with step lines](../fig/01-numpy_exercise_0.png)
960991
> {: .solution}
961992
{: .challenge}
962993

fig/01-numpy_exercise_0.png

37.1 KB
Loading

0 commit comments

Comments
 (0)