swcarpentry / python-novice-inflammation

Programming with Python
http://swcarpentry.github.io/python-novice-inflammation/
Other
301 stars 780 forks source link

03-matplotlib: rename axes1 -> ax1 #824

Open jdkent opened 4 years ago

jdkent commented 4 years ago

Motivated by @maxim-belkin's comment and the subplot documentation (look at the Returns: section), there appears to be low hanging fruit to help improve understanding and be closer to what is recommended in "real world" use.

for this section, instead of using axes1, axes2, etc., we could use ax1, ax2, etc. since each variable points to a single Axes instance.

Current code:

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()

Proposed Change:

import numpy
import matplotlib.pyplot

data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')

fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))

ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)

ax1.set_ylabel('average')
ax1.plot(numpy.mean(data, axis=0))

ax2.set_ylabel('max')
ax2.plot(numpy.max(data, axis=0))

ax3.set_ylabel('min')
ax3.plot(numpy.min(data, axis=0))

fig.tight_layout()

matplotlib.pyplot.show()

There are several locations where this would need to be changed as well. Let me know your thoughts!

ldko commented 4 years ago

Hi @jdkent , I see the point you are making about the convention in matplotlib to use ax or axs vs. axes to clarify if the variable holds a single axes or a collection of them. In the context of the example above, however, I don't think the change does much to clarify for new learners some of the points that @maxim-belkin brought up, such as why do we call it axes1 (or ax1) and not subplot1. I personally would not be opposed to the change you propose, since it is the convention and slightly less typing, however I don't think it alone would make a big difference for our learners who are at this point perhaps not thinking about axes being 1 or possibly more axes. Perhaps others would like to share their thoughts. Thank you for proposing this, James!