AttributeError while adding colorbar in matplotlib

前端 未结 4 946
一个人的身影
一个人的身影 2020-12-02 18:37

The following code fails to run on Python 2.5.4:

from matplotlib import pylab as pl
import numpy as np

data = np.random.rand(6,6)
fig = pl.figure(1)
fig.clf         


        
4条回答
  •  半阙折子戏
    2020-12-02 18:48

    I found another solution to this problem in a tutorial.

    The code below, will work well for the plt.imshow() method:

    def colorbar(Mappable, Orientation='vertical', Extend='both'):
        Ax = Mappable.axes
        fig = Ax.figure
        divider = make_axes_locatable(Ax)
        Cax = divider.append_axes("right", size="5%", pad=0.05)
        return fig.colorbar(
            mappable=Mappable, 
            cax=Cax,
            use_gridspec=True, 
            extend=Extend,  # mostra um colorbar full resolution de z
            orientation=Orientation
        )
    
    fig, ax = plt.subplots(ncols=2)
    
    img1 = ax[0].imshow(data)
    colorbar(img1)
    
    img2 = ax[1].imshow(-data)
    colorbar(img2)
    
    fig.tight_layout(h_pad=1)
    plt.show()
    

    It may not work well with other plotting methods. For example, it did not work with Geopandas Geodataframe plot.

提交回复
热议问题