TypeError: Image data of dtype object cannot be converted to float - Issue with HeatMap Plot using Seaborn

TypeError: Image data of dtype object cannot be converted to float - Issue with HeatMap Plot using Seaborn

我遇到错误:

TypeError: Image data of dtype object cannot be converted to float

当我尝试 运行 下面代码中的 heapmap 函数时:

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 文件的 link。

activity 的 objective 是

  1. 数据文件是有6列的数据集,即:国家、年份、人口、大陆、lifeExpgdpPercap

  2. 创建一个枢轴 table 数据框,x 轴为年份,y 轴为国家/地区,单元格内填充 lifeExp

  3. 使用 seaborn 为刚刚创建的枢轴 table 绘制热图。

感谢您为这个问题提供数据。我相信您的 typeError 来自您的代码为注释创建的 labels 数组。基于 function's built-in annotate properties,我实际上认为您不需要这个额外的工作,它会以一种在绘图时出错的方式修改您的数据。

我尝试重新编写您的项目以生成热图,显示 country 的支点 table 和 lifeExpyear。我还假设将此数字保持为 float 对您很重要。

import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt

## UNCHANGED FROM ABOVE **
# Read in the data
df = pd.read_csv('https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv')
df.head()

## ** UNCHANGED FROM ABOVE ** 
# 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('show year\n', year)
print('\nshow country\n', country)
# Create a pivot table
result = df.pivot(index='country',columns='year',values='lifeExp')
# Note: This index and columns order is reversed from your code. 
# This will put the year on the X axis of our heatmap
result

我删除了 labels 代码块。 sb.heatmap 函数注释:

  • 我用 plt.cm.get_cmap() 来限制你的颜色数量 映射。如果你想使用整个颜色图谱,只需删除 它并包括你最初是如何拥有它的。
  • fmt = "f",如果 float,你的 lifeExp 值。
  • cbar_kws - 您可以使用它来调整颜色条的大小、标签和方向。
# Define the plot - feel free to modify however you want
plt.figure(figsize = [20, 50])

# Set the font size and the distance of the title from the plot
title = 'GapMinder Heat Map'
plt.title(title,fontsize=24)

ax = sb.heatmap(result, annot = True, fmt='f', linewidths = .5,
                 cmap = plt.cm.get_cmap('RdYlGn', 7), cbar_kws={
                     'label': 'Life Expectancy', 'shrink': 0.5})

# This sets a label, size 20 to your color bar
ax.figure.axes[-1].yaxis.label.set_size(20)
plt.show()

截图有限,只有b/c剧情这么大 另一个显示年轴的图底部,在我的浏览器中稍微放大了。