我在导入以下标记中显示的以下库时遇到一些问题,这是通过 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
谢谢!
最佳答案
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/