2016-05-26 35 views
3

我有一個圖形,我想旋轉90度。問題在於圖例也在旋轉。旋轉圖形但不是圖例

有沒有辦法只旋轉圖形? 即使能夠將圖例的旋轉屬性設置爲90度也沒問題。

對於xticks例如我使用plt.xticks(range(len(mu_mse.index)), x_labs, rotation='vertical')

回答

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

+0

我如何使用它,如果我的數字是由4個次要情節組成? – Donbeo

+0

@Donbeo實際上,我在回答頂部給出的鏈接中的原始示例帶有3個子圖(爲了演示目的,我簡化了示例)。如果你去那裏,你會看到每個情節的設置功能,以及常見的子情節調用。只要做你通常做的事情,但是將一個浮動軸(這些設置功能)添加到一個或多個圖中。 – armatita