【问题标题】:Rotate MATLAB legend旋转 MATLAB 图例
【发布时间】:2014-06-24 09:44:19
【问题描述】:

有没有办法在绘图内旋转 MATLAB 图例?下图应阐明我的要求。

【问题讨论】:

    标签: matlab rotation


    【解决方案1】:

    您需要调整定位,并且如果您绘制的线条不止一条,则需要做更多的工作,但以下示例适用于您的示例。

    plot(1:10);  % create a dummy line
    ha = legend('Plot'); %create a legend
    set(ha,'Units','pixels'); % set axes unit to pixels
    pos = get(ha,'Position'); % get the axes position
    set(ha,'Position',[pos(1) pos(2)-pos(3) pos(4) pos(3)]); % Set the new position
    hc = get(ha,'Children'); % Get the legend contents
    set(hc(3),'Position',[0.5 0.6 0],'Rotation',90); % Relocate and rotate text
    set(hc(2),'Xdata',[0.5 0.5],'YData',[0.1 0.5]); % rotate the line
    set(hc(1),'XData',0.5,'YData',0.3); % Rotate the Marker
    

    【讨论】:

    • 您能否在较新的 MATLAB 版本中验证此代码?我收到“索引超出数组范围”。在第 7 行 (R2018a)
    【解决方案2】:

    该示例不是完全自动化的,但应该让您走上正确的道路。您需要旋转包含图例的框,以及带有文本的标签。/

    % Example plot
    plot(1:10)
    h = legend('something')
    
    % Rotate legend
    set(h,'CameraUpVector', [1 0 0], 'Units','pixels','position',[460 230 25 150])
    
    % Rotate text label
    txt = findobj(h,'type','text');
    set(txt,'rotation',90)
    

    不幸的是,另存为功能恢复了'CameraUpVector'

    【讨论】:

    • 使用绝对像素值调整位置在简单的场景中有效,但在复杂的子图中会产生意想不到的结果。
    • 我同意,这是我最初免责声明的一部分。
    • R2018a 上的错误:“Legend 类上没有 CameraUpVector 属性。”
    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多