我正在使用 getdist绘制一些模拟结果。 在 jupyter 中只写这一行
g = plots.getSubplotPlotter()
g.triangle_plot([samples, samples2], filled=True)
Python 将显示我们可以看到的图 here .
现在如果我们想在 Python shell 中编写它并使用 IDLE 运行它,这不会产生任何情节。 plt.show() 在这里不起作用。
如何让python或matplotlib显示绘图并保存?
请您参考如下方法:
问题是 getdist 将后端设置为 Agg(在 this line 中),这是一个非交互式后端,因此无法通过 plt.show().
这是非常糟糕的风格,因为用户应该选择后端,而不是包。您可能想将此设计缺陷告知开发人员。
无论如何,通过 plt.switch_backend(..) 导入 getdist 之后 切换后端是可能的。作为后端,您需要使用任何可用的交互式后端,例如“Qt5Agg” 或 “TkAgg”。
import numpy as np
from getdist import plots, MCSamples
import matplotlib.pyplot as plt
plt.switch_backend("Qt5Agg")
# .. some code ..
g = plots.getSubplotPlotter()
g.triangle_plot([samples, samples2], filled=True)
plt.show()
