Skip to content

Commit aa7c316

Browse files
authored
Merge pull request #318 from astrofrog/vertical-lines
Improved exercise about why the plots don't show nice step functions
2 parents d396240 + 19745cc commit aa7c316

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

fig/01-numpy_exercise_0.png

37.1 KB
Loading

0 commit comments

Comments
 (0)