Error trying to add color bar

I am trying to create a correlation matrix. creating a matrix works fine until I try to add a color panel.

This is my current code:

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() 

I tried plt.colorbar() . This does not work either. Any help would be great!

I examined this question: AttributeError when adding colorbar to matplotlib , but the answers do not seem to work :(

That's my fault:

 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 replaced with data as it was a typo.

data=pd.DataFrame(np.random.rand(10,10)) causes an error

+1
python matplotlib colorbar
source share
1 answer

This is an answer that I am not proud of, and you will probably feel the same way about your question.
Here I go:

Mistake

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

caused by

 mappable.autoscale_None() 

tells you that the heat map in

 plt.colorbar(mappable=heatmap) 

really is a tuple

As it turns out?

If you write

 >>> a = 1, 

you define a tuple

 >>> a (1,) >>> type(a) <type 'tuple'> 

this is the same as you:

 heatmap = ax.pcolor(data, cmap='RdPu'), 

so get rid of the semicolon and you get such a beautiful figure:

enter image description here

+2
source share

All Articles