Ashleigh Clayton
Ashleigh Clayton

Reputation: 1445

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.

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 have tried plt.colorbar(). This does not work either. Any help would be great!!

I have looked at this question:AttributeError while adding colorbar in matplotlib but the answers don't seem to work :(

This is my error:

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)) produces the error

Upvotes: 2

Views: 7554

Answers (1)

joaquin
joaquin

Reputation: 85683

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

The error

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

triggered by

mappable.autoscale_None()

is telling you that heatmap in

plt.colorbar(mappable=heatmap)

is actually a tuple

How comes ?

If you write

>>> a = 1,

you are defining a tuple

>>> 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:

enter image description here

Upvotes: 6

Related Questions