pandas seaborn 热图的人工刻度标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40734343/
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 02:28:50  来源:igfitidea点击:

Artificial tick labels for seaborn heatmaps

pythonpandasheatmapseaborn

提问by Arnold

I have a seaborn heatmap that looks like this:

我有一个如下所示的 seaborn 热图:

enter image description here

在此处输入图片说明

...generated from a pandas dataframe of randomly generated values a piece of which looks like this:

...从随机生成的值的Pandas数据帧生成,其中一部分如下所示:

enter image description here

在此处输入图片说明

The values along the y axis are all in the range [0,1], and the ones on the x axis in the range [0,2*pi], and I just want some short floats at regular intervals for my tick labels, but I can only seem to get values that are in my dataframe. When I try specifying the values I want, it doesn't put them in the right place, as seen in the plot above. He's my code right now. How can I get the axis labels that I tried specifying with xticks and yticks in this code in the correct places (which would be evenly spaced along the axes)?

y 轴上的值都在 [0,1] 范围内,x 轴上的值在 [0,2*pi] 范围内,我只想为我的刻度标签定期设置一些短浮点数,但我似乎只能获得数据框中的值。当我尝试指定我想要的值时,它没有将它们放在正确的位置,如上图所示。他现在是我的密码。如何在正确的位置(沿轴均匀分布)获取我尝试在此代码中使用 xticks 和 yticks 指定的轴标签?

import pandas as pd
import numpy as np
import matplotlib as plt
from matplotlib.mlab import griddata

sns.set_style("darkgrid")
PHI, COSTH = np.meshgrid(phis, cos_thetas)
THICK = griddata(phis, cos_thetas, thicknesses, PHI, COSTH, interp='linear')

thick_df = pd.DataFrame(THICK, columns=phis, index=cos_thetas)
thick_df = thick_df.sort_index(axis=0, ascending=False)
thick_df = thick_df.sort_index(axis=1)

cmap = sns.cubehelix_palette(start=1.6, light=0.8, as_cmap=True, reverse=True)

yticks = np.array([0,0.2,0.4,0.6,0.8,1.0])

xticks = np.array([0,1,2,3,4,5,6])

g = sns.heatmap(thick_df, linewidth=0, xticklabels=xticks, yticklabels=yticks, square=True, cmap=cmap)

plt.show(g)

回答by Julien Marrec

Here's something that should do what you want:

这是应该做你想做的事情:

cmap = sns.cubehelix_palette(start=1.6, light=0.8, as_cmap=True, reverse=True)

yticks = np.linspace(0,1,6)

x_end = 6
xticks = np.arange(x_end+1)

ax = sns.heatmap(thick_df, linewidth=0, xticklabels=xticks, yticklabels=yticks[::-1], square=True, cmap=cmap)

ax.set_xticks(xticks*ax.get_xlim()[1]/(2*math.pi))
ax.set_yticks(yticks*ax.get_ylim()[1])

plt.show()

Seaborn heatmap

Seaborn 热图

You could pass ['{:,.2f}'.format(x) for x in xticks]instead of xticks to get a float with 2 decimals.

您可以通过['{:,.2f}'.format(x) for x in xticks]而不是 xticks 来获得带有 2 个小数的浮点数。

Note that I'm reversing the yticklabels because that's what seaborn does: see matrix.py#L138.

请注意,我正在反转 yticklabels,因为这就是 seaborn 所做的:参见matrix.py#L138

Seaborn calculates the tick positions around the same place (e.g.: #L148), for you that amounts to:

Seaborn 计算同一位置周围的刻度位置(例如:#L148),对于您来说,相当于:

# thick_df.T.shape[0] = thick_df.shape[1]
xticks: np.arange(0, thick_df.T.shape[0], 1) + .5
yticks: np.arange(0, thick_df.T.shape[1], 1) + .5