Why does %matplotlib inline in Repl.it give me an error?

Question:

I’m trying to create a word cloud using Repl.it by following this towards data science article. However, I’m getting an error with %matplotlib inline (underlined in red). How do I fix this?

# Import packages
import matplotlib.pyplot as plt
%matplotlib inline

# Define a function to plot word cloud
def plot_cloud(wordcloud):
    # Set figure size
    plt.figure(figsize=(40, 30))
    # Display image
    plt.imshow(wordcloud) 
    # No axis details
    plt.axis("off");

Asked By: Daisy

||

Answers:

NoNameByProgram from repl.it, and it’s because it actually is a SyntaxError. You can fix this by deleting %matplotlib inline. It will not change the code’s effect.

Answered By: MiguelCoding

The reason you’re getting an error is you’re using what are called magic commands (and not to be confused with magic method which is an informal synonym for special method). More specifically in this case line magics. There are also cell magics.

Line (and cell) magics are not common to all of python but rather to ipython and (by extension) jupyter notebooks.

To create a word cloud in a notebook that isn’t on your own desktop, you’ll need a platform that is able to run an notebook. Something like binder or Google Colaboratory though there certainly others.

Answered By: hrokr

%matplotlib.inline is a command for Jupyter Notebook. For IDLE simply write your plotting code, without the inline command, and it should be able to produce the plot.

Answered By: Arman
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.