ARVIZ will not save energy or density plots

I’m having an issue saving energy plots and density plots to a folder. I’m fitting my model in a pycharm environment so the script is running command line.

The below is the code that is running after the model is fit…

inf_data = az.convert_to_inference_data(fit)
axes_post = az.plot_posterior(inf_data)
fig = axes_post.ravel()[0].figure
fig.savefig(r"C:\Users\JORDAN.HOWELL.GITDIR\PycharmProjects\pythonProject\gooseheadquotes\output\posterior.jpg")

axes_energy = az.plot_energy(fit)

axes_energy.savefig(r"C:\Users\JORDAN.HOWELL.GITDIR\PycharmProjects\pythonProject\gooseheadquotes\output\energy.jpg")

axes_density = az.plot_density(fit)

axes_density.savefig(r"C:\Users\JORDAN.HOWELL.GITDIR\PycharmProjects\pythonProject\gooseheadquotes\output\density.jpg")

Here is the error I get:

Traceback (most recent call last):
  File "C:\Users\JORDAN.HOWELL.GITDIR\PycharmProjects\pythonProject\gooseheadquotes\Model\goosehead_model_run.py", line 38, in <module>
    axes_energy.savefig(r"C:\Users\JORDAN.HOWELL.GITDIR\PycharmProjects\pythonProject\gooseheadquotes\output\energy.jpg")
AttributeError: 'AxesSubplot' object has no attribute 'savefig'
INFO:cmdstanpy:deleting tmpfiles dir: C:\Users\JORDAN~1.GIT\AppData\Local\Temp\tmpku08_r5z
INFO:cmdstanpy:done

The plot posterior saves fine. So I’m not sure what the difference is.

1 Like

In first example you extract the figure object and in the second one you don’t.

Btw if some the title is cut off or something similar add bbox_inches="tight"

To save the current figure object you can use just plt.savefig

plt.savefig(filename, dpi=200, bbox_inches="tight")

To close open figures you can use

plt.close("all")
1 Like