如何在热图中添加自定义刻度?

How to add customized ticks in Heatmap?

我在 seaborn 热图中绘制了一个数组,我想为轴添加刻度限制。

我的代码:

# plot
eixoz = numpy.linspace(0, Z)
eixor = numpy.linspace(ra, R, nr)
eixox = D
numpy.meshgrid(eixoz, eixor)
ax = seaborn.heatmap(eixox)
ax.invert_yaxis()
plt.xlabel("Eixo z",  fontsize=20)
plt.ylabel("Eixo r", fontsize=20)
ax.get_xaxis().set_ticks([])
ax.get_yaxis().set_ticks([])
ax.collections[0].colorbar.set_label("Celsius", fontsize=20)
plt.show()

如何添加蓝色的限制刻度?还有,我怎样才能调整颜色条数字的大小?

可以通过 ax.collections[0].colorbar.ax.tick_params(labelsize=20) 更改颜色栏刻度标签的大小。

可以使用 axes transform 放置坐标轴起点和终点的文本,其中 0 是坐标轴的左侧(或底部),1 是坐标轴的右侧(或顶部)。负值(或大于 1 的值)在轴区域之外成比例。水平线和垂直线可以使用相同的变换,但不像文本需要 clip_on=False 绘制在轴区域之外。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set()
len_eixoz = 20
eixox = np.repeat(np.arange(37.55, 37.66, 0.02), len_eixoz).reshape(-1, len_eixoz)
ax = sns.heatmap(eixox)
ax.invert_yaxis()
ax.set_xlabel("Eixo z", fontsize=20)
ax.set_ylabel("Eixo r", fontsize=20)
ax.get_xaxis().set_ticks([])
ax.get_yaxis().set_ticks([])
ax.collections[0].colorbar.set_label("Celsius", fontsize=20)
cbar = ax.collections[0].colorbar.ax.tick_params(labelsize=20)

x0, x1 = 1, 2
y0, y1 = 0, 1
ax.text(0, -0.07, x0, ha='center', va='top', fontsize=20, color='steelblue', transform=ax.transAxes)
ax.text(1, -0.07, x1, ha='center', va='top', fontsize=20, color='steelblue', transform=ax.transAxes)
ax.text(-0.05, 0, y0, ha='right', va='center', fontsize=20, color='steelblue', transform=ax.transAxes)
ax.text(-0.05, 1, y1, ha='right', va='center', fontsize=20, color='steelblue', transform=ax.transAxes)

ax.vlines([0, 1], [0, 0], [-0.06, -0.06], color='crimson', clip_on=False, transform=ax.transAxes)
ax.hlines([0, 1], [0, 0], [-0.04, -0.04], color='crimson', clip_on=False, transform=ax.transAxes)
plt.tight_layout()
plt.show()

请注意,在开始时调用 sns.set(font_scale=1.8) 会缩放所有字体。