How can I rotate a matplotlib plot through 90 degrees?

111,255

Solution 1

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.

Solution 2

Many of the pyplot 1D plots seem to have "orientation" or "pivot" options within their own arguments. For example, from matplotlib.org example of histogram:

matplotlib.pyplot.hist(x, 
                       bins=10, 
                       range=None, 
                       normed=False, 
                       weights=None, 
                       cumulative=False, 
                       bottom=None, 
                       histtype=u'bar', 
                       align=u'mid', 
                       orientation=u'vertical', 
                       rwidth=None, 
                       log=False, 
                       color=None, 
                       label=None, 
                       stacked=False, 
                       hold=None, 
                       **kwargs)

Just change to horizontal (orientation=u'vertical')

Solution 3

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig=plt.figure() 
ax=fig.add_subplot(111,projection='3d')

# for rotate the axes and update.
for angle in range(0,360): 
    ax.view_init(30,angle)

plt.show()

Solution 4

It is simple:

  1. Swap axis (x, y) of scatter/plot data: (y, x).
  2. Changing sign of y to negative (-y, x) rotates plot 90 degree clockwise, changing sign of x to negative (y, -x) rotates plot 90 degree counterclockwise. Details at: https://www.tutorialguruji.com/python/rotate-plot-in-matplotlib-figure-by-90-degree/
Share:
111,255
user3443148
Author by

user3443148

Updated on July 09, 2022

Comments

  • user3443148
    user3443148 almost 2 years

    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 top right figure contains a two-d image, and the other two plots are the projection onto the Y and X axis respectively. I'd like to rotate the top left quadrant subplot through 90deg counterclockwise, so that the x-axis of that plot lies along the y-axis of the 2-d plot.

    For the subplot, I realize I could flip the x and y data, rotate the axis labels, create a plot title on the left hand side, etc. But I was hoping to find a single call which would just rotate the whole, finished plot through 90deg. But I can't find one.

    Is there a simple way to do this?

  • ricma
    ricma about 6 years
    I really like this answer - it helped me a lot. Note that the order of the sum does matter: rot + base != base + rot
  • sameer_nubia
    sameer_nubia almost 6 years
    Main Code :- ax.view_init(-30, -90)
  • Shubham S. Naik
    Shubham S. Naik over 5 years
    But why does matplotlib flip the coordinates by 90 degrees before plotting