pandas Seaborn 热图,自定义刻度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47784215/
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
Seaborn heatmap, custom tick values
提问by Nukolas
I plotting a pandas dataframe to a seaborn heatmap, and I would like to set specific y-axis ticks for specific locations.
我将 Pandas 数据框绘制为 seaborn 热图,并且我想为特定位置设置特定的 y 轴刻度。
My dataframe index is 100 rows which corresponds to a "depth" parameter, but the values in this index are not arranged with a nice interval :
I would like to set tick labels at multiples of 100. I can do this fine using :
我的数据帧索引是 100 行,它对应于一个“深度”参数,但是这个索引中的值没有以很好的间隔排列:
我想将刻度标签设置为 100 的倍数。我可以使用以下方法做到这一点:
yticks = np.linspace(10,100,10)
ylabels = np.linspace(100,1000,10)
for my dataframe which has 100 rows, with values from approx 100 - 1000, but the result is clearly not desirable, as the position of the tick labels clearly do not correspond to the correct depth values (index value), only the position in the index.
对于我的数据框,它有 100 行,值大约为 100 - 1000,但结果显然不理想,因为刻度标签的位置显然不对应于正确的深度值(索引值),只有在指数。
How can I produce a heatmap where the plot is warped so that the actual depth values (index values) are aligned with the ylabels I am setting?
我如何生成一个热图,其中的图是扭曲的,以便实际深度值(索引值)与我设置的 ylabels 对齐?
A complicating factor for this is also that the index values are not sampled linearly...
一个复杂的因素也是索引值不是线性采样的......
采纳答案by Nukolas
I have developed a solution which does what I intended, modified after liwt31's solution:
我开发了一个解决方案,它符合我的意图,在 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()
回答by liwt31
My solution is a little bit ugly but it works for me. Suppose your depth data is in depth_list
and num_ticks
is the number of ticks you want:
我的解决方案有点难看,但对我有用。假设您的深度数据在depth_list
并且num_ticks
是您想要的刻度数:
num_ticks = 10
# the index of the position of yticks
yticks = np.linspace(0, len(depth_list) - 1, num_ticks, dtype=np.int)
# the content of labels of these yticks
yticklabels = [depth_list[idx] for idx in yticks]
then plot the heatmap in this way (where your data is in data
):
然后以这种方式绘制热图(您的数据所在的位置data
):
ax = sns.heatmap(data, yticklabels=yticklabels)
ax.set_yticks(yticks)
plt.show()