2017-09-13 201 views
2

我遇到了一些麻煩,進口低於降價,這是通過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 /作爲一個Rubyist/elephpant,我肯定不是陌生人的自制軟件 - 在註釋掉%matplotlib之後,該文件顯然會導入除%matplotlib之外的所有庫,這似乎是the Jupyter invocation - 那麼如何正確加載標準中的%matplotlib導入Python(3.6.2)爲了避免下面的錯誤?有其他方法嗎?

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

謝謝!

+4

'%matplotlib'是一個[IPython magic](http://ipython.readthedocs.io/en/stable/interactive/magics.html?highlight=magic#magic-matplotlib)。這是交互式使用。 – wim

+0

Thanks @ wim - 你是否建議只評論'%matplotlib'或者那個和'將matplotlib.pyplot完全導入爲plt'? – alexanderjsingleton

回答

2

由於documentation explains

與IPython的5.0啓動和matplotlib 2.0,你可以儘量避免使用IPython中的特殊魔法和使用matplotlib.pyplot.ion()/matplotlib.pyplot.ioff()具有工作IPython的外部以及的優勢。

(重點煤礦)

所以你的情況,你可以只使用:

plt.ion() 

代替%matplotlib

+0

完美!感謝您指引我朝着正確的方向發展 - 我還沒有看到這些dox - 太專注於[matplotlib dox](http://matplotlib.org/)。再次感謝。 – alexanderjsingleton