dfm / corner.py

Make some beautiful corner plots

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

change colormap

makerscher opened this issue · comments

Hi,

I tried to change the colormap of the density plot in hist2d

import corner
import numpy as np

ndim, nsamples = 2, 10000
np.random.seed(42)
samples = np.random.randn(ndim * nsamples).reshape([nsamples, ndim])
figure = corner.hist2d(samples[:,0], samples[:,1], pcolor_kwargs={'cmap':'viridis'})

but I get the error message

File ~/anaconda3/lib/python3.10/site-packages/corner/core.py:668 in hist2d
    ax.pcolor(X, Y, H.max() - H.T, cmap=density_cmap, **pcolor_kwargs)

TypeError: matplotlib.axes._axes.Axes.pcolor() got multiple values for keyword argument 'cmap'`

Is there another way to do this? In the end I would like to do something like

figure = corner.corner(samples, hist2d_kwargs={'cmap':'viridis'})

but with this the option is ignored.

The way that we construct color maps here is a little convoluted:

# This is the base color of the axis (background color)
base_color = ax.get_facecolor()
# This is the color map for the density plot, over-plotted to indicate the
# density of the points near the center.
density_cmap = LinearSegmentedColormap.from_list(
"density_cmap", [color, colorConverter.to_rgba(base_color, alpha=0.0)]
)
# This color map is used to hide the points at the high density areas.
base_cmap = LinearSegmentedColormap.from_list(
"base_cmap", [base_color, base_color], N=2
)

and the key feature is that we want it (density_cmap ☝️) to fade to transparent at low density. This means that I don't love the idea of just allowing this to be replaced by another color map in general. I'd probably prefer taking the input color map and updating it to fade to transparent, but that's somewhat more tricky. In the meantime, you could try just updating density_cmap with viridis in the source to see if it looks good for your purposes!

Hi Dan,
thanks, now I see why you dont want it. I will try as you suggest.
Just a thought, if you use plot_datapoints=False, the fade to transparent is probably not needed?

That's right! Although I would expect it to still look "better" to fade instead of ending up with the color at the bottom of your color map - recognizing that that is totally subjective.