39

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?

user3443148
  • 411
  • 1
  • 4
  • 5

4 Answers4

19

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.

Community
  • 1
  • 1
  • 3
    I really like this answer - it helped me a lot. Note that the order of the sum does matter: `rot + base != base + rot` – ricma Apr 19 '18 at 08:04
18

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')

danodonovan
  • 19,636
  • 10
  • 70
  • 78
nicsqr
  • 181
  • 1
  • 2
6
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()
slm
  • 15,396
  • 12
  • 109
  • 124
sameer_nubia
  • 721
  • 8
  • 8
1

It is simple if you just want to get a view of a rotated plot without proper coordinates:

  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

Say, we have initial data:

x_coordinates = np.array([351,345,339,336,333,330,327,324,318,312])
y_coordinates = np.array([84, 84, 83, 82, 81, 80, 80, 79, 78, 77])

Its plot plt.plot(x_coordinates,y_coordinates) is: enter image description here

plt.plot(-y_coordinates,x_coordinates) will rotate 90 counterclockwise: enter image description here

By swap and sign change you can not only rotate, but also flip the plot. Again, here the plot axes will not be shown correct.

Yerbol Sapar
  • 31
  • 1
  • 8
  • dead link there :( – KansaiRobot Nov 10 '22 at 08:52
  • 1
    I removed dead link posed before for reference and instantiated solution – Yerbol Sapar Nov 11 '22 at 07:45
  • Thanks. About the solution, this is the approach I took. However I found out a mistake. You see there you are not rotating. The original X became the Y but the original Y went Horizontal with the opposite sign. (77-> -77) It is not big if you are making a draft, but when other people are involved, they notice that as a defect. So this is not a complete solution – KansaiRobot Nov 13 '22 at 05:39
  • @KansaiRobot That is right, someone needs more than just a new plot view, but coordinates as well. – Yerbol Sapar Nov 14 '22 at 04:42