This is a dataframe of countries and the count of cars each country has. It's preferred to have countries on the left/y axis and cars as bottom/x axis.
Asked
Active
Viewed 601 times
-1
-
Did you try this? https://stackoverflow.com/questions/12286607/making-heatmap-from-pandas-dataframe – xszym Jun 28 '20 at 09:06
-
Kindly refer to this.[seaborn.heatmap](http://alanpryorjr.com/visualizations/seaborn/heatmap/heatmap/) – r-beginners Jun 28 '20 at 09:08
1 Answers
1
Simply set the index as country
and plot the heatmap via sns.heatmap
.
Here is the code:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df=pd.DataFrame({'country':['us','france','spain','italy','germany'],
'corvette':[2,0,2,11,0],
'ford':[0,1,10,0,10],
'toyota':[1,10,0,1,1]})
df.set_index(['country'],inplace=True)
print(df) #1
ax=sns.heatmap(df,cmap='coolwarm')
plt.show() #2
OUTPUT: #1
corvette ford toyota
country
us 2 0 1
france 0 1 10
spain 2 10 0
italy 11 0 1
germany 0 10 1

Kaustubh Lohani
- 635
- 5
- 15
-
-
1It performs the operation on the df inplace so that you don't have to assign it back to df again. `df=df.set_index(['col'])` is equivalent to `df.set_index(['col'], inplace=True)` – Kaustubh Lohani Jun 29 '20 at 03:01