Issue
Please help with ideas on how I can plot a heatmap with three variables: Entity, Year, and Category. My data looks like below:
index | Entity | Code | Year | status |
---|---|---|---|---|
13645 | South Africa | ZAF | 2004 | 0 |
3082 | China | CHN | 1960 | 2 |
3106 | China | CHN | 1984 | 3 |
2052 | Brazil | BRA | 1950 | 0 |
13594 | South Africa | ZAF | 1953 | 0 |
6538 | India | IND | 1974 | 2 |
6559 | India | IND | 1995 | 3 |
13640 | South Africa | ZAF | 1999 | 0 |
485 | Angola | AGO | 1998 | 0 |
3141 | China | CHN | 2019 | 3 |
467 | Angola | AGO | 1980 | 0 |
2045 | Brazil | BRA | 1943 | 0 |
2065 | Brazil | BRA | 1963 | 0 |
6571 | India | IND | 2007 | 3 |
6515 | India | IND | 1951 | 1 |
463 | Angola | AGO | 1976 | 0 |
439 | Angola | AGO | 1952 | 0 |
3119 | China | CHN | 1997 | 3 |
13638 | South Africa | ZAF | 1997 | 0 |
3123 | China | CHN | 2001 | 3 |
I am hoping to have the 'Entity' on the y-axis, 'Year' on the x-axis, and the heatmap to vary based on the Status (i.e., 0,1,2,3).
Something that looks like a stacked horizontal bar chart would probably do the job for this viz as well (as long as the the Status remains a categorical variable and not a count.
Solution
I am sure there are prettier ways to plot this, but here is a start:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set()
plt.figure(figsize=(20, 10))
sns.heatmap(df.pivot_table(index='Entity', columns='Year', values='status'), cmap='coolwarm', linewidths=0.5, linecolor='white', annot=True)
plt.show()
Answered By - Daniel Schneider
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.