How to draw a vertical line on a heatmap drawn with Python seaborn

Introduction

Heatmaps are often displayed with spatial coordinates on the horizontal and vertical axes. In addition to that, there are times when you want to take the horizontal axis as time, such as drawing the result of STFT (Short-Time Fourier Transform) where the horizontal axis is time and the vertical axis is frequency. At that time, I wanted to put a vertical line anywhere in the analysis to get a picture of when the event fired. I couldn't find a way to draw a vertical line with seaborn, so I'll leave it as a memorandum.

Target

Put a vertical line in the heatmap

Method

It seems that you can enter it normally using axvline. So I actually tried it.

vline_heatmap.py


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

#Sample array creation
arr = np.random.rand(20, 20)

#Heat map display (left side)
fig, (ax1, ax2) = plt.subplots(1, 2)
sns.heatmap(arr, vmin=0, vmax=5, cmap="jet", ax=ax1)

#Display with vertical lines on the heat map (right side)
sns.heatmap(arr, vmin=0, vmax=5, cmap="jet", ax=ax2)
ax2.axvline(x=4, linewidth=2, color="w")

#drawing
plt.show()

Drawing result

The drawing result with no vertical line on the left and with vertical line on the right is shown. Figure_1.png

Opinions etc.

Please write in the comments of this article or send to the following email address (change [at] to @).

akira.kashihara[at]hotmail.com

reference

After investigating and tracing the vertical lines with reference to article 1, I came across article 2 and tried it. If you have any other articles that you are writing directly, please let me know.

  1. Draw a horizontal line at any position on the y-axis in the graph using seaborn
  2. matplotlib.pyplot.axvline

Recommended Posts

How to draw a vertical line on a heatmap drawn with Python seaborn
[Python] How to draw a line graph with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
How to install NPI + send a message to line with python
How to create a heatmap with an arbitrary domain in Python
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
How to draw a 2-axis graph with pyplot
Strategy on how to monetize with Python Java
Try to draw a life curve with python
[Python] How to draw multiple graphs with Matplotlib
[Python] How to draw a histogram in Matplotlib
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
How to convert / restore a string with [] in python
I tried to draw a route map with Python
How to build a Django (python) environment on docker
[Python] How to create a 2D histogram with Matplotlib
How to build a Python environment on amazon linux 2
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
How to build a new python virtual environment on Ubuntu
How to convert an array to a dictionary with Python [Application]
[Ev3dev] How to display bmp image on LCD with python
How to build a python2.7 series development environment with Vagrant
How to write a Python class
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to batch start a python program created with Jupyter notebook
[Introduction to Python] How to split a character string with the split function
[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to make a surveillance camera (Security Camera) with Opencv and Python
How to get a string from a command line argument in python
How to customize U-Boot with OSD335X on a custom board (memo)
How to use python put in pyenv on macOS with PyCall
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
[Python] How to get a value with a key other than value with Enum
[ROS2] How to play a bag file with python format launch
How to send a request to the DMM (FANZA) API with python
How to add a package with PyCharm
[Python] How to make a class iterable
How to draw a graph using Matplotlib
[Kivy] How to install Kivy on Windows [Python]
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
How to work with BigQuery in Python
[Python] How to invert a character string
How to erase Python 2.x on Mac.
How to do portmanteau test with python
Memorandum on how to use gremlin python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
How to choose a Seaborn color palette
How to test on a Django-authenticated page
[Python] How to deal with module errors
How to run a Maya Python script
How to install python3 with docker centos
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
[Python] Explains how to use the range function with a concrete example