Setting matplotlib colorbar range

后端 未结 3 964
死守一世寂寞
死守一世寂寞 2020-12-25 13:15

I would like to set the matplotlib colorbar range. Here\'s what I have so far:

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(20)
y = np.a         


        
3条回答
  •  臣服心动
    2020-12-25 13:37

    [Sorry, actually a comment to The Red Gator in Virginias answer, but do not have enough reputation to comment]

    I was stuck on updating the colorbar of an imshow object after it was drawn and the data changed with imshowobj.set_data(). Using cbarobj.set_clim() indeed updates the colors, but not the ticks or range of the colorbar. Instead, you have to use imshowobj.set_clim() which will update the image and colorbar correctly.

    data = np.cumsum(np.ones((10,15)),0)
    imshowobj = plt.imshow(data)
    cbarobj = plt.colorbar(imshowobj) #adjusts scale to value range, looks OK
    # change the data to some data with different value range:
    imshowobj.set_data(data/10) #scale is wrong now, shows only dark color
    # update colorbar correctly using imshowobj not cbarobj:
    #cbarobj.set_clim(0,1) #! image colors will update, but cbar ticks not
    imshowobj.set_clim(0,1) #correct
    

提交回复
热议问题