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?
Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With