首页 > 解决方案 > 尝试创建热图时出错:“int”和“str”实例之间不支持“<=”

问题描述

我试图找出我不断收到此错误的原因

pt_gov = gov.pivot_table(values="Income", columns="Year", index="State", aggfunc="sum")
plt.figure(figsize=(32,20), dpi=150)

ax = sns.heatmap(pt_gov, cmap="viridis_r", annot=True,
                linewidths=0.005, linecolor="gray", fmt=",.0f",
                cbar=True, annot_kws={"size":15})
ax.set_xticklabels([int(float(y.get_text())) for y in ax.get_xticklabels()])
plt.ylabel("Income", size=20)
plt.xlabel("Year", size=20)
plt.show()

错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-37-ab7b78c9421b> in <module>
      3 plt.figure(figsize=(32,20), dpi=150)
      4 
----> 5 ax = sns.heatmap(pt_fonden, cmap="viridis_r", annot=True,
      6                 linewidths=0.005, linecolor="gray", fmt=",.0f",
      7                 cbar=True, annot_kws={"size":15})

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
    533     """
    534     # Initialize the plotter object
--> 535     plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
    536                           annot_kws, cbar, cbar_kws, xticklabels,
    537                           yticklabels, mask)

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
    162 
    163         # Determine good default values for the colormapping
--> 164         self._determine_cmap_params(plot_data, vmin, vmax,
    165                                     cmap, center, robust)
    166 

~/opt/anaconda3/lib/python3.8/site-packages/seaborn/matrix.py in _determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust)
    201                 vmin = np.nanpercentile(calc_data, 2)
    202             else:
--> 203                 vmin = np.nanmin(calc_data)
    204         if vmax is None:
    205             if robust:

<__array_function__ internals> in nanmin(*args, **kwargs)

~/opt/anaconda3/lib/python3.8/site-packages/numpy/lib/nanfunctions.py in nanmin(a, axis, out, keepdims)
    326         # Slow, but safe for subclasses of ndarray
    327         a, mask = _replace_nan(a, +np.inf)
--> 328         res = np.amin(a, axis=axis, out=out, **kwargs)
    329         if mask is None:
    330             return res

<__array_function__ internals> in amin(*args, **kwargs)

~/opt/anaconda3/lib/python3.8/site-packages/numpy/core/fromnumeric.py in amin(a, axis, out, keepdims, initial, where)
   2790     6
   2791     """
-> 2792     return _wrapreduction(a, np.minimum, 'min', axis, None, out,
   2793                           keepdims=keepdims, initial=initial, where=where)
   2794 

~/opt/anaconda3/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     88                 return reduction(axis=axis, out=out, **passkwargs)
     89 
---> 90     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     91 
     92 

TypeError: '<=' not supported between instances of 'int' and 'str'

<Figure size 4800x3000 with 0 Axes>

恐怕我不完全理解为什么会弹出这个错误,似乎它与 ax = sns.heatmap(pt_gov, cmap="viridis_r", annot=True, 有人可以帮我/解释一下吗?

太感谢了!

标签: pythonnumpy

解决方案


推荐阅读