gpt4 book ai didi

python - 如何在热图右侧添加自定义刻度

转载 作者:行者123 更新时间:2023-12-02 01:28:16 25 4
gpt4 key购买 nike

给定以下示例数据

data =\
{'q1': [6, 4, 4, 4, 6, 6, 6, 4, 6, 6, 6, 6],
'q2': [3, 3, 3, 4, 3, 3, 4, 3, 4, 3, 4, 1],
'q3': [6, 3, 4, 4, 4, 4, 6, 6, 6, 6, 4, 1],
'q4': [3, 6, 6, 6, 6, 6, 4, 4, 6, 4, 6, 4],
'q5': [4, 3, 3, 3, 3, 6, 6, 4, 4, 6, 3, 4],
'q6': [3, 3, 4, 4, 6, 3, 3, 1, 6, 4, 4, 4],
'q7': [4, 3, 3, 3, 3, 6, 4, 1, 4, 1, 4, 1],
'q8': [4, 4, 1, 6, 4, 6, 4, 1, 6, 1, 4, 1],
'q9': [4, 6, 4, 3, 4, 3, 4, 6, 6, 4, 3, 4],
'q10': [3, 4, 1, 3, 4, 3, 3, 6, 6, 3, 4, 4]}

这是我的代码:

import seaborn as sns
import pandas as pd

df = pd.DataFrame(data)

cm = sns.heatmap(df.T, annot=False, cbar=True)

cm.get_figure().savefig('heatmap.pdf')

这是输出:

output

我想做的是在右侧添加自定义 y 轴刻度,如下所示:

desired_output

我该怎么做?我按照建议考虑使用 tick_params here ,但这仅使用左侧相同的 y 刻度。

最佳答案

您可以创建双轴并设置其刻度和标签:

cm = sns.heatmap(df.T, annot=False, cbar=True)

# create the twin axis
ax_twin = cm.twinx()

# Set the limits of the y twin axis as the ones of the original y axis
ax_twin.set_ylim(cm.get_ylim())

# Set the position of the ticks in the twin axis
# copying the positions of the ticks on the original axis
ax_twin.set_yticks(cm.get_yticks())

# Set the labels of the just created ticks
ax_twin.set_yticklabels('abcdefghij')

# Save the figure, as before
cm.get_figure().savefig('heatmap.pdf')

这就是你得到的:

enter image description here

关于python - 如何在热图右侧添加自定义刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73919130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com