how to use 'extent' in matplotlib.pyplot.imshow

后端 未结 2 819
逝去的感伤
逝去的感伤 2020-11-27 22:00

I managed to plot my data and would like to add a background image (map) to it. Data is plotted by the long/lat values and I have the long/lat values for the image\'s three

2条回答
  •  天命终不由人
    2020-11-27 22:47

    Here's an example based on http://matplotlib.org/examples/pylab_examples/image_demo3.html showing use of extent.

    #!/usr/bin/env python
    from pylab import *
    try:
        from PIL import Image
    except ImportError, exc:
        raise SystemExit("PIL must be installed to run this example")
    
    import matplotlib.cbook as cbook
    
    datafile = cbook.get_sample_data('ada.png')
    h = Image.open(datafile)
    dpi = rcParams['figure.dpi']
    figsize = h.size[0]/dpi, h.size[1]/dpi
    
    figure(figsize=figsize)
    ax = axes([0,0,1,1], frameon=False)
    ax.set_axis_off()
    ax.set_xlim(0,2)
    ax.set_ylim(0,2)
    im = imshow(h, origin='upper',extent=[-2,4,-2,4])  # axes zoom in on portion of image
    im2 = imshow(h, origin='upper',extent=[0,.5,0,.5]) # image is a small inset on axes
    
    show()
    

    If you don't set your axis limits, they become your extents & then don't seem to have any effect.

提交回复
热议问题