AttributeError:“元组”对象没有属性“ autoscale_None”

时间:2019-10-03 14:24:29

标签: python matplotlib

我正在尝试根据绘图的背景色添加颜色栏。 使用p.color函数将背景色与“颜色”列相关。

我的数据框是:

    color         y        x
0   0      0.647127  5.410  
1   0      0.693702  41.198 
2   0      0.712006  75.373 
3   1      0.722758  110.129
4   2      0.724660  145.137
5   3      0.728484  180.091
6   3      0.734518  215.083
7   4      0.732133  250.100
8   4      0.727203  285.187
9   5      0.734058  320.303
10  6      0.733466  355.319




我运行的代码:

fix, ax= plt.subplots()

ax.scatter(df['x'], df['y'])

heatmap =ax.pcolor(df['color'].values[np.newaxis],
              cmap='Set3', alpha=0.3),
#legend
cbar = plt.colorbar(heatmap)


plt.show()

运行它时,出现以下错误,此时绘图的比例也完全关闭:


AttributeError                            Traceback (most recent call last)
<ipython-input-403-26986b346f8c> in <module>()
     11               cmap='Set3', alpha=0.3),
     12 #legend
---> 13 cbar = plt.colorbar(heatmap)
     14 
     15 

3 frames
/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw)
   2098         ax = gca()
   2099 
-> 2100     ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
   2101     return ret
   2102 colorbar.__doc__ = matplotlib.colorbar.colorbar_doc

/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py in colorbar(self, mappable, cax, ax, use_gridspec, **kw)
   2127                              'panchor']
   2128         cb_kw = {k: v for k, v in kw.items() if k not in NON_COLORBAR_KEYS}
-> 2129         cb = cbar.colorbar_factory(cax, mappable, **cb_kw)
   2130 
   2131         self.sca(current_ax)

/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py in colorbar_factory(cax, mappable, **kwargs)
   1564         cb = ColorbarPatch(cax, mappable, **kwargs)
   1565     else:
-> 1566         cb = Colorbar(cax, mappable, **kwargs)
   1567 
   1568     cid = mappable.callbacksSM.connect('changed', cb.on_mappable_changed)

/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py in __init__(self, ax, mappable, **kw)
   1070         # Ensure the given mappable's norm has appropriate vmin and vmax set
   1071         # even if mappable.draw has not yet been called.
-> 1072         mappable.autoscale_None()
   1073 
   1074         self.mappable = mappable

AttributeError: 'tuple' object has no attribute 'autoscale_None'

编辑已删除逗号更新热图

在我删除逗号并放入heatmap [0]之后,出现以下错误:

    10 heatmap =ax.pcolor(df['color'].values[np.newaxis],cmap='Set3', alpha=0.3)
     11 #legend
---> 12 cbar = plt.colorbar(heatmap[0])
     13 
     14 

TypeError: 'PolyCollection' object does not support indexing

1 个答案:

答案 0 :(得分:3)

pcolor行后还有一个逗号

 heatmap = ax.pcolor(...),  # <--- remove comma!