-1

I recently started DataCamp's Statistical Thinking in Python (Part 1) course, and the instructor mentioned that when building plots in Python, the convention was often to assign plot objects to the NULL operator "_" while building the plot, prior to showing the plot.

Is this truly convention? I couldn't find much evidence for it, nor could I think of a justification as to why it would be convention. The code snippet below is taken from the course example and demonstrates what I mean.

# Plot all ECDFs on the same plot
_ = plt.plot(x_set, y_set, marker='.', linestyle='none')
_ = plt.plot(x_vers, y_vers, marker='.', linestyle='none')
_ = plt.plot(x_virg, y_virg, marker='.', linestyle='none')

# Annotate the plot
_ = plt.legend(('setosa', 'versicolor', 'virginica'), loc='lower right')
_ = plt.xlabel('petal length (cm)')
_ = plt.ylabel('ECDF')

# Display the plot
plt.show()
Paul
  • 21
  • 2

1 Answers1

0

As discussed in this answer, _ is often used to store ‘throwaway’ results, and that’s what’s going on here. The matplotlib calls are all returning results that would print to your terminal (or perhaps notebook), so to prevent that, those results are being captured in _ in order to prevent their display. Whether you choose to do this is up to you.

As was pointed out in the comments, if you're using IPython or Jupyter, you can also suppress output from a given line by adding a semicolon at the end of the line.

Michael
  • 51
  • 3
  • Maybe one should mention that nowadays it's pretty common to use IPython and suppressing the output in IPython can be done by appending a semi-colon `plt.plot([1,2,3]);`, which looks a little less strange. – ImportanceOfBeingErnest Nov 13 '18 at 12:26
  • Thanks for this and thanks @ImportanceOfBeingErnest. I think this makes sense. I was wondering if it would conflict ever with the fact that _ can also be used to call the last result output from the console. Thoughts on that at all? – Paul Nov 14 '18 at 03:08
  • Well, if you assign the last result to `_` you can still access it via `_` in the cell after that, so it's consitent with the default behaviour. – ImportanceOfBeingErnest Nov 14 '18 at 09:20