海运热图,自定义刻度值 [英] Seaborn heatmap, custom tick values

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

问题描述

我将 pandas 数据帧绘制到海运热图,并且我要为特定位置设置特定的y轴刻度。

我的数据帧索引是100行,它对应于一个"Depth"参数,但是该索引中的值没有按合适的间隔排列: 我想将刻度标签设置为100的倍数。我可以使用以下命令很好地完成此操作:

yticks = np.linspace(10,100,10)
ylabels = np.linspace(100,1000,10)

对于我的数据帧,它有100行,值大约在100-1000之间,但是结果显然并不理想,因为刻度标签的位置显然不对应于正确的深度值(索引),而只对应于索引中的位置。

如何生成扭曲了绘图的热图,以便实际深度值(索引)与我正在设置的标签对齐?

这方面的一个复杂因素也是索引值没有线性采样.

推荐答案

我已经开发了一种解决方案,它可以达到我的预期效果,在liwt31的解决方案之后进行了修改:

def round(n, k):
    # function to round number 'n' up/down to nearest 'k'
    # use positive k to round up
    # use negative k to round down

    return n - n % k

# note: the df.index is a series of elevation values
tick_step = 25 
tick_min = int(round(data.index.min(), (-1 * tick_step)))  # round down
tick_max = (int(round(data.index.max(), (1 * tick_step)))) + tick_step  # round up

# the depth values for the tick labels 
# I want my y tick labels to refer to these elevations, 
# but with min and max values being a multiple of 25.
yticklabels = range(tick_min, tick_max, tick_step)
# the index position of the tick labels
yticks = []
for label in yticklabels:
    idx_pos = df.index.get_loc(label)
    yticks.append(idx_pos)

cmap = sns.color_palette("coolwarm", 128)
plt.figure(figsize=(30, 10))
ax1 = sns.heatmap(df, annot=False, cmap=cmap, yticklabels=yticklabels)
ax1.set_yticks(yticks)
plt.show()

这篇关于海运热图,自定义刻度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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