简体   繁体   中英

How do I make a heatmap with seaborn module in python using the pandas dataframe given below?

记事本中的python pandas数据框表示

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.

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

OUTPUT: #2 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM