plotyy line width and color - with multiple lines

61 views (last 30 days)
Hi Everyone, I know there are posts on similar lines but I am having difficulty specifically with plotyy in which I have multiple lines plotted on the left hand axis, with one plotted on the right hand axis. I thought I knew how to set axis properties but when I set the line width it sets the width of the box around the axis! Also I am trying to define the colours of each of the lines but it is just making the whole background of the plot blue. I think there must be another layer of handles I'm missing? I've only tried this in the first plot of a subplot.
% %define colours
% colorspec=[0 0 1];[0 1 1];[1 0 0];[1 0 1];[0 0.6 0.4];[1 0.4 0];[0.6 0 0.8];[0 1 0.4];[0 0 0.4];[0.8 0 0.2];[1 0.6 0.2];[0 0.6 1];[0 0.2 0];[0.4 0 0.6];[1 0.6 1];[0.8 1 0.4];[0.8 0.6 0.2];[1 0.8 0];[0.6 0 0.2];[0.6 0.6 0.8];[0.2 0.2 0.4];
%
%
% %Both 2010 and 2011 July velocities
% subplot(2,2,1)
% %horizontal velocities sigma 2010 July
%
% [haxes,hline1,hline2]=plotyy(hvel_July_2010_sigma(2:8,1),hvel_July_2010_sigma(2:8,3:22),hvel_July_2010_sigma(2:8,1),hvel_July_2010_sigma(2:8,27));
% axes(haxes(1))
% ylabel('Horizontal velocity (\sigma)');
% xlim([212 218]);
% set(haxes(1),'XTick',[212:218]);
% set(haxes(1),'XTickLabel',{'31/07/10','01/08/10','02/08/10','03/08/10','04/08/10','05/08/10','06/08/10'});
% set(haxes(1),'LineWidth',2);
% set(haxes(1),'Color',colorspec);
% %set(haxes(1),'YTick',[-2 -1 0 1 2]);
% axes(haxes(2))
% ylabel('Discharge (m^3 s^-^1)');
% xlim([212 218]);
% set(haxes(2),'XTick',[212:218]);
% set(haxes(2),'XTickLabel',{'31/07/10','01/08/10','02/08/10','03/08/10','04/08/10','05/08/10','06/08/10'});
Any help with this much appreciated! I am working in Matlab 2010b. The data is attached.
Kind Regards, Cat

Accepted Answer

Mike Garrity
Mike Garrity on 28 Jul 2015
The call to plotyy returned 3 things:
[haxes,hline1,hline2] = plotyy( ...
The first (haxes) is an array of two axes handles. Setting properties on these change the axes. For example, these two calls:
set(haxes(1),'LineWidth',2);
set(haxes(1),'Color',colorspec);
set the box of the left axes to be linewidth=2 and the background of the left axes to be blue.
The other two things (hline1 & hline2) are the lines in the axes. Setting properties on these will change the lines. For example, if I change those two lines to this:
set(hline1,'LineWidth',2);
set(hline1,'Color',colorspec);
Then the line in the left axes will be linewidth=2 and blue.
Does that make sense?
A few other things though.
You've got code which sets the XLim and XTick values of your axes to be in the range of [212 218]. However, your XData is the range [-2 2]. Also, your XData values are not monotone. It is possible to use plotyy with nonmonotonic X values, but it's fairly unusual. Could you describe the plot you're trying to get here?
  2 Comments
Catriona
Catriona on 28 Jul 2015
Ahha, thanks Mike and dpb, that's excellent and very clear. It's obvious once you know! Don't worry about the ranges, there were a couple of the limits that weren't set correctly which I've fixed now. I don't mean all the lines to be the same colour though (is that what you mean about monotomic values?).
The plots are of glacier velocity data for specific points on the glacier (NL1, SL1, C1 etc) on different days.I would really like to set the colour of each line (which I am trying to do with colorspec but failing) so they are all different. The other subplots have different points so I want to then set their line colours too so the same points have the same colours in all subplots, so I can use one legend for all the subplots. Discharge is the river flow at the same time. The horizontal velocities are shown as the difference from average (hence why the units are sigma).
Many thanks, Cat
dpb
dpb on 28 Jul 2015
Edited: dpb on 28 Jul 2015
BTW, it's convenient with plotyy to
linkaxes('x')
so that changes in one are reflected in the other; otherwise you end up with possibly different scales and other funky visual artifacts. I almost always set(hAx(2),'xtick',[]) so only one set of ticks and tick labels will be visible; rendering otherwise every once in a while causes "jitter" that is unpleasing...
Oh, and another tip...use set on any HG object handle to see a list of all its properties and their default values or get to see current values of all...when you know something's got to be there but can't spell or remember the property name or just are looking for how to to something, it's an invaluable aid.

Sign in to comment.

More Answers (1)

dpb
dpb on 28 Jul 2015
The line properties are associated with the line handles, not those of the parent object. In plotyy, they are returned in the two second optional return variables, that you returned has hline1, hline2 for the left- and right-hand axes, respectively. Set the properties for those as desired.

Categories

Find more on Two y-axis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!