Line colour of two y-axis plot

10 ビュー (過去 30 日間)
Wah On Ho
Wah On Ho 2019 年 12 月 23 日
コメント済み: Adam Danz 2019 年 12 月 23 日
Hi All,
I am plotting multiple lines for y-axis 1 and another line as y-axis 2 using the yyaxis command. If I plot only the first set of data as a single plot with one y-axis, the lines are plotted with different colours. But when I plot using yyaxis left for one set of data, and plot another line for yyaxis left, the data plotted against the first y-axis comes out all the same colour which is not what I want. From below, if I only run 'plot(xdata,y1data)' I get 10 lines all different colours. But if I run from 'yyaxis left' to the end I get 10 lines of the same colour (with symbols which I don't want), and one line of a different colour plotted against the second y-axis. How do I get the multi-colour lines back for the first plot? Thanks.
y1data = magic(10);
y2data = randi([-10 10],10,1);
xdata = [1:10]';
yyaxis left
plot(xdata,y1data);
yyaxis right
plot(xdata, y2data);
  2 件のコメント
Star Strider
Star Strider 2019 年 12 月 23 日
That may not be possible. The whole point of assigning one colour to one y-axls and another colour to the other y-axis is to remove any ambiguity about which y-axis scale the lines refer to.
If you want them in different colours, you will have to plot them in different figures.
Wah On Ho
Wah On Ho 2019 年 12 月 23 日
OK. What I have is a set of data from multiple sensor outputs to be plotted against the left y-axis. I also have a set of temperature readings to be plotted aganist the right y-axis. Together it would show how a set of sensors were responding as the temperature varied, of course the variations from sensors and temperature are on different scales of response, hence my need for two y-axes plots.
Yes I could have plotted the sensor data individually and then added the temperature to the 2nd y-axis. I thought perhaps there was a short-cut but maybe not.
Thank you for considering my question.

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

採用された回答

Adam Danz
Adam Danz 2019 年 12 月 23 日
編集済み: Adam Danz 2019 年 12 月 23 日
Set the LineStyleOrder and ColorOrder properties after selecting the Left/Right yyaxes
y1data = magic(10);
y2data = randi([-10 10],10,1);
xdata = [1:10]';
yyaxis left
set(gca, 'LineStyleOrder', '-', 'ColorOrder', jet(10))
plot(xdata,y1data);
yyaxis right
set(gca, 'LineStyleOrder', '-', 'ColorOrder', jet(10))
plot(xdata, y2data);
You can replace the jet colormap with any other colormap or you can create your own colormap by using an nx3 matrix of RGB values.
You can replace the '-' with any line style or a cell array of line styles to cycle through.
  4 件のコメント
Wah On Ho
Wah On Ho 2019 年 12 月 23 日
Sorry, yes, I was too focussed on the code to look beyond it! Thanks again.
Adam Danz
Adam Danz 2019 年 12 月 23 日
Glad I could help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by