MATLAB Answers

how to change the color of the coordinate in Plotyy?

11 views (last 30 days)
Abdulaziz Abutunis
Abdulaziz Abutunis on 9 Dec 2015
Answered: Abdulaziz Abutunis on 28 Dec 2015
Hi all, Please is there any way to change the color of y coordinate in plotyy. Also can I place the plotyy in for loop. I wan to add more curves to the plot.
Thank you Aziz

  1 Comment

Walter Roberson
Walter Roberson on 23 Dec 2015
Is the question about the color of the lines that are drawn, or about the colors of the labels along the axes?
Note that plotyy is for the case where you need separate labeling for the ticks, such as if you were labeling a plot with frequency along the left and wavelength along the right. Because you run out of sides to label, you cannot just keep adding more axes. You might want to look at the File Exchange contribution plotyyy but more likely you just need regular plot() and legend()

Sign in to comment.

Answers (2)

Vidya Viswanathan
Vidya Viswanathan on 23 Dec 2015
It is possible to change the color of the axes as well as the data points in a plot. The plot could have been generated using any of the functions used for creating graphs, such as, "plot", "plotyy", etc. As long as the axes handle is available, we can alter the properties of the plot programmatically. Consider the following code snippet that shows how "plotyy" can be used to plot multiple signals and how the color of the two axes can be altered:
%%Plots sine and cosine signals of two different frequencies on the same figure
freq=.1:0.1:.2; % defines two different frequencies
time_idx=0:0.01:10; % defines the time indices for the signals
figure;
colors=['r','b']; % defines two colors (one corresponding to sine signal and the other for cosine signal)
for i=1:2
x=sin(2*pi*freq(i).*time_idx); % generates the sine signal
y=2.*cos(2*pi*freq(i).*time_idx); % generates the cosine signal
[Hax,Hl1,Hl2]=plotyy(time_idx,x,time_idx,y) % Hax contains both the axes handles,
%Hl1 has the handle for the sine plot,
%Hl2 has the handle for cosine plot
hold on % To display the plots corresponding to second frequency as well in the same figure
Hax(1).YColor='r'; % Alters the color of the first axis
Hax(1).YLabel.String='Sine plot'; % Adds a label to the first axis
Hax(2).YColor='b'; % Alters the color of the second axis
Hax(2).YLabel.String='Cosine plot'; % Adds a label to the second axis
Hl1.Color=colors(1); % Changes the color of the sine plot
Hl2.Color=colors(2); % Changes the color of the cosine plot
end
As an alternative, the properties of the figure can also be modified interactively from the figure window. In the "Figure" window, click on "View" and select "Plot Browser". The properties of the axes can be modified by double-clicking the corresponding axis. This opens the "Property Editor" window.
I hope this answers your query.

  0 Comments

Sign in to comment.


Abdulaziz Abutunis
Abdulaziz Abutunis on 28 Dec 2015
Thank you Walter and Vidya for your responses.
Vidya, I have tried your code snippet and the figure did not show any plotting. This code be due to the MATLAB version I have (2010). I will try it later on a new version.
Thanks again

  0 Comments

Sign in to comment.

Sign in to answer this question.