Issue
Let's say I have a dataframe with this structure
and I intend to transform it into something like this (done laboriously and quite manually):
Is there a simple call to (say) a seaborn or plotly function that would do this? Something like
heatmap(df, x='Dose', y='Distance', z='Passrate')
or perhaps a simple way of restructuring the dataframe to facilitate using sns.heatmap or plotly's imshow, or similar? It seems strange to me that I cannot find a straightforward way of putting data formatted in this way into a high-level plotting function.
Solution
Use df.pivot_table
to get your data in the correct shape first.
Setup: create some random data
import pandas as pd
import numpy as np
import seaborn as sns
p_rate = np.arange(0,100)/np.arange(0,100).sum()
data = {'Dose': np.repeat(np.arange(0,3.5,0.5), 10),
'Distance': np.tile(np.arange(0,3.5,0.5), 10),
'Passrate': np.random.choice(np.arange(0,100), size=70,
p=p_rate)}
df = pd.DataFrame(data)
Code: pivot and apply sns.heatmap
df_pivot = df.pivot_table(index='Distance',
columns='Dose',
values='Passrate',
aggfunc='mean').sort_index(ascending=False)
sns.heatmap(df_pivot, annot=True, cmap='coolwarm')
Result:
Answered By - ouroboros1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.