DEV Community

Akira Kashihara
Akira Kashihara

Posted on

How to Draw a Vertical Line on a Heatmap Using seaborn, Python

I want to draw a vertical line on a heatmap to grasp when the event occurred on the analysis object. For example, the heatmap expresses the result of STFT (Short-Time Fourier Transform) has two dimensions; the x-axis is the time axis, and the y-axis indicates frequency.

Source Code and The Result

Source Code

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()
Enter fullscreen mode Exit fullscreen mode

The Result

The figure on the left side does not draw a vertical line, and the figure on the right side draws one.

The execution result

Reference


This original article is the following that is written by me. This is a translation of a portion of this original article from Japanese to English.

Pythonのseabornで描画したヒートマップ上に垂直線を引く方法 #matplotlib - Qiita

はじめに ヒートマップは、横軸と縦軸に空間的な座標をとって表示することが良くあります。それに加え、横軸に時間、縦軸に周波数をとるSTFT(Short-Time Fourier Transform)…

favicon qiita.com

Top comments (0)