Change values on matplotlib imshow() graph axis

前端 未结 2 640
我在风中等你
我在风中等你 2020-11-30 23:05

Say I have some input data:

data = np.random.normal(loc=100,scale=10,size=(500,1,32))
hist = np.ones((32,20)) # initialise hist
for z in range(32):
    hist[         


        
2条回答
  •  佛祖请我去吃肉
    2020-11-30 23:57

    I would try to avoid changing the xticklabels if possible, otherwise it can get very confusing if you for example overplot your histogram with additional data.

    Defining the range of your grid is probably the best and with imshow it can be done by adding the extent keyword. This way the axes gets adjusted automatically. If you want to change the labels i would use set_xticks with perhaps some formatter. Altering the labels directly should be the last resort.

    fig, ax = plt.subplots(figsize=(6,6))
    
    ax.imshow(hist, cmap=plt.cm.Reds, interpolation='none', extent=[80,120,32,0])
    ax.set_aspect(2) # you may also use am.imshow(..., aspect="auto") to restore the aspect ratio
    

    enter image description here

提交回复
热议问题