Python Plotting: Heatmap from dataframe with fixed colors in case of strings
<p>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.</p>

<p>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.</p>

<p>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?</p>

<p>Thank you for your time.</p>

<p>You could use <code>cmap_custom.set_under('red')</code> and <code>cmap_custom.set_over('black')</code> to apply custom colors to values below and above <code>vmin</code> and <code>vmax</code> (See <a href="http://stackoverflow.com/a/27175748/190597">1</a>, <a href="http://stackoverflow.com/a/35905483/190597">2</a>):</p>

<pre><code>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 (http://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()
</code></pre>

<hr>

<p>The DataFrame</p>

<pre><code>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
</code></pre>

<p>becomes</p>

<p><a href="https://i.stack.imgur.com/9mReF.png" rel="nofollow"><img src="https://i.stack.imgur.com/9mReF.png" alt="enter image description here"></a></p>

python pandas matplotlib dataframe heatmap