I have a dataframe in the following format:
Percent category MolA MolB
20 1 02K@sc 81G@bs
15 0 04F@sc 79U@bs
16 2 04F@sc 79U@sg
18 1 04F@sc 80A@bs
4 0 04F@sc 80A@ph
I want to plot the MolA column against MolB according to percent value. For coloring each category should be in different color for example 0 -->Blue, 1 --> Red, 2-->Green and then the hue/shades of these color will be according to percentage.
I tried using the sns.colorpalette("YlGnBu", 30)
but this is not exactly what I want and this seems to be not very clear for distinguishing between two different categories.
Is it possible to plot the dataframe in such way?
CodePudding user response:
It is possible to leverage the behavior of heatmap automatically excluding null values from the map by iterating over each category, nulling out Percent
not in that category and making a heatmap for the resulting pivot table. If you loop that you may be able to get what you want.
import pandas as pd
df = pd.DataFrame({'Percent': [20, 15, 16, 18, 4],
'category': [1, 0, 2, 1, 0],
'MolA': ['02K@sc', '04F@sc', '04F@sc', '04F@sc', '04F@sc'],
'MolB': ['81G@bs', '79U@bs', '79U@sg', '80A@bs', '80A@ph']})
palettes = ['Blues','Greens','Oranges']
for c, p in zip(df.category.unique(), palettes):
t = df.copy()
t['Percent'] = np.where(t['category'].ne(c), np.nan, df['Percent'])
sns.heatmap(t.pivot('MolA','MolB','Percent'), cbar=False, cmap=p, annot=True)