gpt4 book ai didi

python - Matplotlib 图表在 PyCharm 中不显示

转载 作者:IT老高 更新时间:2023-10-28 20:26:15 24 4
gpt4 key购买 nike

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

我的问题是: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()

最佳答案

% 表示法用于 magic functions .您引用的特定魔术函数和参数 %matplotlib inline 用于 IPython notebook session 。在正常的 python session 中使用魔术函数会出现语法错误。

%matplotlib 魔术函数旨在为 matplotlib 指定后端,如果您不在 IPython 笔记本 session 中,参数 inline 将产生错误。

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

plt.show()

plt.savefig( 'myfig.png' )

关于python - Matplotlib 图表在 PyCharm 中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com