How can I rotate a matplotlib plot through 90 degrees?

前端 未结 3 1664
逝去的感伤
逝去的感伤 2020-11-29 08:55

I have created a figure in matplotlib which contains three subplots, one in the top left quadrant, one in the top right quadrant, and one in the bottom right quadrant. The t

3条回答
  •  情书的邮戳
    2020-11-29 09:37

    Another interesting parameter for a lot of functions is transform (unlike orientation or pivot this parameter can also be used in e.g. plot).

    The transform parameter allows you to add a transformation, specified by a Transform object. For the sake of example, this is how you would rotate the plot of some random data:

    import numpy
    from matplotlib import pyplot, transforms
    
    data = numpy.random.randn(100)
    
    # first of all, the base transformation of the data points is needed
    base = pyplot.gca().transData
    rot = transforms.Affine2D().rotate_deg(90)
    
    # define transformed line
    line = pyplot.plot(data, 'r--', transform= rot + base)
    # or alternatively, use:
    # line.set_transform(rot + base)
    
    pyplot.show()
    

    For an example on how to rotate a patch, see this answer, which was also the source of inspiration for this answer.


    update

    I recently found out that the transform parameter does not work as expected when using pyplot.scatter (and other PathCollections). In this case, you might want to use the offset_transform. See this answer for more information on how to the offset_transform can be set.

提交回复
热议问题