Combining two heat maps in seaborn

后端 未结 2 1199
抹茶落季
抹茶落季 2021-02-09 05:27

I have 2 data tables with the dimensions 4x25. Each table is from a different point in time, but has exactly the same meta data, in essence the same column and row

2条回答
  •  清酒与你
    2021-02-09 06:19

    The best part about matplotlib/seaborn libraries is that everything is plotted in the same figure until you clear it. You can use the mask argument in sns.heatmap to get a diagonal heatmap plot. To get a "mixed" heatmap, such that you can have two different types of data plotted with different colormaps, you can do something like this:

    from sklearn.datasets import load_iris
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    data = load_iris()
    df= pd.DataFrame(data.data,columns = data.feature_names)
    df['target'] = data.target
    
    df_0 = df[df['target']==0]
    df_1 = df[df['target']==1]
    
    df_0.drop('target',axis=1,inplace=True)
    df_1.drop('target',axis=1,inplace=True)
    
    matrix_0 = np.triu(df_0.corr())
    matrix_1 = np.tril(df_1.corr())
    
    import seaborn as sns
    from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
    from mpl_toolkits.axes_grid1.colorbar import colorbar
    sns.heatmap(df_0.corr(),annot=True,mask=matrix_0,cmap="BuPu")
    sns.heatmap(df_1.corr(),annot=True,mask=matrix_1,cmap="YlGnBu")
    

    Hope this is what your second idea was. Note that this will only work when you have same column names.

提交回复
热议问题