dfm / corner.py

Make some beautiful corner plots

Home Page:http://corner.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Top and bottom of the corner plot seem clipped

zhexingli opened this issue · comments

Hello,

I apologize if this seem to be an easy fix. My corner plot (attached) somehow is not displaying normally. The top and the bottom seemed to be clipped. Because of that, some of the percentiles above the histograms at the top can't be seen and at the bottom, the axis labels are fighting with the axis tick marks. I tried to adjust the figure size but doesn't seem to work. Could someone please share some ideas of what's going on here? Thanks.

corner15-1.pdf

Here's my code used to create this plot.

mpl.rcParams['axes.labelsize'] = 7
title_dict = {}
title_dict['fontsize'] = 7
labels = [r'$P_1$',r'$P_2$',r'$m_1$',r'$m_2$',r'$\lambda_1$',r'$\lambda_2$',\ r'$e_1$sin$\omega_1$',r'$e_2$sin$\omega_2$',r'$e_1$cos$\omega_1$',\ r'$e_2$cos$\omega_2$',r'$\sigma_{HARPS1}$',r'$\sigma_{HARPS2}$',\ r'$\sigma_{HIRES}$']

figf = corner.corner(samplesf,show_titles=True,quantiles=[0.16,0.5,0.84],\
                     labels=labels,title_kwargs=title_dict,smooth=True)

for ax in figf.get_axes():
    ax.tick_params(axis='both', labelsize=5)
#fig1.tight_layout()
figf.savefig(path+'cornerall.pdf',dpi=500)`

The best approach for this is generally to use bbox_inches="tight" in the savefig call.

Hi Dan, thanks for the quick reply! I tried that and it partially worked. The top now seems fine now (attached), although there's still clipping when opening up in a pdf for some reasons. At the bottom the axis labels are still on top of the axis tick numbers. Is it the axis tick labels too big or there's a way to move the axis label further down a bit?
cornerall.pdf

I think the labelpad argument should do what you want for that!

https://corner.readthedocs.io/en/latest/api.html

Thanks it works!

Sorry, there's one thing I couldn't understand. When not using the padding argument and looking at my last corner plot, why does it appear that the distance between the x-axis and x-axis label is much shorter than that for the y-axis, causing the label overlap on the x-axis?

I assume that's something to do with your matplotlib rcparams making the aspect ratio all wonky. With the default settings, corner always generates square figures, but I'm not sure exactly what's happening there!

Ok thanks. I'm not sure what changed the default rcparams settings. I'll see if I can change them back to default.

Great - feel free to report back here if you get it sorted or not to help future users.

Hello again,

I tried a couple things and it appears to be corner.py related issue. Here's what I tried:

To make sure everything else works fine, I restarted anaconda and ipython, reset rcparams to default, and followed the example plotting on corner.py's documentaiton page:

ndim, nsamples = 2, 10000
np.random.seed(42)
samples = np.random.randn(ndim * nsamples).reshape([nsamples, ndim])
fig = corner.corner(samples)

It plotted the test corner plot with no problem at all and the dimension looks good. I then double checked with the following
fig.get_size_inches()
and got the right default [6.4,4.8] values.

Then, I bumped the number of dimensions to a larger number:
ndim, nsamples = 13, 10000
np.random.seed(42)
samples = np.random.randn(ndim * nsamples).reshape([nsamples, ndim])
fig = corner.corner(samples)
fig.savefig(file.pdf)

Then the same problem occurs. Top and bottom clipping. When I checked the figure dimension again, it's saying it has this weird [14.4,7.9] dimension.
I tried with other dimensions and they all appear to have the same [14.4,7.9] output dimension and have the same clipping problem.

I'm not sure why it's doing this since people use corner.py a lot and with large dimensions before but all seem fine. But none of my other plots have similar issues...

This is a problem with your environment. In your first example, the size should be square:

Screen Shot 2022-06-24 at 7 48 10 PM

Yes sorry my mistake. It indeed returned [5.5,5.5] dimension for the first example.

I think I found out what's causing the problem. It appears to be a spyder issue and the plotting backend being used. When working in spyder the default plotting backend was restricted to the screen size of my laptop (probably because of its interactive feature), so that when the plot dimension goes over that, it'll start to ignore what the code does and change the output dimension, ultimately clip out the part exceeding the screen size and squeeze everything within the limited size. To avoid this, I need to either run the corner plot code from the command line, or to change the plotting backend:
matplotlib.get_backend()
gives me 'Qt5Agg' I guess it's the default backend I have been using.

Then I switched to another non-interactive backend
matplotlib.use('Agg')
and everything works now.

This explains why I have never had such issue before because I have been doing that using CLI all the time. It's just this time I'm testing something different.

Anyway, thanks for the help. I learned something today.

Interesting! Glad you got it sorted out and thanks for sharing back your result so that we could all learn!

and I believe this could explain some of other questions people raised about labels overlapping in the plot.

Glad it finally worked and happy my solution could be helpful to the community :)