Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib chart does not display in PyCharm

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() 
like image 615
william007 Avatar asked Aug 06 '14 14:08

william007


People also ask

Why is matplotlib not showing plot?

The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib.


2 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' ) 
like image 87
Gabriel Avatar answered Sep 17 '22 15:09

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) 
like image 40
vinSan Avatar answered Sep 18 '22 15:09

vinSan