DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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.

Top comments (0)

DEV has this feature:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. ๐Ÿ›