36

I run the following code in PyCharm 3.4.1, and it highlighted %matplotlib inline showing syntax error, and I delete the first line, and run, I expect it will prompt me some charts, but it runs normally with Process finished with exit code 0, and no charts is showing.

My question is: 1. What is %matplotlib inline use for; 2. How to let pycharm shows matplotlib chart?

%matplotlib inline
from IPython.core.pylabtools import figsize
import numpy as np
from matplotlib import pyplot as plt
figsize(11, 9)

import scipy.stats as stats

dist = stats.beta
n_trials = [0, 1, 2, 3, 4, 5, 8, 15, 50, 500]
data = stats.bernoulli.rvs(0.5, size=n_trials[-1])
x = np.linspace(0, 1, 100)

# For the already prepared, I'm using Binomial's conj. prior.
for k, N in enumerate(n_trials):
    sx = plt.subplot(len(n_trials) / 2, 2, k + 1)
    plt.xlabel("$p$, probability of heads") \
        if k in [0, len(n_trials) - 1] else None
    plt.setp(sx.get_yticklabels(), visible=False)
    heads = data[:N].sum()
    y = dist.pdf(x, 1 + heads, 1 + N - heads)
    plt.plot(x, y, label="observe %d tosses,\n %d heads" % (N, heads))
    plt.fill_between(x, 0, y, color="#348ABD", alpha=0.4)
    plt.vlines(0.5, 0, 4, color="k", linestyles="--", lw=1)

    leg = plt.legend()
    leg.get_frame().set_alpha(0.4)
    plt.autoscale(tight=True)


plt.suptitle("Bayesian updating of posterior probabilities",
             y=1.02,
             fontsize=14)

plt.tight_layout()

6 Answers 6

55

The % notation is for magic functions. The particular magic function and argument you reference, %matplotlib inline, is meant for an IPython notebook session. You will get a syntax error using magic functions in a normal python session.

The %matplotlib magic function is meant to specify a backend for matplotlib and the argument inline will produce an error if you are not in an IPython notebook session.

To show your plot you should use plt.show for an interactive window or plt.savefig to save it to file. For example,

plt.show()

or

plt.savefig( 'myfig.png' )
Sign up to request clarification or add additional context in comments.

3 Comments

yep. i was just referencing the function itself, but added some examples.
yep but better to show the full command or no doubt someone sometime will get confused!
agreed. it's easy to forget what the learning experience was like
9

plot show with some value, helped in providing the window where I was able to see the image. By default the interactive mode was false. I neeed to turn on the interactive mode. plt.interactive(True)

Add the plt.show command after plotting the graph.

plt.show(10)

Comments

2

plt.show() only displays the static PNG form of the plot for me. I had to use 'mpl.use('Qt5Agg')' per this post https://www.tutorialspoint.com/how-to-get-an-interactive-plot-of-a-pyplot-when-using-pycharm.

Comments

2

if using an IDE such as Pycharm, instead of using %matplotlib inline. Try using:

import matplotlib.pyplot as plt
plt.show()

1 Comment

Instead of step 2 just did: plt.show() and 2 of my scatter plots were displayed
1

I got my plot to show by selecting Edit Configurations... and under Execution, check the box Run with Python console. I just used plt.tight_layout(). I was not in a Notebook.

Comments

0

%matplotlib specifies the backend for matplotlib, and with the argument inline you can display the graph and make the plot interactive.

% is used for representing magic function in python in Pycharm i.e %matplotlib inline- becomes a magic function.

In Pycharm you can directly use

plt.show()

Comments

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.