Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a semicolon ; after matplotlibs plot() function?

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

What confuses me is 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 does not explain why it is so wide-spread with matplotlib).

like image 528
Martin Thoma Avatar asked Aug 01 '18 07:08

Martin Thoma


People also ask

What does a semi colon mean in Python?

Statement Separators A semi-colon in Python denotes separation, rather than termination. It allows you to write multiple statements on the same line.

How does plot () work matplotlib?

If you provide a single list or array to the plot() command, matplotlib assumes it is a sequence of y values, and automatically generates the x values for you. Since python ranges start with 0, the default x vector has the same length as y but starts with 0. Hence the x data are [0,1,2,3] .

Is semicolon necessary in Python?

Python doesn't use Semicolons but it is not restricted. In Python, Semicolon is not used to denote the end of the line. Python is called the simple coding language because there is no need to use Semicolon and if we even forget to place, it doesn't throw an error.

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.


2 Answers

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.

like image 126
ImportanceOfBeingErnest Avatar answered Sep 28 '22 17:09

ImportanceOfBeingErnest


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.

like image 32
Alz Avatar answered Sep 28 '22 18:09

Alz