# ARVIZ will not save energy or density plots

**URL:** <https://discourse.mc-stan.org/t/arviz-will-not-save-energy-or-density-plots/26096>\
**Category:** CmdStan\
**Tags:** arviz\
**Created:** [January 25, 2022, 5:20pm UTC](https://discourse.mc-stan.org/t/arviz-will-not-save-energy-or-density-plots/26096 "2022-01-25T17:20:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jordan\_Howell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jordan_howell/32/4786_2.png) [@Jordan\_Howell](https://discourse.mc-stan.org/u/Jordan_Howell)\
**Post date:** [January 25, 2022, 5:20pm UTC](https://discourse.mc-stan.org/t/arviz-will-not-save-energy-or-density-plots/26096/1 "2022-01-25T17:20:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [January 25, 2022, 5:31pm UTC](https://discourse.mc-stan.org/t/arviz-will-not-save-energy-or-density-plots/26096/2 "2022-01-25T17:31:33Z")

</div>

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")
```
