pandas 带有对数刻度颜色条的 Seaborn 热图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36898008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:08:17  来源:igfitidea点击:

Seaborn Heatmap with logarithmic-scale colorbar

pandasseaborn

提问by fulatoro

Is there a way to set the color bar scale to log on a seaborn heat map graph?
I am using a pivot table output from pandas as an input to the call

有没有办法设置颜色条比例以登录seaborn热图?
我正在使用 Pandas 的数据透视表输出作为调用的输入

 sns.heatmap(df_pivot_mirror,annot=False,xticklabels=256,yticklabels=128,cmap=plt.cm.YlOrRd_r)

Thank you.

谢谢你。

回答by cphlewis

Yes, but seaborn has hard-coded a linear tick locator for the colorbar, so the result might not be quite what you want:

是的,但是 seaborn 已经为颜色条硬编码了一个线性刻度定位器,所以结果可能不是你想要的:

# http://matplotlib.org/examples/pylab_examples/pcolor_log.html
# modified to use seaborn

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
from matplotlib.mlab import bivariate_normal
import seaborn as sns; sns.set()


N = 20
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]

# A low hump with a spike coming out of the top right.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)

fig, axs = plt.subplots(ncols=2)

sns.heatmap(Z1, ax = axs[0])
sns.heatmap(Z1, ax = axs[1],
            #cbar_kws={'ticks':[2,3]}, #Can't specify because seaborn does
            norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()))


axs[0].set_title('Linear norm colorbar, seaborn')
axs[1].set_title('Log norm colorbar, seaborn')
plt.show()

See the pylab example this started with for a pylab version that automatically gets colorbar tick labels (though is otherwise not as pretty).

请参阅 pylab 示例,该示例以自动获取颜色条刻度标签的 pylab 版本开始(尽管不那么漂亮)。

spiky data with linear and log colorbar

带有线性和对数颜色条的尖峰数据

You can edit the seaborn code to make it work: if you alter the plot()function in /seaborn/matrix.py (ver 0.7.0):

您可以编辑 seaborn 代码以使其工作:如果您更改plot()/seaborn/matrix.py (ver 0.7.0) 中的函数:

    # Possibly add a colorbar
    if self.cbar:
        ticker = mpl.ticker.MaxNLocator(6)
        if 'norm' in kws.keys():
            if type(kws['norm']) is mpl.colors.LogNorm:
                ticker = mpl.ticker.LogLocator(numticks=8)

you get:

你得到:

enter image description here

在此处输入图片说明

I'll suggest that on the seaborn github, but if you want it earlier, there it is.

我会建议在seaborn github上,但如果你更早想要它,它就在那里。

回答by user2084795

You can normalize the values on the colorbar with matplotlib.colors.LogNorm. I also had to manually set the labels in seaborn and ended up with the following code:

您可以使用matplotlib.colors.LogNorm对颜色条上的值进行标准化。我还必须在 seaborn 中手动设置标签,最终得到以下代码:

#!/usr/bin/env python3

import math

import numpy as np
import seaborn as sn
from matplotlib.colors import LogNorm

data = np.random.rand(20, 20)

log_norm = LogNorm(vmin=data.min().min(), vmax=data.max().max())
cbar_ticks = [math.pow(10, i) for i in range(math.floor(math.log10(data.min().min())), 1+math.ceil(math.log10(data.max().max())))]

sn.heatmap(
    data,
    norm=log_norm,
    cbar_kws={"ticks": cbar_ticks}
)

heatmap rand

热图兰特

回答by Thomas G.

Short Answer:

简答:

from matplotlib.colors import LogNorm

sns.heatmap( df,  norm=LogNorm())

回答by Jándr?

Responding to cphlewis (I don't have enough reputation), I solved this problem using cbar_kws; as I saw here: seaborn clustermap: set colorbar ticks.

响应 cphlewis(我没有足够的声誉),我使用cbar_kws;解决了这个问题;正如我在这里看到的:seaborn clustermap: set colorbar ticks

For example cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}.

例如cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}

from matplotlib.colors import LogNorm
s=np.random.rand(20,20)
sns.heatmap(s, norm=LogNorm(s.min(),s.max()),
            cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]},
            vmin = 0.001, vmax=10000)
plt.show()

Have a nice day.

祝你今天过得愉快。