简体   繁体   English

TypeError: dtype object 的图像数据无法转换为浮点数 - 使用 Seaborn 的 HeatMap Plot 问题

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

I'm getting the error:我收到错误:

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

when I try to run the heapmap function in the code below:当我尝试在下面的代码中运行堆映射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 is a link to the CSV file. 是 CSV 文件的链接。

The objective of the activity is to活动的目标是

  1. data file is the dataset with 6 columns namely: country, year, pop, continent, lifeExp and gdpPercap . data 文件是包含 6 列的数据集,即:country、year、pop、continent、 lifeExpgdpPercap

  2. Create a pivot table dataframe with year along x-axes, country along y-axes and lifeExp filled within cells.创建一个 pivot 表 dataframe,x 轴为年份,y 轴为国家,单元格内填充lifeExp

  3. Plot a heatmap using seaborn for the pivot table that was just created. Plot 为刚刚创建的 pivot 表使用 seaborn 的热图。

Thanks for providing your data to this question.感谢您为此问题提供数据。 I believe your typeError is coming from the labels array your code is creating for the annotation.我相信您的 typeError 来自您的代码为注释创建的labels数组。 Based on the function's built-in annotate properties , I actually don't think you need this extra work and it's modifying your data in a way that errors out when plotting.基于函数的内置注释属性,我实际上认为您不需要这些额外的工作,并且它正在以一种在绘图时出错的方式修改您的数据。

I took a stab at re-writing your project to produce a heatmap that shows the pivot table of country and year of lifeExp .我尝试重新编写您的项目以生成一个热图,显示 pivot 表的countrylifeExp year I'm also assuming that it is important for you to keep this number a float .我还假设将这个数字保持为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

数据透视表

I removed the labels code block.我删除了labels代码块。 Notes on the sb.heatmap function:关于sb.heatmap function 的注释:

  • I used plt.cm.get_cmap() to restrict the number of colors in your mapping.我使用plt.cm.get_cmap()来限制映射中 colors 的数量。 If you want to use the entire colormap spectrum, just remove it and include how you had it originally.如果您想使用整个颜色图谱,只需将其删除并包括您最初的方式。
  • fmt = "f", this if for float , your lifeExp values. fmt = "f", this if for float ,你的lifeExp值。
  • cbar_kws - you can use this to play around with the size, label and orientation of your color bar. cbar_kws - 您可以使用它来调整颜色条的大小、label 和方向。
# 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()

limited screenshot, only b/c the plot is so large有限的截图,只有 b/c plot 这么大最终情节 another of the bottom of the plot to show the year axis, slightly zoomed in on my browser. plot 底部的另一个显示年份轴,在我的浏览器上稍微放大。 附加截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:dtype 对象的图像数据无法转换为浮点数 - TypeError: Image data of dtype object cannot be converted to float TypeError: dtype object 的图像数据无法转换为浮点数修复 - TypeError: Image data of dtype object cannot be converted to float Fix plt.imshow()给出TypeError:无法使用PyTorch将dtype对象的图像数据转换为float - plt.imshow() gives TypeError: Image data of dtype object cannot be converted to float using PyTorch TypeError:dtype对象的图像数据无法转换为float。 当显示来自神经网络的图像时 - TypeError: Image data of dtype object cannot be converted to float . When showing image from neural net 使用 matplotlib.pyplot.imshow() 绘制二维直方图时出现“TypeError:dtype 对象的图像数据无法转换为浮点数” - "TypeError: Image data of dtype object cannot be converted to float" when drawing 2d histogram using matplotlib.pyplot.imshow() 在Tensorflow 2.0中,dtype对象的图像数据无法转换为浮动错误。 是什么原因造成的? - Image data of dtype object cannot be converted to float error in Tensorflow 2.0. What's causing this? 类型错误:图像数据无法转换为浮点数 - TypeError: Image data cannot be converted to float 类型错误:无法将图像数据转换为浮点数 无法将图像数据转换为浮点数 - TypeError: Image data cannot be converted to float Image data cannot be converted to float matplotlib.image.imsave TypeError:无法将图像数据转换为float - matplotlib.image.imsave TypeError: Image data cannot be converted to float Python OpenCV 错误:“类型错误:图像数据无法转换为浮点数” - Python OpenCV Error: “TypeError: Image data cannot be converted to float”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM