Python 无法从数据框中获取PyPlot热图上的正确标签

Python 无法从数据框中获取PyPlot热图上的正确标签,python,pandas,matplotlib,visualization,Python,Pandas,Matplotlib,Visualization,尝试使用下面的代码渲染热图时收到错误消息。这只是一种测试方法,我有更多涉及到应用到二手车的大型数据集…但我甚至无法用两个数据块解决这个问题 import pandas as pd import matplotlib as plt from matplotlib import pyplot import numpy as np # initialize list of lists #Putting in numbers for the "Name" data ends up working #

尝试使用下面的代码渲染热图时收到错误消息。这只是一种测试方法,我有更多涉及到应用到二手车的大型数据集…但我甚至无法用两个数据块解决这个问题

import pandas as pd
import matplotlib as plt
from matplotlib import pyplot
import numpy as np

# initialize list of lists

#Putting in numbers for the "Name" data ends up working
#data = [[3, 10], [3, 15], [6,50]]

#initializing like this with actual strings for names gives the error
data = [["James", 10], ["Mary", 15], ["Emily", 14]]


# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Age']) 

# print dataframe. 
print(df)

plt.pyplot.pcolor(df, cmap='RdBu')
plt.pyplot.colorbar()
plt.pyplot.ylabel("Age")
plt.pyplot.xlabel("Name")
plt.pyplot.show()
错误如下:

Traceback (most recent call last):
  File "/home/j/dataexploratory.py", line 22, in <module>
    plt.pyplot.colorbar()
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2320, in colorbar
    ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/figure.py", line 2098, in colorbar
    cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 1399, in colorbar_factory
    cb = Colorbar(cax, mappable, **kwargs)
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 945, in __init__
    ColorbarBase.__init__(self, ax, **kw)
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 327, in __init__
    self.draw_all()
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 349, in draw_all
    self._process_values()
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/colorbar.py", line 703, in _process_values
    expander=0.1)
  File "/home/j/anaconda2/lib/python2.7/site-packages/matplotlib/transforms.py", line 2930, in nonsingular
    if (not np.isfinite(vmin)) or (not np.isfinite(vmax)):
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
回溯(最近一次呼叫最后一次):
文件“/home/j/dataexplorative.py”,第22行,在
plt.pyplot.colorbar()
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/pyplot.py”,第2320行,颜色栏
ret=gcf().colorbar(可映射,cax=cax,ax=ax,**千瓦)
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/figure.py”,第2098行,颜色栏
cb=cbar.colorbar_工厂(cax,可映射,**cb_kw)
colorbar_工厂中的文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/colorbar.py”,第1399行
cb=颜色条(cax,可映射,**kwargs)
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/colorbar.py”,第945行,在__
色度基准。初始功率(自身,最大功率,**kw)
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/colorbar.py”,第327行,在__
self.draw_all()
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/colorbar.py”,第349行,全部绘制
self.\u进程\u值()
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/colorbar.py”,第703行,在“进程”值中
扩展器=0.1)
文件“/home/j/anaconda2/lib/python2.7/site packages/matplotlib/transforms.py”,第2930行,非奇异格式
如果(非np.isfinite(vmin))或(非np.isfinite(vmax)):
TypeError:输入类型不支持ufunc“isfinite”,并且无法根据强制转换规则“safe”将输入安全强制为任何受支持的类型

将“名称”转换为分类数据类型。

嗯,你不能从文本数据创建热图。你需要数字数据。此外,对于熊猫,人们通常使用
df.plot…
而不是
plt…
来访问围绕matplotlib的熊猫包装。这是一个开始探索熊猫和策划的好地方。我不明白为什么我不能制作这样的热图。细胞的颜色由个体的年龄决定。因此,我需要从名称数据中得到的只是帮助标记其轴上的记号。为了显示1D数据,使用了条形图。热图用于二维数据。只需尝试一下
df.plot
和链接页面上的选项。此外,还有一些有趣的选项来绘制复杂的热图。