为什么有一个分号;matplotlibs plot()函数后? [英] Why is there a semicolon ; after matplotlibs plot() function?

查看:94
本文介绍了为什么有一个分号;matplotlibs plot()函数后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过几次了:

# http://geopandas.org/aggregation_with_dissolve.html大洲.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd');# https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.htmlplt.legend('ABCDEF',ncol = 2,loc ='左上');# https://matplotlib.org/api/pyplot_api.htmlx = np.arange(0, 5, 0.1);

令我困惑的是,它经常使用 matplotlib 完成,而且还不一致.

这只是一个错误吗?是不是在 matplotlib 文档里有一段时间了?

/code ; 在Python的和行上是否有任何功能?(

因此它不一定与matplotlib有关,但是matplotlib经常在笔记本中以一种或另一种方式使用,并且笔记本很容易转换为html,因此您经常在源自此类笔记本的网站上找到代码.这就是在第一个和第二个示例案例中使用分号的原因.

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

解决方案

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 mentionned case (https://matplotlib.org/api/pyplot_api.html)

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

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

  2. 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.

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.

这篇关于为什么有一个分号;matplotlibs plot()函数后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆