14

is it possible to rotate matplotlib.axes.Axes as it is for matplotlib.text.Text

# text and Axes instance t = figure.text(0.5,0.5,"some text") a = figure.add_axes([0.1,0.1,0.8,0.8]) # rotation t.set_rotation(angle) a.set_rotation()??? 

a simple set_rotation on a text instance will rotate the text by the angle value about its coordinates axes. Is there any way to do to same for the axes instance ?

2 Answers 2

24

Are you asking how to rotate the entire axes (and not just the text)?

If so, yes, it's possible, but you have to know the extents of the plot beforehand.

You'll have to use axisartist, which allows more complex relationships like this, but is a bit more complex and not meant for interactive visualization. If you try to zoom, etc, you'll run into problems.

import matplotlib.pyplot as plt from matplotlib.transforms import Affine2D import mpl_toolkits.axisartist.floating_axes as floating_axes fig = plt.figure() plot_extents = 0, 10, 0, 10 transform = Affine2D().rotate_deg(45) helper = floating_axes.GridHelperCurveLinear(transform, plot_extents) ax = floating_axes.FloatingSubplot(fig, 111, grid_helper=helper) fig.add_subplot(ax) plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way of doing this so that the rotated axis can be plotted on top of another plot? This does create a rotated axis, but its missing a lot of properties that "regular" axes have (e.g., I cannot figure out how to remove the x/y ticks and the background)
@choldgraf - Yes, but for axes made with the axisartist toolkit, the individual spines, xaxis, yaxis, ticks, etc are handled a bit differently. (This is the main difference between a usual Axes and an axisartist Axes. Thus the different API.) Here's an example: gist.github.com/joferkington/6789f086769527cc3157 However, there may be an easier way (e.g. rotating your data). What exactly are you wanting to do?
Thanks for the tip - that did work (for some reason just doing set_visible on the axis and aux_axis object wasn't working). I am trying to create a histogram overlaid onto a scatterplot (e.g., similar to this). It's close to what I'd like, but difficult to scale the floating histogram in an easy way.
@JoeKington is there any way to achieve this rotation without setting extents? Could you please take a look at this
Is it also possible to rotate the subplot with different extents for x and y axis and correct for the aspect ratio ? Say plot_extents = 0, 1, 0, 10 ?
1

Yes, it is possible. But you have to rotate each label separately. Therefore, you can try using an iteration:

from matplotlib import pyplot as plt figure = plt.figure() ax = figure.add_subplot(111) t = figure.text(0.5,0.5,"some text") t.set_rotation(90) labels = ax.get_xticklabels() for label in labels: label.set_rotation(45) plt.show() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.