旋转图形而不是图例

时间:2016-05-26 13:22:51

标签: python matplotlib rotation

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

有没有办法只旋转数字? 即使能够将图例的旋转属性设置为90度也没问题。

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

1 个答案:

答案 0 :(得分:2)

您可以查看有关floating axes的演示示例以了解旋转的多种用法。在这种情况下,您只会旋转轴,而不是正常调用图例。

您需要对图例的位置,图表的限制等进行调整,但快速查看此示例应该为您提供背后的逻辑:

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()

这个特殊的配方(改编自本答案顶部的链接)导致:

Rotated axis but with un-rotated legend in matplotlib