Matplotlib coord. sys origin to top left

前端 未结 5 670
栀梦
栀梦 2020-12-14 09:26

How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I\'m using matplotlib.pylab.plot to produce the plot (

相关标签:
5条回答
  • 2020-12-14 09:47

    The following is a basic way to achieve this

    ax=pylab.gca()
    
    ax.set_ylim(ax.get_ylim()[::-1])
    
    0 讨论(0)
  • 2020-12-14 09:49

    axis ij just makes the y-axis increase downward instead of upward, right? If so, then matplotlib.axes.invert_yaxis() might be all you need -- but I can't test that right now.

    If that doesn't work, I found a mailing post suggesting that

    setp(gca(), 'ylim', reversed(getp(gca(), 'ylim')))
    

    might do what you want to resemble axis ij.

    0 讨论(0)
  • 2020-12-14 09:55

    This

    plt.ylim(max(plt.ylim()), min(plt.ylim()))
    

    has an advantage over this

    plt.gca().invert_yaxis()
    

    and is that if you are in interactive mode and you repeatedly plot the same plot (maybe with updated data and having a breakpoint after the plot) the y axis won't keep inverting every time.

    0 讨论(0)
  • 2020-12-14 10:02

    The easiest way is to use:

    plt.gca().invert_yaxis()
    

    After you plotted the image. Origin works only for imshow.

    0 讨论(0)
  • 2020-12-14 10:04

    For an image or contour plot, you can use the keyword origin = None | 'lower' | 'upper' and for a line plot, you can set the ylimits high to low.

    from pylab import *
    A = arange(25)/25.
    A = A.reshape((5,5))
    
    figure()
    imshow(A, interpolation='nearest', origin='lower')
    
    figure()
    imshow(A, interpolation='nearest')
    
    d = arange(5)
    figure()
    plot(d)
    ylim(5, 0)
    
    show()
    
    0 讨论(0)
提交回复
热议问题