Two graphs in one plot with own RGB hex or decimal code

Hi, I would like to set my own RGB color to my graphs. I did it with single plots, this works, but I can't get it working with two graphs in one plot and own color codes. How? Here is my single plot code:
% figure(6);
plot(f,Yr,...
'LineWidth',1,...
'color', [0/255 177/255 175/255])
xlim([20e6 30e6])
%ylim([4 5])
grid on
title('Admittance Y_r')
xlabel('f[Hz]')
ylabel('Yr(f)')
And here is the plot I would like to set my own RGB color code as well, but doesn't matter how I change the syntax, it doesn't work on the same principle like in the single graph plot.
figure(7);
plot(f,Bn,'green',f,Gn,'black',...
'LineWidth',1)
xlim([20e6 30e6])
%ylim([4 5])
grid on
title('Conductance Gn and susceptance Bn')
xlabel('f[Hz]')
ylabel('Bn and Gn')
Can somebody help me please. I googled for so long, but all "solutions" failed. I'm using MATLAB R2017a with a student license.
best regards Jogibaer

2 件のコメント

Adam
Adam 2017 年 6 月 13 日
What exactly is wrong? When I run code similar to yours I get a green plot and a black plot, as expected. I don't have your exact data so I made some up, but that should be irrelevant.
Jogibaer
Jogibaer 2017 年 6 月 13 日
Like I wrote above, I would like to add my own RGB color code for each graph. Concerning the second code, green should be replaced with 0/255 177/255 175/255, the second graph should stay black, but I don't know how to write the syntax to do so.

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

回答 (1 件)

Adam
Adam 2017 年 6 月 14 日

0 投票

You'd have to plot them in two instructions to do that, e.g.
plot( hAxes, x1, y1, 'color', [0 0.5 0] );
hold( hAxes, 'on' );
plot( hAxes, x2, y2, 'color', [0 0 0.5] );

5 件のコメント

Jogibaer
Jogibaer 2017 年 6 月 14 日
Thank you for your reply, but this doesn't work, or I made a mistake. I got an error message concerning second line of this code. Here is how I wrote it:
figure(7);
plot( hAxes, f, Bn, 'color', [0 0.5 0]);
hold( hAxes, 'on' );
plot( hAxes, f, Gn, 'color', [0 0 0.5]);
xlim([20e6 30e6])
%ylim([4 5])
grid on
title('Conductance Gn and susceptance Bn')
xlabel('f[Hz]')
ylabel('Bn and Gn')
Steven Lord
Steven Lord 2017 年 6 月 14 日
What is the full text of the error message you received (all the text displayed in red)?
Adam
Adam 2017 年 6 月 14 日
編集済み: Adam 2017 年 6 月 14 日
You need to define hAxes as e.g.
figure(7); hAxes = gca;
...
I forgot to mention that, but whenever I give help regarding plotting I always make a point of including the axes handle argument because you should get used to always using this. In your case it isn't defined.
Relying on the current axes is not robust. I do use gca in the example snippet above, which is slightly more lazy than to be totally explicit, but I only use this when the instruction is on a single line like that where it is (virtually?) impossible that 'gca' could be any axes other than the one for the current figure.
Obviously if you have multiple axes you would deal with these differently and define exactly which one to plot on.
Alternatively you can just remove the hAxes argument from all those plot and hold statements, but I don't recommend this approach.
Jogibaer
Jogibaer 2017 年 6 月 14 日
編集済み: Jogibaer 2017 年 6 月 14 日
Thank you very much, now it looks like how I imagined. I had no idea of handles and how to use them in MATLAB, good to know. ^^ For completion, my final plot code:
figure(7);
hAxes = gca;
plot( hAxes, f, Bn,...
'LineWidth',1,...
'color', [0/255 177/255 175/255]);
hold( hAxes, 'on' );
plot( hAxes, f, Gn,...
'LineWidth',1,...
'color', [0/255 0/255 0/255]);
xlim([20e6 30e6])
%ylim([4 5])
grid on
title('Conductance Gn and susceptance Bn')
xlabel('f[Hz]')
ylabel('Bn and Gn')
The image :)
Adam
Adam 2017 年 6 月 15 日
It is very useful to get used to graphic object handles in Matlab and it is more intuitive now that they are proper visible objects. Anything you want to change on a plot you can just do so via the handle that you keep hold of (likewise axes etc) and you can play around with settings to see what you want by using, for example
inspect( hAxes )
and changing settings on there as you please. Then you can put them in code when you are happy. Also just typing
hAxes
on the command line will show you all the properties that you can edit.

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

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

質問済み:

2017 年 6 月 13 日

コメント済み:

2017 年 6 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by