Legend and LineStyle with Plotyy

Torquil Macdonald Sørensen torquil at gmail.com
Fri Aug 8 15:26:33 CDT 2008


Hi, I am converting a Matlab-script to Octave. It uses plotyy, but I 
can't get the legend to work, or different linestyles. I always try to 
have understandable plots without colors, by using different linestyles. 
In this case I want one solid curve, and one dashed curve.

Here is the result of my efforts this far (this is the contents of a 
file "air_resistance.m". The problem is that both curves are solid, and 
there is no legend. Note that it is only the failure of the legend 
command that is the reason for the blue and green colours. By commenting 
out legend, the colours are fine.

1;

% Differential equation
function dydt = f(y, t)
	dydt = [y(2); -sign(y(2))*y(2)^2-y(1)];
endfunction

% Time span
t = linspace(0, 10, 100);

% Initial conditions
y0=[1; 0];

% Solve the differential equation
lsode_options('relative tolerance', 0.01);
y=lsode(@f, y0, t);

% Plot the solution
[ax,h1,h2]=plotyy(t,y(:,1),t,y(:,2));
xlabel('t');
title('Solution of d^2y/dt^2+sign(dy/dt)*(dy/dt)^2+y=0');

ylabel(ax(1), 'y');
ylabel(ax(2), 'dy/dt');

% Set graph styles
set(h1,'LineStyle','-')  % Works in MatLab
set(h2,'LineStyle','--') % Works in MatLab

% Legend
legend([h1 h2],'y','dy/dt'); % Works in MatLab

%% Esthetics
% Set y-axis colors
set(ax(1),'ycolor','k')
set(ax(2),'ycolor','k')
% Set graph colors
set(h1,'Color','k')
set(h2,'Color','k')

Best regards
Torquil Sørensen


More information about the Help-octave mailing list