Why does ycolor also color the xaxis?

7 views (last 30 days)
After using plotyy, the ycolor command turns the left yaxis blue, right yaxis red and the xaxis red. I want xaxis to remain black. Using xcolor has no effect.
set(gca,{'ycolor'},{'b';'r'})

Accepted Answer

Dave B
Dave B on 1 Feb 2022
With plotyy, you can use the output arguments to get access to the 'right' axes. Setting the property on gca would just affect the left axis (and I'd have expected an error with the syntax you provided):
ax=plotyy(1:10,1:10,1:10,ones(1,10));
set(ax(1),'YColor','b');
set(ax(2),'YColor','r');
Note that as you'll see on the plotyy documentation page, plotyy is not recommended, and we suggest you use yyaxis instead. Here is the equivalent code using yyaxis:
figure
yyaxis left
plot(1:10,1:10);
set(gca,'YColor','b')
yyaxis right
plot(1:10,ones(1,10));
set(gca,'YColor','r')
  3 Comments
Dave B
Dave B on 2 Feb 2022
Ah I didn't get it from your initial question, but I see now...this is an awkward interaction of plotyy and bar.
What you're seeing is the red 'baseline' running over the top of the black XColor.
Bar charts have a BaseLine, which is often the bottom of your y axis, but doesn't have to be that way. The baseline takes on the color of the associated y axis, because it sort-of acts as a big YTick. In a normal (not-plotyy) Bar chart, the baseline would end up underneath the x-axis, so you wouldn't see it. But in the plotyy case, the right baseline ends up on top of the x-axis associated with the left. Of course, all of this is why we made yyaxis, so we could avoid the 'trick' of stacking two axes on top of eachother and all the problems it causes.
In any case, you can set the baseline color directly (but I hope you'll use yyaxis instead!):
x1=1:10;y4=1:10;y5=2:11;
[Y4,H4,H5]=plotyy(x1,y4,x1,y5,'bar','bar');
set(Y4(1),'YColor','b');
set(Y4(2),'YColor','r');
set(H5.BaseLine,'Color','k');
Derek Neal
Derek Neal on 4 Feb 2022
Edited: Derek Neal on 4 Feb 2022
Fantastic. Thank you Dave! Yes, I decided to use yyaxis.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!