旋转图形但不旋转图例

Don*_*beo 5 python matplotlib rotation

我有一个图形,我想旋转 90 度。问题是图例也会旋转。

有没有办法只旋转图形?甚至能够将图例的旋转属性设置为 90 度就可以了。

对于 xticks 例如我正在使用plt.xticks(range(len(mu_mse.index)), x_labs, rotation='vertical')

arm*_*ita 2

您可以查看有关浮动轴的演示示例,以了解旋转的多种用途。在这种情况下,您将仅旋转轴,而不是对图例进行正常调用。

您需要对图例的位置、绘图的限制等进行调整,但快速浏览一下此示例应该可以为您提供其背后的逻辑:

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()
Run Code Online (Sandbox Code Playgroud)

这个特殊的食谱(改编自本答案顶部的链接)会导致以下结果:

matplotlib 中旋转轴但具有未旋转图例