2

I'm trying to visualise a large (pandas) dataframe in Python as a heatmap. This dataframe has two types of variables: strings ("Absent" or "Unknown") and floats.

I want the heatmap to show cells with "Absent" in black and "Unknown" in red, and the rest of the dataframe as a normal heatmap, with the floats in a scale of greens.

I can do this easily in Excel with conditional formatting of cells, but I can't find any help online to do this with Python either with matplotlib, seaborn, ggplot. What am I missing?

Thank you for your time.

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
JRCX
  • 249
  • 2
  • 14

1 Answers1

3

You could use cmap_custom.set_under('red') and cmap_custom.set_over('black') to apply custom colors to values below and above vmin and vmax (See 1, 2):

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1 as axes_grid1
import pandas as pd

# make a random DataFrame
np.random.seed(1)
arr = np.random.choice(['Absent', 'Unknown']+list(range(10)), size=(5,7))
df = pd.DataFrame(arr)

# find the largest and smallest finite values
finite_values = pd.to_numeric(list(set(np.unique(df.values))
                                   .difference(['Absent', 'Unknown'])))
vmin, vmax = finite_values.min(), finite_values.max()

# change Absent and Unknown to numeric values
df2 = df.replace({'Absent': vmax+1, 'Unknown': vmin-1})
# make sure the values are numeric
for col in df2:
    df2[col] = pd.to_numeric(df2[col])

fig, ax = plt.subplots()
cmap_custom = plt.get_cmap('Greens')
cmap_custom.set_under('red')
cmap_custom.set_over('black')
im = plt.imshow(df2, interpolation='nearest', cmap = cmap_custom, 
           vmin=vmin, vmax=vmax)
# add a colorbar (https://stackoverflow.com/a/18195921/190597)
divider = axes_grid1.make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax, extend='both')
plt.show()

The DataFrame

In [117]: df
Out[117]: 
        0        1  2        3  4        5       6
0       3        9  6        7  9        3  Absent
1  Absent  Unknown  5        4  7        0       2
2       3        0  2        9  8        0       2
3       5        5  7  Unknown  5   Absent       4
4       7        7  5        4  7  Unknown  Absent

becomes

enter image description here

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677