Python Matplotlib 图表在 PyCharm 中不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25163593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:49:05  来源:igfitidea点击:

Matplotlib chart does not display in PyCharm

pythonmatplotlibpycharm

提问by william007

I run the following code in PyCharm 3.4.1, and it highlighted %matplotlib inlineshowing 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.

我在PyCharm 3.4.1中运行以下代码,高亮%matplotlib inline显示语法错误,我删除第一行,然后运行,我期望它会提示我一些图表,但它运行正常Process finished with exit code 0,并且没有图表显示。

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

我的问题是: 1. 有什么%matplotlib inline用;2.如何让pycharm显示matplotlib图表?

%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()

采纳答案by Gabriel

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

%符号用于魔术函数。您引用的特定魔术函数和参数%matplotlib inline,适用于IPython 笔记本会话。在正常的 Python 会话中使用魔法函数会出现语法错误。

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

%matplotlib神奇的功能是为了指定matplotlib后端和参数inline,如果你不是在IPython的笔记本会议将产生一个错误。

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

要显示您的绘图,您应该将其plt.show用于交互式窗口或plt.savefig将其保存到文件中。例如,

plt.show()

or

或者

plt.savefig( 'myfig.png' )

回答by vinSan

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)

绘图显示具有一些价值,有助于提供我能够看到图像的窗口。默认情况下,交互模式为 false。我需要打开交互模式。 plt.interactive(True)

Add the plt.show command after plotting the graph.

绘制图形后添加 plt.show 命令。

plt.show(10)

回答by Jerald Cogswell

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.

我通过选择 Edit Configurations... 并在 Execution 下选中 Run with Python console 框来显示我的图。我只是使用 plt.tight_layout()。我不在笔记本中。