尝试添加颜色栏时出错 [英] Error while attempting to add color bar
问题描述
我要创建一个相关矩阵.创建矩阵可以正常工作,直到我尝试添加颜色栏为止.
I'm attmbting to create a correlation matrix. creating the matrix works fine, until I attempt to add the color bar.
这是我当前的代码:
def corr_matrix(data):
'''function to find the mean for days'''
data=data.ix[:,1:].corr(method='pearson')
row_lab=[]
col_lab=[]
for i in data:
row_lab.append(i)
col_lab.append(i)
column_labels = col_lab
row_labels = row_lab
data=np.round(data.corr(method='pearson').abs(), decimals=2)
data=np.array(data)
fig, ax = plt.subplots()
plt.axis('tight')
heatmap = ax.pcolor(data, cmap='RdPu'),
plt.colorbar(mappable=heatmap) # put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(row_labels, minor=False, rotation=90)
ax.set_yticklabels(column_labels, minor=False)
plt.show()
我尝试了 plt.colorbar()
.这也不起作用.任何帮助将是巨大的!
I have tried plt.colorbar()
. This does not work either. Any help would be great!!
我已经看了这个问题:在将颜色条添加到matplotlib中时出现了AttributeError 但是答案似乎不起作用:(
I have looked at this question:AttributeError while adding colorbar in matplotlib but the answers don't seem to work :(
这是我的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 538, in runfile
execfile(filename, namespace)
File "C:/Users/AClayton/Desktop/HData/correlation.py", line 152, in <module>
cmat=corr_matrix(all_data)
File "C:/Users/AClayton/Desktop/HData/correlation.py", line 88, in corr_matrix
plt.colorbar(mappable=heatmap) # put the major ticks at the middle of each cell
File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\matplotlib\pyplot.py", line 2121, in colorbar
ret = gcf().colorbar(mappable, cax = cax, ax=ax, **kw)
File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\matplotlib\figure.py", line 1451, in colorbar
cb = cbar.colorbar_factory(cax, mappable, **kw)
File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\matplotlib\colorbar.py", line 1274, in colorbar_factory
cb = Colorbar(cax, mappable, **kwargs)
File "C:\Users\AClayton\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\lib\site-packages\matplotlib\colorbar.py", line 852, in __init__
mappable.autoscale_None()
AttributeError: 'tuple' object has no attribute 'autoscale_None'
EDIIT all_data
替换为数据,因为它是一个错字.
EDIIT
all_data
replaced with data as it was a typo.
data = pd.DataFrame(np.random.rand(10,10))
会产生错误
推荐答案
这是我不感到骄傲的答案,也许您会对您的问题有相同的看法.
我去了:
This is an answer I am not proud of, and probably you will feel the same about your question.
Here I go:
错误
AttributeError: 'tuple' object has no attribute 'autoscale_None'
由
mappable.autoscale_None()
告诉您其中的热图
plt.colorbar(mappable=heatmap)
实际上是一个元组
怎么了?
如果你写
>>> a = 1,
您正在定义一个元组
>>> a
(1,)
>>> type(a)
<type 'tuple'>
这与您在其中所做的相同:
this is the same as you did in:
heatmap = ax.pcolor(data, cmap='RdPu'),
因此,摆脱分号,您将得到一个像这样的漂亮人物:
so, get rid of the semicolon and you will get a nice figure like this:
这篇关于尝试添加颜色栏时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!