2

I'm experiencing some trouble importing the below libraries displayed within the below markdown, which is required for a Python sentiment analysis via Twitter:

# 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

Although somewhat new to Python3/pip, as a Rubyist/elephpant, I'm certainly no stranger to homebrew- after commenting-out %matplotlib the file apparently imports all libraries listed except%matplotlib, which appears to be the Jupyter invocation- so how do I correctly load the import for %matplotlib in standard Python (3.6.2) in order to avoid the below error? Is there an alternative approach?

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

Thank you!

2
  • 4
    %matplotlib is an IPython magic. That's for interactive use. Commented Sep 13, 2017 at 20:07
  • Thanks @wim- are you suggesting to just comment out %matplotlib or that and import matplotlib.pyplot as plt altogether? Commented Sep 13, 2017 at 20:15

1 Answer 1

4

As the 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.

(emphasis mine)

So in your case you could just use:

plt.ion()

instead of the bare %matplotlib.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks for pointing me in the right direction- I hadn't seen those dox yet- was too focused on matplotlib dox. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.