Pair plot with heat maps (possibly logarithmic)?

matplotlib, pandas, python, seaborn

Solution

The key to your answer is the matplotlib function `plt.hist2d`, which plots counts within rectangular bins using a color scale (a "heatmap"). Its API is almost compatible with `PairGrid`, but not quite, because it doesn't know how to handle a `color=` kwarg. This is easily solved by writing a thin wrapper function. Also if you want the colormap to logarithmically map counts, that's easily accomplished with a matplotlib `LogNorm`:

import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
sns.set_style("white")
iris = sns.load_dataset("iris")    

g = sns.PairGrid(iris)
g.map_diag(plt.hist, bins=20)

def pairgrid_heatmap(x, y, **kws):
    cmap = sns.light_palette(kws.pop("color"), as_cmap=True)
    plt.hist2d(x, y, cmap=cmap, cmin=1, **kws)

g.map_offdiag(pairgrid_heatmap, bins=20, norm=LogNorm())

Problem

How to create a pair plot in Python like the following: but with heat maps instead of points (or instead of a "hex bin" plot)? Having the possibility of instead displaying logarithmic heat map counts would be an added bonus. (Histograms on the diagonal would be perfectly fine.) By "heat map", I mean a 2D histogram of the counts, displayed like Seaborn's or Wikipedia's heat maps: Using Pandas, seaborn, or matplotlib would be great (maybe plot.ly). I tried naive variations of the following, to no avail: ``` pairplot = sns.PairGrid(data) # sns = seaborn pairplot.map_offdiag(sns.kdeplot) # Off-diagnoal heat map wanted instead! pairplot.map_diag(plt.hist) # plt = matplotlib.pyplot ``` (the above uses a Kernel Density Estimator, which I do not want; a hex bin grid can also be obtained with Pandas, but I am looking instead for a "square" 2D histogram and Matplotlib's `hist2d()` didn't work).

Original source

Related problems