12

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 ?

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Cobry
  • 4,348
  • 8
  • 33
  • 49

2 Answers2

23

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

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 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 Feb 09 '15 at 21:34
  • @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: https://gist.github.com/joferkington/6789f086769527cc3157 However, there may be an easier way (e.g. rotating your data). What exactly are you wanting to do? – Joe Kington Feb 09 '15 at 21:57
  • 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](http://nbviewer.ipython.org/github/choldgraf/write-ups/blob/master/matplotlib/diagonal_histogram.ipynb)). It's close to what I'd like, but difficult to scale the floating histogram in an easy way. – choldgraf Feb 10 '15 at 01:01
  • @JoeKington is there any way to achieve this rotation without setting extents? Could you please take a look at [this](https://stackoverflow.com/questions/53966431/rotate-seaborn-heatmap-without-saving-the-plot?noredirect=1#comment94771342_53966431) – steven Dec 29 '18 at 05:03
  • 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 ? – Jyotika Feb 19 '23 at 20:05
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()
T-800
  • 1,543
  • 2
  • 17
  • 26