9

I've seen this a couple of times:

# http://geopandas.org/aggregation_with_dissolve.html
continents.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd');

# https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.html
plt.legend('ABCDEF', ncol=2, loc='upper left');

# https://matplotlib.org/api/pyplot_api.html
x = np.arange(0, 5, 0.1);

It confuses me that it is done very often with matplotlib and also inconsistently.

Is this only an error? Was it in the matplotlib documentation for a while?

Does / did ; have any function at the and of a line in Python? (This suggests the answer is "no", but it does not explain why it is so wide-spread with matplotlib.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 1
    This probably comes from people coming from other programming languages. A trailing semicolon is legal in Python, but (obviously) not required: it seperates statements. – L3viathan Aug 01 '18 at 07:10
  • @L3viathan You noticed that I linked the question you claimed my question was a duplicate of in my question? – Martin Thoma Aug 01 '18 at 07:13
  • 1
    Yes I did! Still a duplicate; the first answer is pretty much spot-on. – L3viathan Aug 01 '18 at 07:14
  • 2
    In MATLAB it prevents the display of the result. – hpaulj Aug 01 '18 at 07:42
  • 1
    In IPython, adding a semicolon also prevents the return value to be printed (in the case of plotting, the resulting plot objects). I also sometimes use semicolons in interactive sessions to chain several statements into one line, which is not a recommended practice for actual code but it is practical when you have a few instructions you keep reusing. Other than that, as mentioned by others it could be just an habit from another language, but there is no functional difference. – jdehesa Aug 01 '18 at 08:52
  • In Python, `;` is used to conjugate statements or expressions. Alone, it has no effect. It may have special meaning in non-standard Python tools, such as ipython or others. If ipython is often used with mathplotlib, this may explain the correlation. – Burhan Khalid Aug 01 '18 at 08:54
  • Much more specific: *[Suppress output of object when plotting in IPython](https://stackoverflow.com/questions/14506583/suppress-output-of-object-when-plotting-in-ipython)* (2013) – Peter Mortensen Nov 22 '22 at 21:01

2 Answers2

11

As stated in the linked question's answer, the only purpose of a semicolon in pure Python is to separate statements in a single line.

However, here you observe semi colons at the end of lines, which, indeed, would not serve any purpose in pure Python.

There are mainly two reasons here:

  1. As commented by hpaulj, semicolons are used in MATLAB, and often people now working with Python, matplotlib and related packages are very much used to MATLAB and hence might accidentally type those in. This is the reason for the semicolon accidentally appearing in the third mentioned case (pyplot)

     x = np.arange(0, 5, 0.1);
    

It should be noted that this link refers to an older version in the matplotlib documentation, which in the current version has been corrected.

  1. More importantly, you will often see semicolons being used when code has been initially used in IPython or Jupyter Notebooks. In those cases, the semicolon is, just like in MATLAB, used to prevent output from a code line.

    Enter image description here

So it's not necessarily related to matplotlib, but in turn matplotlib is often used in one way or the other in notebooks, and notebooks are easily converted to HTML and hence you often find code on websites stemming from such notebooks. This would be the reason for the semicolons in the first and second example case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

From the post Silver BlogGold BlogData Visualization in Python: Matplotlib vs Seaborn:

A handy tip is that whenever matplotlib is executed, the output will always include a text output that can be very visually unappealing. To fix this, add a semicolon - ';' at the end of the last line of code when executing a code chunk to generate a figure.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alz
  • 71
  • 1
  • 4