[英]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活动的目标是
data file is the dataset with 6 columns namely: country, year, pop, continent, lifeExp
and gdpPercap
. data 文件是包含 6 列的数据集,即:country、year、pop、continent、
lifeExp
和gdpPercap
。
Create a pivot table dataframe with year along x-axes, country along y-axes and lifeExp
filled within cells.创建一个 pivot 表 dataframe,x 轴为年份,y 轴为国家,单元格内填充
lifeExp
。
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 表的
country
和lifeExp
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 的注释:
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.