Legend and LineStyle with Plotyy

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

Legend and LineStyle with Plotyy

Bugzilla from torquil@gmail.com
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.


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

% 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
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 graph colors

Best regards
Torquil Sørensen
Help-octave mailing list
[hidden email]
Reply | Threaded
Open this post in threaded view

Re: Legend and LineStyle with Plotyy

David Bateman-3
Sorry for the delay in responding. Issues with plotyy were one of the
things I wanted to address, before a 3.2.0 release of Octave, and so I
just stuck your mail on my todo pile. However, I now suspect that your
problems are harder to address than I'd thought.

Firstly, I propose the simpler example code

x = 0:0.1:2*pi;
y1 = sin (x);
y2 = exp (x - 1);
[ax, h1, h2] = plotyy (x, y1, x - 1, y2, @plot, @semilogy);
legend([h1, h2], 'y1', 'y2')

to demonstrate the issues. The linestyle issue is in fact a limitation
of gnuplot and there is not much we can do about it at this point. See
the comment in __go_draw_axes__.m that states

    ## FIXME -- linetype is currently broken, since it disables the
    ## gnuplot default dashed and solid linestyles with the only
    ## benefit of being able to specify '--' and get a single sized
    ## dashed line of identical dash pattern for all called this way.
    ## All dash patterns are a subset of "with lines" and none of the
    ## lt specifications will correctly propagate into the x11 terminal
    ## or the print command.   Therefore, it is currently disabled in
    ## order to allow print (..., "-dashed") etc. to work correctly.

    ##    if (! isempty (lt))
    ##      fprintf (plot_stream, " linetype %s", lt);
    ##      found_style = true;
    ##    endif

so as the comment states the gnuplot terminal does not respect the
dashed linestyles, and the code to do it (ie 4 lines) is commented out.
You can still be dashed lines in the printed plots for your publications
with the "-dashed" option to the print command.

As for the legend, there is an incompatibility in how Octave and Matlab
treat legends.. In Octave a legend is assoicated with an axis, whereas
in Matlab a legend is a new axis object with the keys printed. The
Octave way of doing it allows easy interfacing with the gnuplot legend
code. The downside of Octave's choice is that you'd need a legend that
is different for each of the axes of the plotyy command. In Octave 3.0.2
you might try something like

x = 0:0.1:2*pi;
y1 = sin (x);
y2 = exp (x - 1);
[ax, h1, h2] = plotyy (x, y1, x - 1, y2, @plot, @semilogy);
legend ('y1')


David Bateman                                [hidden email]
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax)

The information contained in this communication has been classified as:

[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary

Help-octave mailing list
[hidden email]