我如何使用下面给出的pandas数据框在python中使用seaborn模块制作热图?

我如何使用下面给出的pandas数据框在python中使用seaborn模块制作热图?,python,seaborn,Python,Seaborn,这是一个国家和每个国家拥有的汽车数量的数据框架。 最好将国家/地区设置在左/y轴,汽车设置在下/x轴。只需将索引设置为国家/地区,并通过sns绘制热图。热图 代码如下: import pandas as pd import seaborn as sns import matplotlib.pyplot as plt df=pd.DataFrame({'country':['us','france','spain','italy','germany'], 'co

这是一个国家和每个国家拥有的汽车数量的数据框架。
最好将国家/地区设置在左/y轴,汽车设置在下/x轴。

只需将索引设置为
国家/地区
,并通过
sns绘制热图。热图

代码如下:

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
输出:#1

输出:#2

你试过这个吗?请参考这一点。设置inplace等于True做什么?它在df inplace上执行操作,这样您就不必再次将其分配回df
df=df.set_索引(['col'])
相当于
df.set_索引(['col'],inplace=True)
         corvette  ford  toyota
country
us              2     0       1
france          0     1      10
spain           2    10       0
italy          11     0       1
germany         0    10       1