I am looking to create a 2 x 2 grid of seaborn heatmaps from a pandas dataframe in python, but I am having trouble getting the desired result. Currently, this code...
import numpy as np
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'x': np.random.uniform(0, 100, 1000),
'y': np.random.uniform(0, 100, 1000),
'z1': np.random.uniform(0, 1, 1000),
'z2': np.random.uniform(0, 1, 1000),
'z3': np.random.uniform(0, 1, 1000),
'z4': np.random.uniform(0, 1, 1000)})
fig,axn = plt.subplots(2, 2, sharex=True, sharey=True)
result0 = df.pivot(index='x', columns='y', values='z1')
result1 = df.pivot(index='x', columns='y', values='z2')
result2 = df.pivot(index='x', columns='y', values='z3')
result3 = df.pivot(index='x', columns='y', values='z4')
plt.subplot(2, 2, 1)
sns.heatmap(result0, annot=False, cmap='RdBu_r')
plt.subplot(2, 2, 2)
sns.heatmap(result1, annot=False, cmap='RdBu_r')
plt.subplot(2, 2, 3)
sns.heatmap(result2, annot=False, cmap='RdBu_r')
plt.subplot(2, 2, 4)
sns.heatmap(result3, annot=False, cmap='RdBu_r')
generates a graph that looks like this...
But the features I want to add are:
- Invert each of the y-axes, so that 0 is at the bottom and 100 at the top
- The grid should share both x and y axes, so that the ticks are not displayed unnecessarily
- The y-axis and x-axis ticks should be whole numbers, not decimals (despite the y series being floats
- Each grid should have a separate title
- The colour bar should be shared across all charts
That sample data doesn't really do the heatmap justice, but I couldn't figure out a better way atm. Any help solving five points above would be greatly appreciated. Thanks!