Overlapping graphs of az.plot_trace()

Hi all,
I’m using PyMC5.
When I try to show a trace plot, the outcome is weird and hard to read.
Please see the screenshot below.
The upper graph and lower graph overlap and their caption is hard to read, right?
How do I make the interval between 2 graphs wider?
I couldn’t find any option for az.plot_trace() to do this.

The attached is the result of az.plot_trace(trace)

My versions are

  • Jupyter Lab : 4.0.7
  • PyMC: v5.16.2
  • OS: MacOS 12.4

Jupyter Lab is running on Docker Desktop(Docker Engine v20.10.17).

I’ve got the same issue…

1 Like

az.plot_trace just does matplotlib stuff, so all the usual matplotlib rules apply.

In this case, you can just call plt.tight_layout() after az.plot_trace(idata). If you wanted to be fancy, you can use plt.gcf() to get the figure object that arviz created and call fig.tight_layout() or fig.use_constrained_layout(), as in:

axes = az.plot_trace(idata)
fig = plt.gcf()
fig.tight_layout()
plt.show()
2 Likes

Thank you so much.
Your solution worked perfect!

Though perhaps tight_layout should be automatically applied by arviz. Having to always do this manual step afterwards is kinda annoying.

1 Like

You can update your plt.rcParams at the top of your scripts/notebooks as follows to avoid having to always call tight_layout:

import matplotlib.pyplot as plt
plt.rcParams.update({'figure.constrained_layout.use':True})

To be honest I’m not 100% clear on the difference between “constrained layout” and “tight layout”, but the results will be the same in this case.

3 Likes

constrained_layout is the recommended method by matplotlib devs (over tight_layout). You can set it as @jessegrabowski already explained.

constrained_layout is set to True if you use any of the styles provided by ArviZ, for instance az.style.use("arviz-doc"), Other available styles are listed here Matplotlib styles — ArviZ 0.19.0 documentation or you can define your own style.

3 Likes

Thank you for a really great suggestion, Mr. @aloctavodia
I’m reading your book “BAP” which I love and I encountered the problem shown above while I was reading the book.

1 Like

I am glad you are enjoying the book. Be sure to check the code at book’s repository BAP3/code at main · aloctavodia/BAP3 · GitHub

1 Like