sns.heatmap(corr_matrix_T, ax=axs[1, 2], cmap="YlGnBu", cbar=False, annot=True, fmt='.2f'),改变x轴刻度名称
时间: 2024-05-10 07:15:34 浏览: 116
要改变 x 轴刻度的名称,你可以使用 `set_xticklabels()` 方法。以下是一个示例,假设你想将原先的 x 轴刻度名称 "A", "B", "C" 改为 "Feature 1", "Feature 2", "Feature 3":
```python
# 创建一个包含新刻度名称的列表
new_labels = ["Feature 1", "Feature 2", "Feature 3"]
# 设置 x 轴刻度名称
axs[1, 2].set_xticklabels(new_labels)
```
将这段代码放在 `sns.heatmap()` 前面或后面都可以,只要 `axs[1, 2]` 对应的轴是 x 轴即可。完整代码如下:
```python
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# 创建一个随机相关矩阵
corr_matrix = np.random.rand(3, 3)
corr_matrix_T = pd.DataFrame(corr_matrix, columns=["A", "B", "C"])
# 创建一个包含新刻度名称的列表
new_labels = ["Feature 1", "Feature 2", "Feature 3"]
# 创建画布和子图
fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(12, 8))
# 绘制热力图
sns.heatmap(corr_matrix_T, ax=axs[1, 2], cmap="YlGnBu", cbar=False, annot=True, fmt='.2f')
# 设置 x 轴刻度名称
axs[1, 2].set_xticklabels(new_labels)
# 显示图形
plt.show()
```
阅读全文