nested x-ticks in seaborn heatmap

2019-07-07 02:32发布

问题:

I have data that looks like this:

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

A = [1,2,3]
A_2 = [1,2,3]
B = [4,5,6]
B_2 = [1,2,1]
C = [7,8,9]
C_2 = [2,3,5]
data = {'A' : A, 'B' : B, 'C' : C}
df =  pd.DataFrame(data)

fig = sns.heatmap(df)
fig.set(yticklabels=['1st', '2nd', '3rd'])
plt.show(fig)

Essentially, I have two tables of data with the exact same dimensions and labels(A,B,C). Now, I'd like to plot them both on the same heatmap but create label grouping.

For instance, in the heatmap, I'd have 6 columns of data: A, A2, B, B2, C, C2, in that exact order and the primary x-labels would just alternate between table1.name and table2.name. Then for every pair, I'd like to have a secondary x-label right below the first one that says A,B,C.

I am able to plot both data tables in the same heatmap by just creating a 25x6 dimension dataframe with all the data, but then I'm unable to create the labels for the x-axis properly.

I'd like to have the x-axis look something like this:

How can I achieve this? I saw many posts that do this for bar charts. But I'm having trouble modifying that code for a heat map.