python - %matplotlib 返回无效语法错误

标签 python matplotlib jupyter

我在导入以下标记中显示的以下库时遇到一些问题,这是通过 Twitter 进行 Python 情绪分析所必需的:

# General:
import tweepy           # To consume Twitter's API
import pandas as pd     # To handle data
import numpy as np      # For number computing

# For plotting and visualization:
from IPython.display import display
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib

虽然对 Python3/pip 有点陌生,但作为 Rubyist/elephpant,我对自制程序当然并不陌生 - 在注释掉 %matplotlib 之后,该文件显然导入了除 之外列出的所有库%matplotlib,看起来是 the Jupyter invocation - 那么如何在标准 Python (3.6.2) 中正确加载 %matplotlib 的导入以避免以下错误?有替代方法吗?

 File "toolbox.py", line 10
    %matplotlib
    ^
SyntaxError: invalid syntax

谢谢!

最佳答案

作为documentation explains :

Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of IPython’s specific magic and use matplotlib.pyplot.ion()/matplotlib.pyplot.ioff() which have the advantages of working outside of IPython as well.

(强调我的)

所以在你的情况下你可以使用:

plt.ion()

而不是 %matplotlib

关于python - %matplotlib 返回无效语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46205948/

相关文章:

python - Pandas :如何计算过去几天但仅在给定时间的变量的滚动总和?

python - 为什么我的 pylab 动画每次更新都会变慢?

python - 来自 Pandas Dataframe 的条形图

python - 如何获取 Jupyter Notebook 的路径?

python - 如何解决 Matplotlib 类型错误 : only integer scalar arrays can be converted to a scalar index

python - 为python安装最新版本的aubio

python - 更改 tensorflow 对象检测中的优化器

python - 将 plot() 添加到 imshow() 时避免更改图形大小

python - 在 ssh 上使用 matplotlib.pyplot 时遇到问题

python - jupyter如何访问安装在正确路径中的新tensorflow模块?