DEV Community

Akira Kashihara
Akira Kashihara

Posted on

3 1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more