我得到了错误:
TypeError: Image data of dtype object cannot be converted to float
当我尝试运行下面代码中的heatmap
函数:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Read the data
df = pd.read_csv("gapminder-FiveYearData.csv")
print(df.head(10))
# Create an array of n-dimensional array of life expectancy changes for countries over the years.
year = ((np.asarray(df['year'])).reshape(12, 142))
country = ((np.asarray(df['country'])).reshape(12, 142))
print(year)
print(country)
# Create a pivot table
result = df.pivot(index='year',columns='country',values='lifeExp')
print(result)
# Create an array to annotate the heatmap
labels = (np.asarray(["{1:.2f} \n {0}".format(year,value)
for year, value in zip(year.flatten(),
country.flatten())])
).reshape(12, 142)
# Define the plot
fig, ax = plt.subplots(figsize=(15, 9))
# Add title to the Heat map
title = "GapMinder Heat Map"
# Set the font size and the distance of the title from the plot
plt.title(title,fontsize=18)
ttl = ax.title
ttl.set_position([0.5,1.05])
# Hide ticks for X & Y axis
ax.set_xticks([])
ax.set_yticks([])
# Remove the axes
ax.axis('off')
# Use the heatmap function from the seaborn package
hmap = sns.heatmap(result,annot=labels,fmt="",cmap='RdYlGn',linewidths=0.30,ax=ax)
# Display the Heatmap
plt.imshow(hmap)
Here是指向CSV文件的链接。
活动的目的是
1.数据文件是具有6列的数据集,即:国家、年份、流行音乐、大洲、lifeExp
和gdpPercap
。
1.创建一个透视表数据框,x轴为年份沿着y轴为国家,单元格内填充lifeExp
。
1.使用seaborn为刚创建的数据透视表绘制热图。
1条答案
按热度按时间9bfwbjaz1#
感谢您为这个问题提供数据。我相信你的typeError来自你的代码为注解创建的
labels
数组。基于function's built-in annotate properties,我实际上不认为你需要这些额外的工作,它修改你的数据的方式,错误时绘图。我尝试重写了你的项目,以生成一个热图,显示
country
和lifeExp
的year
的数据透视表。我还假设将这个数字保持为float
对您来说很重要。我删除了
labels
代码块。关于sb.heatmap
函数的说明:plt.cm.get_cmap()
来限制贴图中的颜色数量。如果你想使用整个色彩Map表光谱,只需要删除它,并包括你最初拥有它的方式。fmt
=“f”,如果float
是您的lifeExp
值。cbar_kws
-你可以用它来调整颜色条的大小、标签和方向。另一个底部的情节,以显示年轴,稍微放大在我的浏览器。