dfm / corner.py

Make some beautiful corner plots
http://corner.readthedocs.io
BSD 2-Clause "Simplified" License
519 stars 229 forks source link

How can I control the binning and plot limits, and to do so independently? #173

Open jtlz2 opened 3 years ago

jtlz2 commented 3 years ago

I would like fine-grained control over the binning scheme, but at the moment I think I can only provide the number of bins using the bins argument, e.g. bins=20. Is it possible to, for example, supply bin ranges e.g. bins=np.linspace(-1.0,1.0,11)? Or is there another way to achieve the same outcome?

Having binned the data, I would then like to set the limits for each axis. This would be plt.xlim(a,b) and plt.ylim(c,d) in matplotlib. How can I set the axis ranges in/for corner plots?

The need for independent control is to ensure the plotted probability distribution correctly includes the points outside the plot range, and is not influenced by the given axis limits, unless this is specifically required for a particular problem.

[Specifically, for this use case, I have NaNs that I would like to exclude using a magic number, but I want the magic location to be excluded from binning and plotting. The NaNs are uncorrelated, i.e. samples are incomplete and excluding by sample is not possible. But I think this is for a separate question?]

Essentially I would like a manual rather than auto mode.

@dfm Does that make sense? Thanks!

dfm commented 3 years ago

There is no support for supplying your own bin edges, but you can get the result that you're looking for using the range parameter:

samples = np.random.randn(1000, 3)
samples[np.random.rand(*samples.shape) < 0.1] = np.nan
samples[np.isnan(samples)] = 1000 # magic number
corner.corner(samples, range=[(-5, 5), (-5, 5), (-5, 5)]);

To set the axis limits manually, follow the custom plotting tutorial (and use ax.set_xlim), but I don't think that you need it for your described use case.

pmelchior commented 2 years ago

I ran into this issue today as well, and there's a simple solution for specifying custom bin edges: set smooth1d to some small value.

The offending line is https://github.com/dfm/corner.py/blob/3ddef76af113a8967114f12c86f1c41292289bc0/src/corner/core.py#L201 because it requires that bins[i] is a scalar number. However, the code for the smoothed 1d histograms a few lines further down simply passes the bin specifications to np.histogram, which deals just fine with specified bin edges.