我想在热图上画一条垂直线,以掌握分析对象上发生事件的时间。例如,热图表示 STFT(Short-Time Fourier Transform)的结果有两个维度; x轴是时间轴,y轴表示频率。

源代码和结果

源代码

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

# Make the sample data
arr = np.random.rand(20, 20)

# Draw the heatmap (a figure on the left)
fig, (ax1, ax2) = plt.subplots(1, 2)
sns.heatmap(arr, vmin=0, vmax=5, cmap="jet", ax=ax1)

# Add the vertical line on the headmap (a figure on the right)
sns.heatmap(arr, vmin=0, vmax=5, cmap="jet", ax=ax2)
ax2.axvline(x=4, linewidth=2, color="w")

# Drawing
plt.show()

进入全屏模式 退出全屏模式

结果

左边的图没有画一条竖线,右边的图画了一条。

[执行结果](https://res.cloudinary.com/practicaldev/image/fetch/s--N0vsfQ0W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to -uploads.s3.amazonaws.com/uploads/articles/yvthh464a204u2j1053r.png)

参考

matplotlib.org


这篇原创文章是我写的以下内容。这是这篇原始文章的一部分从日语到英语的翻译。

如何在 Python 中使用 seaborn 绘制的热图上绘制垂直线 - Qiita

图标qiita.com

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐