Rotate MATLAB legend

好久不见. 提交于 2020-03-02 05:36:05

问题


Is there a way to rotate the MATLAB legend inside a plot? The image below should clarify my requirement.


回答1:


You'll need to play around with the positioning, and need to do more work if you have more than one line plotted, but the following does it for your example.

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



回答2:


The example is not fully automated but should set you on the right route. You need to rotate the box containing the legend, and the label with the text./

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

Unfortunately, the save as function restores the 'CameraUpVector'.



来源:https://stackoverflow.com/questions/24376576/rotate-matlab-legend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!