Why does ycolor also color the xaxis?

3 ビュー (過去 30 日間)
Derek Neal
Derek Neal 2022 年 2 月 1 日
編集済み: Derek Neal 2022 年 2 月 4 日
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'})

採用された回答

Dave B
Dave B 2022 年 2 月 1 日
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 件のコメント
Dave B
Dave B 2022 年 2 月 2 日
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 2022 年 2 月 4 日
編集済み: Derek Neal 2022 年 2 月 4 日
Fantastic. Thank you Dave! Yes, I decided to use yyaxis.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTwo y-axis についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by