5

I have a figure that I want to rotate by 90 degrees. The problem is that also the legend rotates.

Is there a way to rotate only the figure? Even be able to set the rotation attribute of the legend to 90 degrees would be fine.

For the xticks for example I am using plt.xticks(range(len(mu_mse.index)), x_labs, rotation='vertical')

Donbeo
  • 17,067
  • 37
  • 114
  • 188

1 Answers1

2

You can check the demo example about floating axes for several uses of rotation. In this case you will only rotate the axes and than make the normal call for a legend.

You'll need to make the adjustments for position of legend, limits of the plot and so on, but a quick look at this example should provide you the logic behind it:

from matplotlib.transforms import Affine2D
import mpl_toolkits.axisartist.floating_axes as floating_axes
import numpy as np
import mpl_toolkits.axisartist.angle_helper as angle_helper
from matplotlib.projections import PolarAxes
from mpl_toolkits.axisartist.grid_finder import (FixedLocator, MaxNLocator,
                                                 DictFormatter)
import matplotlib.pyplot as plt


def setup_axes1(fig, rect):
    """
    A simple one.
    """
    tr = Affine2D().scale(2, 1).rotate_deg(30)

    grid_helper = floating_axes.GridHelperCurveLinear(
        tr, extremes=(0, 100, 0, 100))

    ax1 = floating_axes.FloatingSubplot(fig, rect, grid_helper=grid_helper)
    fig.add_subplot(ax1)

    aux_ax = ax1.get_aux_axes(tr)

    grid_helper.grid_finder.grid_locator1._nbins = 4
    grid_helper.grid_finder.grid_locator2._nbins = 4

    return ax1, aux_ax

fig = plt.figure()

x = range(100)
y = x + np.random.randint(-10,10,100)
ax1, aux_ax1 = setup_axes1(fig, 111)
aux_ax1.scatter(x,y,c='green',label='green')
aux_ax1.scatter(y,x,c='purple',label='purple')
ax1.legend()

plt.show()

This particular recipe (adapted from the link at the top of this answer) results in this:

Rotated axis but with un-rotated legend in matplotlib

armatita
  • 12,825
  • 8
  • 48
  • 49
  • How can I use it if my figure is composed by 4 subplots? – Donbeo May 27 '16 at 14:56
  • @Donbeo Actually the original example in the link I gave at the top of my answer comes with 3 subplots (so I simplified the example for demonstration purposes). If you go there you'll see a setup function for each plot, and the common calls to subplots. Just do what you usually do but add a floating axes (those setup functions) to one or more of your plots. – armatita May 27 '16 at 15:02