pyplot/matplotlib Bar chart with fill color depending on value

后端 未结 2 572
南笙
南笙 2021-02-01 22:31

I want to produce in python with matplotlib/pyplot

  • a bar chart with a fill depending on the value.
  • legend color bar

while

2条回答
  •  野性不改
    2021-02-01 23:09

    You can use Normalize and ScalarMappable without plotting a scatter. For example:

    import matplotlib mpl
    import matplotlib.pyplot as plt
    from matplotlib import cm
    
    f,(ax1,ax2) = plt.subplots(2)
    
    #ax1 --> plot here your bar chart
    
    norm = mpl.colors.Normalize(vmin=0, vmax=1)
    
    mpl.colorbar.ColorbarBase(ax2, cmap=cm.RdBu,
                                    norm=norm,
                                    orientation='horizontal')
    

    Finally, add the desired format to the colorbar.

提交回复
热议问题