【问题标题】:TypeError: Image data of dtype object cannot be converted to float - Issue with HeatMap Plot using SeabornTypeError: dtype 对象的图像数据无法转换为浮点数 - 使用 Seaborn 的 HeatMap Plot 问题
【发布时间】:2020-05-19 13:48:56
【问题描述】:

我收到了错误:

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

活动的目的是

  1. 数据文件是6列的数据集,分别为:国家、年份、流行、大陆、lifeExp和gdpPercap。

  2. 创建一个数据透视表数据框,x 轴为年份,y 轴为国家,lifeExp 填充在单元格内。

  3. 使用 seaborn 为刚刚创建的数据透视表绘制热图。

【问题讨论】:

    标签: python numpy matplotlib seaborn heatmap


    【解决方案1】:

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

    我尝试重新编写您的项目以生成显示country 和year 的lifeExp 的数据透视表的热图。我还假设将此号码保留为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剧情这么大 图底部的另一个显示年份轴,在我的浏览器上稍微放大。

    【讨论】:

    • 非常感谢@jwho。这是我期待的输出。像魅力一样工作。
    • 太棒了!很高兴听到它:)
    猜你喜欢
    • 2019-11-04
    • 1970-01-01
    • 2015-11-24
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2018-05-22
    相关资源
    最近更新 更多