MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Thread Subject: Inser text in graph

 Subject: Inser text in graph From: Sanaa Date: 5 Jun, 2012 14:53:09 Message: 1 of 13 Hi, I am having one figure which shows there different solutions in three different colors. I want to add a text in the upper right corner of the figure with the same colors. Any help will be appreciated.
 Subject: Inser text in graph From: Nasser M. Abbasi Date: 5 Jun, 2012 14:55:39 Message: 2 of 13 On 6/5/2012 9:53 AM, Sanaa wrote: > Hi, > I am having one figure which shows there different solutions in three >different colors. I want to add a text in the upper right corner of >the figure with the same colors. Any help will be appreciated. You can use legend(), or if you want to put the text yourself you can use text() "text Text annotation.      text(X,Y,'string') adds the text in the quotes to location (X,Y)      on the current axes," " legend Display legend.      legend(string1,string2,string3, ...) puts a legend on the current plot      using the specified strings as labels"
 Subject: Inser text in graph From: Sanaa Date: 5 Jun, 2012 15:13:07 Message: 3 of 13 Thanks a lot for your help. But I need the strings appears with the same corresponding colors!! Thanks a lot in advance
 Subject: Inser text in graph From: Nasser M. Abbasi Date: 5 Jun, 2012 21:50:41 Message: 4 of 13 On 6/5/2012 10:13 AM, Sanaa wrote: > Thanks a lot for your help. But I need the strings appears with > the same corresponding colors!! > Thanks a lot in advance But that what legend does. Have you tried it? x = 0:pi/100:2*pi; plot(x,sin(x),'r',x,cos(x),'k'); legend('sin(x)','cos(x)'); screen shot: http://12000.org/tmp/060512/legend_example.png So, I have no idea what is the problem you are having. --Nasser
 Subject: Inser text in graph From: Sanaa Date: 5 Jun, 2012 22:18:07 Message: 5 of 13 Thanks a lot for your help. I will explain more. I have to plot the trajectories of a map with three different parameters. So, I am having the code % define the vector of values that r takes r_vec = [0.4, 07, 1.5]; % define the vector of colors, to plot the dat aof reach value of r in % different color color_vec = ['r', 'b', 'k']; % do a loop of values of r for j = 1 : size(r_vec,2) % define delay r = r_vec(j); % define coefficient rho rho = 0.5; % define the number of deiscrete times in interval [k*r, (k+1)*r] n = 50; % define the nimber of iterations k = 1,.....,Nit Nit = 10; % define the initial vector which is a vector of size n x0 = ones(1,n); % define vectors x_next and x_previous x_next = zeros(1,n); x_previous = zeros(1,n); time = zeros(1,n); % initialize x_previous x_previous = x0; x_next = x_previous; time = linspace(0,r,n);        % set the figure details     figure(1)     xlabel('t')     ylabel('x')     plot(time,x_next,'.')     hold on for i = 1: Nit          x_next = 1 - rho*x_previous.^2;     x_previous = x_next;     time = linspace(i*r,(i+1)*r,n);               plot(time,x_next,'.', 'Color', char(color_vec(j)))      end         end hold off As you can see, I am having tow plot commands for just one ''r''. In the example you wrote you have sine and cos....I am confused
 Subject: Inser text in graph From: dpb Date: 5 Jun, 2012 23:55:13 Message: 6 of 13 On 6/5/2012 5:18 PM, Sanaa wrote: > Thanks a lot for your help. I will explain more. I have to plot the > trajectories of a map with three different parameters. So, I am having > the code >   r_vec = [0.4, 07, 1.5];   color_vec = ['r', 'b', 'k']; ... > > As you can see, I am having tow plot commands for just one ''r''. In the > example you wrote you have sine and cos....I am confused [lh,oh]=legend({'r=0.25';'r=0.5';'r=1'}); for ix=1:3,set(oh(ix*2),'color',color_vec(ix)),end By your drawing as you did, there are six object handles, the first of which is the text the 2nd,4th,6th are the line objects corresponding to the legend texts (you can find this out by exploring the objects' properties in the object handle vector via set()--look at 'type' for starters). --
 Subject: Inser text in graph From: Nasser M. Abbasi Date: 6 Jun, 2012 00:08:18 Message: 7 of 13 On 6/5/2012 5:18 PM, Sanaa wrote: > Thanks a lot for your help. I will explain more. I have to plot the trajectories of a map with three different parameters. So, I am having the code > > % define the vector of values that r takes > r_vec = [0.4, 07, 1.5]; > % define the vector of colors, to plot the dat aof reach value of r in > % different color > color_vec = ['r', 'b', 'k']; > > > % do a loop of values of r > for j = 1 : size(r_vec,2) > % define delay > r = r_vec(j); > % define coefficient rho > rho = 0.5; > % define the number of deiscrete times in interval [k*r, (k+1)*r] > n = 50; > % define the nimber of iterations k = 1,.....,Nit > Nit = 10; > % define the initial vector which is a vector of size n > x0 = ones(1,n); > % define vectors x_next and x_previous > x_next = zeros(1,n); > x_previous = zeros(1,n); > time = zeros(1,n); > > % initialize x_previous > x_previous = x0; > x_next = x_previous; > time = linspace(0,r,n); > > % set the figure details > figure(1) > xlabel('t') > ylabel('x') > plot(time,x_next,'.') > hold on > for i = 1: Nit > > x_next = 1 - rho*x_previous.^2; > x_previous = x_next; > time = linspace(i*r,(i+1)*r,n); > > > plot(time,x_next,'.', 'Color', char(color_vec(j))) > > end > > > end > hold off > > As you can see, I am having tow plot commands for just one ''r''. >In the example you wrote you have sine and cos....I am confused btw, I think your code above is not efficient. Try this, does the same for less than half the code. You define things you do not use. You create things not needed. NO need to put things inside a loop if they do not change. etc... --------------------------------------- clear all; close all; r_vec = [0.4, 7, 1.5]; annot = {'-ro','-.b','*k'}; rho = 0.5; n = 50; Nit = 10; figure(1); hold on for j = 1 : length(r_vec)            x_previous = ones(1,n);      x_next = x_previous;      time = linspace(0,r_vec(j),n);                  % plot(time,x_next,'.') %why?      for i = 1: Nit               x_next = 1 - rho*x_previous.^2;         x_previous = x_next;         time = linspace(i*r_vec(j),(i+1)*r_vec(j),n);         plot(time,x_next,char(annot(j)))      end end xlabel('t') ylabel('x') ---------------------------- I see you got an answer for the legend issue from dpb now, so I hope all is set now. --Nasser
 Subject: Inser text in graph From: Sanaa Date: 6 Jun, 2012 19:22:07 Message: 8 of 13 Thanks a lot for your modification. However, when I used legend I got one small problem which is: the values of the parameters are reversed that is, not in the same order as in the plot '' I mean their colors'' and one of them is written completely in that color. To see what I mean please run this code clear all; close all; r_vec = [0.25, 0.5, 1]; annot = {'r','b','k'}; rho = 0.5; n = 50; Nit = 10; figure(1); hold on for j = 1 : length(r_vec)            x_previous = ones(1,n);      x_next = x_previous;      time = linspace(0,r_vec(j),n);                  % plot(time,x_next,'.') %why?      for i = 1: Nit               x_next = 1 - rho*x_previous.^2;         x_previous = x_next;         time = linspace(i*r_vec(j),(i+1)*r_vec(j),n);         plot(time,x_next,char(annot(j)),'linewidth',3)      end end [lh,oh]=legend({'r=0.25';'r=0.5';'r=1'}); for ix=1:3,set(oh(ix*2),'color',annot(ix)),end xlabel('t') ylabel('x') Thanks a lot for your help
 Subject: Inser text in graph From: dpb Date: 6 Jun, 2012 19:40:24 Message: 9 of 13 On 6/6/2012 2:22 PM, Sanaa wrote: > Thanks a lot for your modification. However, when I used legend I got > one small problem which is: > the values of the parameters are reversed that is, not in the same order > as in the plot '' I mean their colors'' and one of them is written > completely in that color. To see what I mean please run this code ...  >> for ix=1:3,set(oh(ix*2),'color',color_vec(ix)),end Seems to work here...I get the above annotation w/ a red,blue,black line in that order from the top... Apparently there's a modification in the internals of legend() between versions; what does the above show you for the handles to the legend objects?  >> get(oh,'type') ans =      'text'      'line'      'line'      'line'      'line'      'line'      'line'  >> get(oh(2:end),'color');  >> ans{:} ans =       1 0 0 ans =       1 0 0 ans =       0 0 1 ans =       0 0 1 ans =       0 0 0 ans =       0 0 0  >> These are the color triplets for the lines; the first two are red, next blue, last two black. If they are indeed different, then you'll have to look into them and see which ones you need to change. --
 Subject: Inser text in graph From: dpb Date: 6 Jun, 2012 19:48:49 Message: 10 of 13 On 6/6/2012 2:40 PM, dpb wrote: ... > >> for ix=1:3,set(oh(ix*2),'color',color_vec(ix)),end > > Seems to work here...I get the above annotation w/ a red,blue,black line > in that order from the top... > ... That is, the legend is r=0.25 r=0.5 r=1 in that order w/ the lines respectively r, b, k and the text in black after executing the above set() loop --
 Subject: Inser text in graph From: Sanaa Date: 6 Jun, 2012 20:47:07 Message: 11 of 13 Thanks a lot for helping me. I have modified the code according to what Me. Nasser wrote. That is I don't have color_vec anymore, I am having annot instead, that's why I am getting the error ??? Error using ==> set Color value must be a 3 element numeric vector Error in ==> Trajectories_modified at 27 for ix=1:3,set(oh(ix*2),'color',annot(ix)),end The code is clear all; close all; r_vec = [0.25, 0.5, 1]; annot = {'r','b','k'}; rho = 0.5; n = 50; Nit = 10; figure(1); hold on for j = 1 : length(r_vec)            x_previous = ones(1,n);      x_next = x_previous;      time = linspace(0,r_vec(j),n);                  % plot(time,x_next,'.') %why?      for i = 1: Nit               x_next = 1 - rho*x_previous.^2;         x_previous = x_next;         time = linspace(i*r_vec(j),(i+1)*r_vec(j),n);         plot(time,x_next,char(annot(j)),'linewidth',3)      end end [lh,oh]=legend({'r=0.25';'r=0.5';'r=1'}); for ix=1:3,set(oh(ix*2),'color',annot(ix)),end %for ix=1:3,set(oh(ix*2),'color',color_vec(ix)),end xlabel('t') ylabel('x') Thanks a lot in advance.
 Subject: Inser text in graph From: dpb Date: 6 Jun, 2012 21:30:00 Message: 12 of 13 On 6/6/2012 3:47 PM, Sanaa wrote: > Thanks a lot for helping me. I have modified the code according to what > Me. Nasser wrote. That is I don't have color_vec anymore, I am having > annot instead, that's why I am getting the error > > ??? Error using ==> set > Color value must be a 3 element numeric vector > > Error in ==> Trajectories_modified at 27 > for ix=1:3,set(oh(ix*2),'color',annot(ix)),end > > The code is > > clear all; close all; > r_vec = [0.25, 0.5, 1]; > annot = {'r','b','k'}; ... Well, that's because you're passing a cell-string to set() not a character. I don't know why you would change, but if you're going to use a cell string instead of a character string, you'll have to dereference it w/ the curly brackets instead of passing the cell. --
 Subject: Inser text in graph From: dpb Date: 6 Jun, 2012 23:17:13 Message: 13 of 13 On 6/6/2012 4:30 PM, dpb wrote: ... > ... if you're going to use > a cell string instead of a character string, you'll have to dereference > it w/ the curly brackets instead of passing the cell. Or, that's why there's the char() casting function in the other place you've used it; but imo writing the {} is simpler altho results in same thing. But, raises the question of why? if all you're using it for is to get the character that is required anyway, why not simply use a character string instead and eliminate the need for the cast? --

## Tags for this Thread

No tags are associated with this thread.

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us