将图与热图配对(可能是对数的)?
Pair plot with heat maps (possibly logarithmic)?
如何在 Python 中创建一对图,如下所示:
但是使用 热图 而不是点(或 "hex bin" 图)?有可能改为显示对数热图计数将是一个额外的好处。 (对角线上的直方图就可以了。)
"heat map",我指的是计数的二维直方图,显示为 Seaborn's or Wikipedia's 热图:
使用 Pandas、seaborn 或 matplotlib 会很棒(也许 plot.ly)。
我尝试了以下天真的变体,但无济于事:
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
(上面使用了我不想要的内核密度估计器;也可以使用 Pandas 获得十六进制网格,但我正在寻找 "square" 2D 直方图和Matplotlib 的 hist2d()
没用)。
准备:
%matplotlib inline #for jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")
新答案:
g = sns.PairGrid(iris)
g = g.map_upper(plt.scatter,marker='+')
g = g.map_lower(sns.kdeplot, cmap="hot",shade=True)
g = g.map_diag(sns.kdeplot, shade=True)
sns.plt.show()
答案:
g = sns.PairGrid(iris)
g = g.map_upper(plt.scatter)
g = g.map_lower(sns.kdeplot, cmap="hot",shade=True)
g = g.map_diag(plt.hist)
sns.plt.show()
答案的关键是 matplotlib 函数 plt.hist2d
,它使用色标 ("heatmap") 在矩形箱内绘制计数。它的 API 几乎与 PairGrid
兼容,但不完全兼容,因为它不知道如何处理 color=
kwarg。这很容易通过编写一个薄包装函数来解决。此外,如果您希望颜色图以对数方式映射计数,则可以使用 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())
如何在 Python 中创建一对图,如下所示:
"heat map",我指的是计数的二维直方图,显示为 Seaborn's or Wikipedia's 热图:
使用 Pandas、seaborn 或 matplotlib 会很棒(也许 plot.ly)。
我尝试了以下天真的变体,但无济于事:
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
(上面使用了我不想要的内核密度估计器;也可以使用 Pandas 获得十六进制网格,但我正在寻找 "square" 2D 直方图和Matplotlib 的 hist2d()
没用)。
准备:
%matplotlib inline #for jupyter notebook
import matplotlib.pyplot as plt
import seaborn as sns
iris = sns.load_dataset("iris")
新答案:
g = sns.PairGrid(iris)
g = g.map_upper(plt.scatter,marker='+')
g = g.map_lower(sns.kdeplot, cmap="hot",shade=True)
g = g.map_diag(sns.kdeplot, shade=True)
sns.plt.show()
答案:
g = sns.PairGrid(iris)
g = g.map_upper(plt.scatter)
g = g.map_lower(sns.kdeplot, cmap="hot",shade=True)
g = g.map_diag(plt.hist)
sns.plt.show()
答案的关键是 matplotlib 函数 plt.hist2d
,它使用色标 ("heatmap") 在矩形箱内绘制计数。它的 API 几乎与 PairGrid
兼容,但不完全兼容,因为它不知道如何处理 color=
kwarg。这很容易通过编写一个薄包装函数来解决。此外,如果您希望颜色图以对数方式映射计数,则可以使用 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())