Create a discrete colorbar in matplotlib

后端 未结 1 1439
半阙折子戏
半阙折子戏 2021-01-07 06:04

I\'ve tried the other threads, but can\'t work out how to solve. I\'m attempting to create a discrete colorbar. Much of the code appears to be working, a discrete bar does a

相关标签:
1条回答
  • 2021-01-07 06:37

    Indeed, the fist argument to colorbar should be a ScalarMappable, which would be the scatter plot PathCollection itself.

    Setup

    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import pandas as pd
    
    df = pd.DataFrame({"x" : np.linspace(0,1,20),
                       "y" : np.linspace(0,1,20),
                       "cluster" : np.tile(np.arange(4),5)})
    
    cmap = mpl.colors.ListedColormap(["navy", "crimson", "limegreen", "gold"])
    norm = mpl.colors.BoundaryNorm(np.arange(-0.5,4), cmap.N) 
    

    Pandas plotting

    The problem is that pandas does not provide you access to this ScalarMappable directly. So one can catch it from the list of collections in the axes, which is easy if there is only one single collection present: ax.collections[0].

    fig, ax = plt.subplots()
    df.plot.scatter(x='x', y='y', c='cluster', marker='+', ax=ax, 
                    cmap=cmap, norm=norm, s=100, edgecolor ='none', alpha=0.70, colorbar=False)
    
    fig.colorbar(ax.collections[0], ticks=np.linspace(0,3,4))
    plt.show()
    

    Matplotlib plotting

    One could consider using matplotlib directly to plot the scatter in which case you would directly use the return of the scatter function as argument to colorbar.

    fig, ax = plt.subplots()
    scatter = ax.scatter(x='x', y='y', c='cluster', marker='+', data=df,
                    cmap=cmap, norm=norm, s=100, edgecolor ='none', alpha=0.70)
    
    fig.colorbar(scatter, ticks=np.linspace(0,3,4))
    plt.show()
    

    Output in both cases is identical.

    0 讨论(0)
提交回复
热议问题