Matplotlib chart does not display in PyCharm

Question:

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()
Asked By: william007

||

Answers:

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' )
Answered By: Gabriel

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)
Answered By: vinSan

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.

Answered By: Jerald Cogswell

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

import matplotlib.pyplot as plt
plt.show()
Answered By: Uram Mnisi

%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()
Answered By: Amar_AI

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.

Answered By: Tom Johnson
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.