'Matplotlib not saving axes on savfig
This code only saves this image. As you can see it's rather empty :| Where are my axes plots?
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
Solution 1:[1]
Turns out that it actually is rendering correctly. However I am expecting display on the downside so there is a mathematical issue with the equations happening.
# wealth_ax.set_ylim(top=y_max)
Commenting out the y limiter for the axis allowed me to render this so no issue in the rendering.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael Paccione |