Arn*_*old 3 python heatmap pandas seaborn
我有一个seaborn热图,看起来像这样:
...从随机生成的值的pandas数据帧生成,其中一段看起来像这样:
沿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)
Run Code Online (Sandbox Code Playgroud)
这是应该做你想做的事情:
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()
Run Code Online (Sandbox Code Playgroud)
你可以传递['{:,.2f}'.format(x) for x in xticks]而不是xticks来获得一个带有2位小数的浮点数.
请注意,我正在反转yticklabels,因为这就是seaborn的作用:参见matrix.py#L138.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11409 次 |
| 最近记录: |