旋转图形但不旋转图例 [英] Rotate a figure but not the legend

查看:41
本文介绍了旋转图形但不旋转图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

您可以查看关于

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

解决方案

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:

这篇关于旋转图形但不旋转图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆