PyStan示例程序图未显示

时间:2017-11-15 06:20:34

标签: python matplotlib plot stan

在尝试Stan 8学校示例程序时,使用fit.plot()时输出不是绘图。 https://pystan.readthedocs.io/en/latest/getting_started.html

我已经安装了matplotlib以及他们提供的示例。 我是从macOS iterm运行的。

1 个答案:

答案 0 :(得分:0)

缺少plt.show()。 我已粘贴下面的完整代码。

import pystan
import matplotlib.pyplot as plt

schools_code = """
data {
    int<lower=0> J; // number of schools
    real y[J]; // estimated treatment effects
    real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    real eta[J];
}
transformed parameters {
    real theta[J];
    for (j in 1:J)
    theta[j] = mu + tau * eta[j];
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

sm = pystan.StanModel(model_code=schools_code)
fit = sm.sampling(data=schools_dat, iter=1000, chains=4)
print(fit)
fit.plot()

plt.show()