Binary heat plot in Python

418 Views Asked by At

I'm new to python and need help. I'm trying to produce a binary matrix (presence/absence) with the following data in a CSV format. I have tried several codes found in chats, but non of them solved the problem.

site_Name tool1 tool2 tool3
site1 0 1 0
site2 1 0 0
site3 0 0 1
site4 0 1 1

I have tried to convert the dataset into numpy array, transposed the dataset, dropping columns, etc

1

There are 1 best solutions below

5
JohanC On

You could try a seaborn heatmap:

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

N, M = 10, 20
df = pd.DataFrame((np.random.rand(N, M) * 2).astype(int),
                  columns=[f'tool{i + 1:02d}' for i in range(M)],
                  index=[f'site{i + 1:02d}' for i in range(N)])
sns.heatmap(df, annot=True, lw=1, cbar=False)
plt.tight_layout()
plt.show()

sns.heatmap of binary data