python SyntaxError: 无效的语法 %matplotlib inline

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39449549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:17:05  来源:igfitidea点击:

python SyntaxError: invalid syntax %matplotlib inline

pythonmatplotlib

提问by Prajakta Dumbre

I got this error in my python script:

我在我的 python 脚本中遇到了这个错误:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

from utils import progress_bar_downloader
import os
#Hosting files on my dropbox since downloading from google code is painful
#Original project hosting is here: https://code.google.com/p/hmm-speech-recognition/downloads/list
#Audio is included in the zip file
link = 'https://dl.dropboxusercontent.com/u/15378192/audio.tar.gz'
dlname = 'audio.tar.gz'

if not os.path.exists('./%s' % dlname):
    progress_bar_downloader(link, dlname)
    os.system('tar xzf %s' % dlname)
else:
    print('%s already downloaded!' % dlname)

I want use matplotlib but it gives syntax error, I tried sudo apt-get install python-matplotlib

我想使用 matplotlib 但它给出了语法错误,我试过 sudo apt-get install python-matplotlib

回答by Sandipan Dey

if you are not using Jupyter IPython notebook, just comment out (or delete) the line, everything will work fine and a separate plot window will be opened if you are running your python script from the console.

如果您没有使用 Jupyter IPython notebook,只需注释(或删除)该行,一切都会正常工作,并且如果您从控制台运行 Python 脚本,则会打开一个单独的绘图窗口。

However, if you are using Jupyter IPython notebook, the very first python code cell in your notebook should have the line "%matplotlib inline" for you to be able to view any plot.

但是,如果您使用的是 Jupyter IPython notebook,则 notebook 中的第一个 python 代码单元应该有一行“%matplotlib inline”,以便您能够查看任何绘图。

回答by MMN

"%matplotlib inline" isn't valid python code, so you can't put it in a script.

"%matplotlib inline" 不是有效的 python 代码,所以你不能把它放在脚本中。

I assume you're using a Jupyter notebook? If so, put it in the first cell and all should work.

我假设您使用的是 Jupyter 笔记本?如果是这样,把它放在第一个单元格中,一切都应该工作。

回答by Magdrop

"%matplotlib inline" is a magic command that works best with Jupyter IPython notebook. This command makes the image automatically shows inline inside the browser when using Jupyter notebook without having to call the show(). IPython is the core that supports these magic commands, but in this case, using IPython from console alone is not enough since this particular call tries to display graphics inline. Not sure if it works with any other combo, but to start, use Jupyter notebook.

"%matplotlib inline" 是一个神奇的命令,最适合 Jupyter IPython notebook。此命令使图像在使用 Jupyter notebook 时自动显示在浏览器中,而无需调用 show()。IPython 是支持这些魔法命令的核心,但在这种情况下,仅从控制台使用 IPython 是不够的,因为此特定调用试图内联显示图形。不确定它是否适用于任何其他组合,但首先,请使用 Jupyter notebook。

You can only use this code inside the cell. Press Shift+Enter to execute it.

您只能在单元格内使用此代码。按 Shift+Enter 执行它。

In []: %matplotlib inline

Since this is not a valid python code, if we include it inside a python script it will return with a syntax error (even when the script is executed from Jupyter notebook using import or other mechanism).

由于这不是有效的 python 代码,如果我们将它包含在 python 脚本中,它将返回语法错误(即使脚本是使用导入或其他机制从 Jupyter notebook 执行的)。

As any other shortcuts, if don't want to use jupyter notebook, you can remove "%matplotlib inline" from your python script and add show() at the end to display your plots.

与任何其他快捷方式一样,如果不想使用 jupyter notebook,您可以从 python 脚本中删除“%matplotlib inline”并在末尾添加 show() 以显示您的绘图。

回答by BillD

I had the same syntax error when using %matplotlibinline in Spyder.
After I replace it with the following lines of code, the Series, new_obj, that I wanted to plot successfully displayed on the console:

%matplotlib在 Spyder 中使用内联时,我遇到了相同的语法错误。
在我用以下代码行替换它之后new_obj,我想成功绘制的 Series,显示在控制台上:

    import matplotlib.pyplot as plt
    new_obj.resample('M').sum().plot(kind="bar")
    plt.show()