plotyy - how to change line width, colour and xaxis labels
17 views (last 30 days)
Show older comments
Hi all,
I am trying to use the plotyy function to plot a bar chart with a line graph on top. Generally I have managed to achieve what I was after but would also like to change the line thickness (the hLine.LineWidth = 3; doesn't seem to work), the colour (ditto with hLine.Color = [159/255 20/255 124/255; 70/255 32/255 112/255];) and the xaxis labels are displaying correctly but underneath are the values 1,2,3, etc. This is my code
figure
set(0,'DefaultAxesFontName', 'Aller')
[ax,hBar,hLine]=plotyy(1:13,[aircraft.median_2011;aircraft.median_2013]', ...
1:13,[aircraft.count_2011;aircraft.count_2013]', ...
'bar','semilogy')
legend('2011','2013')
ylabel(ax(1),'Joining Point (nm)')
ylabel(ax(2),'No. of Flights')
str = [aircraft.name];
set(gca, 'XTickLabel',str, 'XTick',1:numel(str));
barmap=[159/255 20/255 124/255; 70/255 32/255 112/255]; %[0.7 0.7 0.7] is grey, [ 0.05 .45 0.1] is green
colormap(barmap);
hLine.LineWidth = 3;
hLine.Color = [159/255 20/255 124/255; 70/255 32/255 112/255];
If anyone knows how to do any of these help would be very much appreciated.
Cheers
2 Comments
Robert Cumming
on 5 Jan 2015
what version of matlab?
any error message(s)?
Also - we dont have your aircraft variable to recreate your plots exactly.
Mike Garrity
on 6 Jan 2015
I think that it should have reported the following error
Incorrect number of right hand side elements in dot name assignment. Missing []
around left hand side is a likely cause.
That's not as helpful as it could be. It's really the left hand side that's the issue here.
Accepted Answer
dpb
on 5 Jan 2015
Edited: dpb
on 5 Jan 2015
I don't have the last release w/ the new graphics engine so I use the set solution...
set(hLine,'linewidth',3, ...
{'color'},mat2cell([159 20 124; 70 32 112]/255,[1 1 1], 3))
Note that must cast the color vectors into cell arrays (of dimension the size of the number of handles()) and use cell for the property name to handle multiple handles in a single call to *set.
For the xtick problems, never rely on gca when using plotyy or other complex graphics because the result is dependent upon order in which you do things...it is, as it says the "current" axes handle.
plotyy creates two axes as you've noted; it starts out with two overlaid x-axis lines drawn and labeled identically. Hence, it appears to be one, but there really are two.
What you need to do is to disable the display on one and then make any changes of scale and limits, etc., on both (or link them).
After creating the plot, then
set(ax(2),'xtick',[]) % hide ticks/labels on the RH x-axis
set(ax(1), 'XTickLabel',str, 'XTick',1:numel(str))
and all should be well...
(*) NB: a cell holding an array of size [2,3) will NOT work; it must be a cell array with the proper dimension for the array. That is, mat2cell(clr) will not cut it even though it seems it should superficially. I only finally figured this out within the last month after some 30 years of Matlab usage. I personally think TMW should enhance the syntax such that if the array is of the proper size it works as expected, but I suppose there's a valid reason for the more limited syntax.
0 Comments
More Answers (1)
Mike Garrity
on 6 Jan 2015
Because each of your two plots contains two series, the variable hLine is an array of two line objects. Unfortunately, dot notation doesn't work on arrays of objects yet. That means that you need to use set like dpb showed above.
0 Comments
See Also
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!