numpy TypeError:dtype对象的图像数据无法转换为float -使用Seaborn的HeatMap Plot问题

7z5jn7bk  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(196)

我得到了错误:

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列的数据集,即:国家、年份、流行音乐、大洲、lifeExpgdpPercap
1.创建一个透视表数据框,x轴为年份沿着y轴为国家,单元格内填充lifeExp
1.使用seaborn为刚创建的数据透视表绘制热图。

9bfwbjaz

9bfwbjaz1#

感谢您为这个问题提供数据。我相信你的typeError来自你的代码为注解创建的labels数组。基于function's built-in annotate properties,我实际上不认为你需要这些额外的工作,它修改你的数据的方式,错误时绘图。
我尝试重写了你的项目,以生成一个热图,显示countrylifeExpyear的数据透视表。我还假设将这个数字保持为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()来限制贴图中的颜色数量。如果你想使用整个色彩Map表光谱,只需要删除它,并包括你最初拥有它的方式。
  • 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的情节是如此之大 *

    另一个底部的情节,以显示年轴,稍微放大在我的浏览器。

相关问题