Colorbar limits are not respecting set vmin/vmax in plt.contourf. How can I more explicitly set the colorbar limits?

后端 未结 2 1177
感动是毒
感动是毒 2020-12-15 09:22

Getting a strange result when trying to adjust the data range when plotting using contourf

import matplotlib
import numpy as np
import matplotlib.cm as cm
im         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-15 10:01

    We can explicitly set the colorbar limits by sending a scalar mappable to colorbar.

    CS = plt.contourf(X, Y, Z, 5, vmin = 0., vmax = 2., cmap=cm.coolwarm)
    plt.title('Simplest default with labels')
    m = plt.cm.ScalarMappable(cmap=cm.coolwarm)
    m.set_array(Z)
    m.set_clim(0., 2.)
    plt.colorbar(m, boundaries=np.linspace(0, 2, 6))
    

提交回复
热议问题