Python中的 %matplotlib inline
在一些代码中经常会看到这样的:
其中第二句 %matplotlib inline 比较奇怪,而且无论是用哪个 python 的 IDE 如 spyder 或者 pycharm,这个地方都会报错,显示invalid syntax(无效语法)。
那为什么这些代码里面会有这一句呢?原来是这样的。
%matplotlib 作用
- 是在使用 jupyter notebook 或者 jupyter qtconsole 的时候,才会经常用到 %matplotlib,也就是说那份代码可能是别人使用jupyter notebook 或者 jupyter qtconsole 进行编辑的。
- 而 %matplotlib 具体作用是当你调用 matplotlib.pyplot 的绘图函数 plot() 进行绘图的时候,或者生成一个 figure 画布的时候,可以直接在你的python console里面生成图像。
而在 spyder 或者 pycharm 实际运行代码的时候,可以直接注释掉这一句,也是可以运行成功的。
如下示例,生成了一个带坐标轴的figure对象,并且画布颜色是白色的。
下面分别看一下 jupyter notebook 中“不加 %matplotlib inline ”和“加 %matplotlib inline ”的情况:
参考 https://www.jianshu.com/p/2dda5bb8ce7d