具有对数刻度色条的Seaborn热图 [英] Seaborn Heatmap with logarithmic-scale colorbar

查看:339
本文介绍了具有对数刻度色条的Seaborn热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以设置色条比例尺来登录海洋热图图?
我正在使用从熊猫输出的数据透视表作为呼叫的输入

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

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

谢谢.

推荐答案

是的,但是seaborn已为色条硬编码了线性刻度定位器,因此结果可能不是您想要的:

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

# 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()

有关自动获取颜色条刻度标签的pylab版本,请参见此示例的pylab示例(尽管不是那么漂亮).

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

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

You can edit the seaborn code to make it work: if you alter the plot() function in /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)

您得到:

我建议在Seaborn的github上使用,但是如果您想早点使用它,那就可以了.

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

这篇关于具有对数刻度色条的Seaborn热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆