Matplotlib 不在 savefig 上保存轴

Matplotlib not saving axes on savfig

这段代码只保存这张图片。如您所见,它是空的:|我的轴图在哪里?

start = 100
sim_rets = gbm(
    mu=m.mu,
    n_scenarios=m.scenarios,
    n_years=m.years,
    prices=True,
    s_0=start,
    sigma=m.sigma,
    steps_per_year=m.rebalances
)
sofr = 0.28703
risky_r = pd.DataFrame(sim_rets)
# run the "back"-test
btr = run_cppi(risky_r=risky_r, riskfree_rate=sofr,
                m=m.cppi_multiple, start=start, floor=m.floor)
wealth = btr["Wealth"]
# calculate terminal wealth stats
y_max = wealth.values.max()*m.y_max/100
terminal_wealth = wealth.iloc[-1]

# Plot!
fig, (wealth_ax, hist_ax) = plt.subplots(nrows=1, ncols=2,
                                            sharey=True, gridspec_kw={'width_ratios': [3, 2]}, figsize=(24, 9))
plt.subplots_adjust(wspace=0.0)

wealth.plot(ax=wealth_ax, legend=False, alpha=0.3, color="indianred")
wealth_ax.axhline(y=start, ls=":", color="black")
wealth_ax.axhline(y=start*m.floor, ls="--", color="red")
wealth_ax.set_ylim(top=y_max)

terminal_wealth.plot.hist(
    ax=hist_ax, bins=50, ec='w', fc='indianred', orientation='horizontal')
hist_ax.axhline(y=start, ls=":", color="black")

img = BytesIO()
fig.savefig(img, format='jpg')
img.seek(0)

base64_img = base64.b64encode(img.read())

return base64_img

事实证明它确实正确呈现。但是,我预计会出现不利情况,因此方程式会出现数学问题。

# wealth_ax.set_ylim(top=y_max)

注释掉轴的 y 限制器允许我渲染它,所以渲染中没有问题。