2016-06-20 3 views
6

У меня есть два набора данных, которые я хочу построить на том же рисунке, например. два косинус и две синусоидальные участки, которые как раз различаются по амплитуде:Слияние линий в легенде MATLAB

x = -pi:pi/20:pi; 
hold all; 
amplitude = 1; 
plot(x,amplitude*cos(x),'-'); 
plot(x,amplitude*sin(x),'-'); 

ax = gca; 
ax.ColorOrderIndex = 1; 

amplitude=3; 
plot(x,amplitude*cos(x),'.'); 
plot(x,amplitude*sin(x),'.'); 

legend('1*cos(x)','1*sin(x)', '2*cos(x)','2*sin(x)'); 
hold off; 

current

Я хочу «сжать» легенду так, что две линии турникеты (нормальная линия и пунктирная линия) «сливаются» и появится рядом с той же записи текстологии в легенде, такие как:

desired

Как я могу добиться этого в MATLAB? В настоящее время я использую R2015b.

+1

Tricky квест ион, я думаю, что это может помочь, посмотрите: [link] (http://stackoverflow.com/questions/33474206/add-custom-legend-without-any-relation-to-the-graph) – Niles

ответ

0

Это ближайший я получил имея быстрый взгляд с r2015b:

Example image

%% 
f = figure; 
ax = axes; 
x = -pi:pi/20:pi; 
hold all; 
amplitude = 1; 
c1 = plot(x,amplitude*cos(x),'-', 'DisplayName', 'cos(x)'); 
s1 = plot(x,amplitude*sin(x),'-', 'DisplayName', 'sin(x)'); 

ax.ColorOrderIndex = 1; 

amplitude=3; 
c2 = plot(x,amplitude*cos(x),'.', 'DisplayName', ' '); 
s2 = plot(x,amplitude*sin(x),'.', 'DisplayName', ' '); 

lg = legend([c1 c2 s1 s2]); 
hold off; 

Манипулирование легенды было проще предварительно HG2 - так использует старую версию Matlab (r2013a) я получаю:

enter image description here

%% 
f = figure; 
ax = handle(axes); 
x = -pi:pi/20:pi; 
hold all; 
amplitude = 1; 
c1 = plot(x,amplitude*cos(x),'r-', 'DisplayName', 'cos(x)'); 
s1 = plot(x,amplitude*sin(x),'b-', 'DisplayName', 'sin(x)'); 

amplitude=3; 
c2 = plot(x,amplitude*cos(x),'r.', 'DisplayName', ' '); 
s2 = plot(x,amplitude*sin(x),'b.', 'DisplayName', ' '); 

lg = handle(legend([c1 c2 s1 s2])); 
hold off; 

% You need to find which of the children on the legend is 
% each of the plots: 
c1 = handle(lg.Children(1)); 
c1.YData = 0.3; 

s1 = handle(lg.Children(7)); 
s1.YData = 0.75; 
+0

To кто бы ни оторвался - можете ли вы объяснить, почему? – matlabgui

+1

не знаю, я просто поддержал :) –

Смежные вопросы