[英]Artificial tick labels for seaborn heatmaps
我有一個看起來像這樣的熱圖:
...從具有隨機生成的值的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)
這是應該執行的操作:
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()
您可以通過['{:,.2f}'.format(x) for x in xticks]
而不是xticks來獲得帶有2個小數的浮點數。
請注意,我正在反轉yticklabel,因為那是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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.