给出以下示例数据

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

我怎么能这样做呢?我按照建议的here使用了tick_params,但这只使用了左侧相同的y-tick.

推荐答案

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

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相关问答推荐

Python在tuple上操作不会通过整个单词匹配

Pandas 第二小值有条件

如何使用LangChain和AzureOpenAI在Python中解决AttribeHelp和BadPressMessage错误?

C#使用程序从Python中执行Exec文件

将9个3x3矩阵按特定顺序排列成9x9矩阵

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

Django admin Csrf令牌未设置

搜索按钮不工作,Python tkinter

如何在Great Table中处理inf和nans

根据客户端是否正在传输响应来更改基于Flask的API的行为

如果有2个或3个,则从pandas列中删除空格

如何在FastAPI中替换Pydantic的constr,以便在BaseModel之外使用?'

使用嵌套对象字段的Qdrant过滤

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?

read_csv分隔符正在创建无关的空列

如何获取给定列中包含特定值的行号?

在MongoDB文档中仅返回数组字段

在不中断格式的情况下在文件的特定部分插入XML标签

来自任务调度程序的作为系统的Python文件

PYODBC错误(SQL包含-26272个参数标记,但提供了235872个参数,HY 000)