I have multiple figures open, and I want to update them independently during runtime. The following toy example should clarify my intention:
clf;
figure('name', 'a and b'); % a and b should be plotted to this window
hold on;
ylim([-100, 100]);
figure('name', 'c'); % only c should be plotted to this window
a = 0;
b = [];
for i = 1:100
a = a + 1;
b = [b, -i];
c = b;
xlim([0, i]);
plot(i, a, 'o');
plot(i, b(i), '.r');
drawnow;
end
The problem here is that when I open the second figure, I cannot tell the plot functions to plot to the first one instead of the second (and only c should be plotted to the second).
You can use something like
figure(1)
plot(x,y) % this will go on figure 1
figure(2)
plot(z,w) % this will go on another figure
The command will also set the figure visible and on top of everything.
You can switch back and forth between the figures as necessary by issuing the same figure command. Alternatively, you can use the handle to the figure as well:
h=figure(...)
and then issue figure(h) instead of using numeric indices. With this syntax, you can also prevent the figure from popping up on top by using
set(0,'CurrentFigure',h)
You can specify the axes-object in the plot-command. See here:
http://www.mathworks.de/help/techdoc/ref/plot.html
So, open a figure, insert the axes, save the id of the axes object, and then plot into it:
figure
hAx1 = axes;
plot(hAx1, 1, 1, '*r')
hold on
figure
hAx2 = axes;
plot(hAx2, 2, 1, '*r')
hold on
plot(hAx2, 3, 4, '*b')
plot(hAx1, 3, 3, '*b')
Alternatively, you can use gca instead of creating the axes object yourself (because it's automatically created within the actual figure, when it doesn't exist!)
figure
plot(1,1)
hAx1 = gca;
hold on
figure
plot(2,2)
plot(hAx1, 3, 3)
See the following hierarchy representing the relationship between figures and axes
From http://www.mathworks.de/help/techdoc/learn_matlab/f3-15974.html.
Related
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r');
plot(t, c, 'b');
plot(t, m, 'g');
hold off;
legend('', 'cosine', '');
There are several curves in my plotting. I want to display legend for only some of them. How do I do it?
For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.
I do not like storing the handle values, it becomes a mess when I have a lot of graphs in my figures. Therefore i found another solution.
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
h2 = plot(t, c, 'b', 'DisplayName', 'cosine'); % Plotting and giving legend name
plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
legend show % Generating legend based on already submitted values
This give me the same graph as shown in Eitan T's answer.
It should be noted that this will affect other matlab functions also, for example will cla only remove the plots mentioned on the legend. Search for HandleVisibility in the Matlab documentation for more about that.
Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:
hold on;
plot(t, s, 'r');
h2 = plot(t, c, 'b'); % # Storing only the desired handle
plot(t, m, 'g');
hold off;
legend(h2, 'cosine'); % # Passing only the desired handle
You should get this plot:
Let's start with your variables and plot them:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
figure;
hold ('all');
hs = plot(t, s);
hc = plot(t, c);
hm = plot(t, m);
There is a property called IconDisplayStyle. It is buried quite deep. The path you need to follow is:
Line -> Annotation -> LegendInformation -> IconDisplayStyle
Setting the IconDisplayStyle property off will let you skip that line. As an example, I am going to turn off hs's legend.
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
Of course you can go ahead and do it like this:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
But I find it much harder to understand.
Now, the legend function will just skip hs.
Ending my code with this:
legend('cosine', 'repeat for this handle')
will give you this:
EDIT: Jonas had a nice suggestion in the comments:
Setting the DisplayName property of hc like this:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
will give you the legend you need. You will have associated your line handle with 'cosine'. So, you can just call the legend with 'off' or 'show' parameters.
You could just change the order in wich the curves are plotted and apply the legend to the first curve:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
plot(t,c,t,s,t,m) % cosine is plotted FIRST
legend('cosine') % legend for the FIRST element
if i'd want to put in a legend for cosine and -sine:
plot(t,c,t,m,t,s) % cosine and -sine are first and second curves
legend('cosine', '-sine')
To expand Sebastian's answer, I have a special case where I'm plotting several lines in one of two formats (truss beams either in compression or tension) and was able to plot specific plot handles in the legend as long as the labels were the same length
for ii=1:nBeams
if X(ii)<0 %Bars with negative force are in compession
h1=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'r:');
elseif X(ii)>0 %Bars with positive force are in tension
h2=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'b');
end
end
legend([h1;h2],['Compression';'Tension ']);
Where 4 spaces have been added behind 'Tension' so that the number of characters is consistent.
Quick in-plot hack:
Cut everything you don't want to appear in the legend
Apply legend
Paste
I am trying to learn how the graphic objects work in MATLAB. I tried to create a plot without using the plot function but I am confused why it is not working.
AFIK, when I use the plot function it creates figure, axis, line objects and then sets the property of each object accordingly. I tried to do so but all I'm getting is a white/blank figure.
I'm trying to plot a sine wave so my X and Y data are:
x = 0:0.1:2*pi;
y = sin(x);
This is my main code:
figH = figure();
axis([-2, 2, -2, 2]);
lineH = findobj(figH, 'type', 'line');
set(lineH, 'XData', x,...
'YData', y,...
'Color', 'r');
The weird thing is that when I type
get(lineH)
I'm not getting anything back. I appreciate tips and comments.
You need to create the line before you can find it and change a property.
e.g.
hLine = line ( x, y, .... );
% Then you can modify the properties, i.e.
set ( hLine, 'XData', x );
% etc...
Edit:
Its a good idea to create and store each of your objects directly (rather than allowing the command to find the appropriate figure, axes etc....)
hFig = figure;
hAx = axes ( 'parent', hFig );
hLine = line ( hAx, x, y, .... );
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r');
plot(t, c, 'b');
plot(t, m, 'g');
hold off;
legend('', 'cosine', '');
There are several curves in my plotting. I want to display legend for only some of them. How do I do it?
For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.
I do not like storing the handle values, it becomes a mess when I have a lot of graphs in my figures. Therefore i found another solution.
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
h2 = plot(t, c, 'b', 'DisplayName', 'cosine'); % Plotting and giving legend name
plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
legend show % Generating legend based on already submitted values
This give me the same graph as shown in Eitan T's answer.
It should be noted that this will affect other matlab functions also, for example will cla only remove the plots mentioned on the legend. Search for HandleVisibility in the Matlab documentation for more about that.
Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:
hold on;
plot(t, s, 'r');
h2 = plot(t, c, 'b'); % # Storing only the desired handle
plot(t, m, 'g');
hold off;
legend(h2, 'cosine'); % # Passing only the desired handle
You should get this plot:
Let's start with your variables and plot them:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
figure;
hold ('all');
hs = plot(t, s);
hc = plot(t, c);
hm = plot(t, m);
There is a property called IconDisplayStyle. It is buried quite deep. The path you need to follow is:
Line -> Annotation -> LegendInformation -> IconDisplayStyle
Setting the IconDisplayStyle property off will let you skip that line. As an example, I am going to turn off hs's legend.
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
Of course you can go ahead and do it like this:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
But I find it much harder to understand.
Now, the legend function will just skip hs.
Ending my code with this:
legend('cosine', 'repeat for this handle')
will give you this:
EDIT: Jonas had a nice suggestion in the comments:
Setting the DisplayName property of hc like this:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
will give you the legend you need. You will have associated your line handle with 'cosine'. So, you can just call the legend with 'off' or 'show' parameters.
You could just change the order in wich the curves are plotted and apply the legend to the first curve:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
plot(t,c,t,s,t,m) % cosine is plotted FIRST
legend('cosine') % legend for the FIRST element
if i'd want to put in a legend for cosine and -sine:
plot(t,c,t,m,t,s) % cosine and -sine are first and second curves
legend('cosine', '-sine')
To expand Sebastian's answer, I have a special case where I'm plotting several lines in one of two formats (truss beams either in compression or tension) and was able to plot specific plot handles in the legend as long as the labels were the same length
for ii=1:nBeams
if X(ii)<0 %Bars with negative force are in compession
h1=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'r:');
elseif X(ii)>0 %Bars with positive force are in tension
h2=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'b');
end
end
legend([h1;h2],['Compression';'Tension ']);
Where 4 spaces have been added behind 'Tension' so that the number of characters is consistent.
Quick in-plot hack:
Cut everything you don't want to appear in the legend
Apply legend
Paste
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r');
plot(t, c, 'b');
plot(t, m, 'g');
hold off;
legend('', 'cosine', '');
There are several curves in my plotting. I want to display legend for only some of them. How do I do it?
For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.
I do not like storing the handle values, it becomes a mess when I have a lot of graphs in my figures. Therefore i found another solution.
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
hold on;
plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
h2 = plot(t, c, 'b', 'DisplayName', 'cosine'); % Plotting and giving legend name
plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle
legend show % Generating legend based on already submitted values
This give me the same graph as shown in Eitan T's answer.
It should be noted that this will affect other matlab functions also, for example will cla only remove the plots mentioned on the legend. Search for HandleVisibility in the Matlab documentation for more about that.
Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:
hold on;
plot(t, s, 'r');
h2 = plot(t, c, 'b'); % # Storing only the desired handle
plot(t, m, 'g');
hold off;
legend(h2, 'cosine'); % # Passing only the desired handle
You should get this plot:
Let's start with your variables and plot them:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
figure;
hold ('all');
hs = plot(t, s);
hc = plot(t, c);
hm = plot(t, m);
There is a property called IconDisplayStyle. It is buried quite deep. The path you need to follow is:
Line -> Annotation -> LegendInformation -> IconDisplayStyle
Setting the IconDisplayStyle property off will let you skip that line. As an example, I am going to turn off hs's legend.
hsAnno = get(hs, 'Annotation');
hsLegend = get(hsAnno, 'LegendInformation');
set(hsLegend, 'IconDisplayStyle', 'off');
Of course you can go ahead and do it like this:
set(get(get(hs, 'Annotation'), 'LegendInformation'), 'IconDisplayStyle', 'off');
But I find it much harder to understand.
Now, the legend function will just skip hs.
Ending my code with this:
legend('cosine', 'repeat for this handle')
will give you this:
EDIT: Jonas had a nice suggestion in the comments:
Setting the DisplayName property of hc like this:
set(hc, 'DisplayName', 'cosine');
legend(gca, 'show');
will give you the legend you need. You will have associated your line handle with 'cosine'. So, you can just call the legend with 'off' or 'show' parameters.
You could just change the order in wich the curves are plotted and apply the legend to the first curve:
t = 0 : 0.01 : 2 * pi;
s = sin(t);
c = cos(t);
m = -sin(t);
plot(t,c,t,s,t,m) % cosine is plotted FIRST
legend('cosine') % legend for the FIRST element
if i'd want to put in a legend for cosine and -sine:
plot(t,c,t,m,t,s) % cosine and -sine are first and second curves
legend('cosine', '-sine')
To expand Sebastian's answer, I have a special case where I'm plotting several lines in one of two formats (truss beams either in compression or tension) and was able to plot specific plot handles in the legend as long as the labels were the same length
for ii=1:nBeams
if X(ii)<0 %Bars with negative force are in compession
h1=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'r:');
elseif X(ii)>0 %Bars with positive force are in tension
h2=plot(linspace(beamCord(ii,1),beamCord(ii,3)),...
linspace(beamCord(ii,2),beamCord(ii,4)),'b');
end
end
legend([h1;h2],['Compression';'Tension ']);
Where 4 spaces have been added behind 'Tension' so that the number of characters is consistent.
Quick in-plot hack:
Cut everything you don't want to appear in the legend
Apply legend
Paste
Trying to engineer the following in Matlab:
** loop start;
y(:,i) = function of x;
z(:,i) = function of x;
plot(x,y(:,i)) on figure 1, hold all;
plot(x,z(:,i)) on figure 2, hold all;
** loop end;
add title, legend, etc for figure 1 (NB: we have multiple lines);
add title, legend, ets for figure 2 (NB: same, have multiple lines for the legend);`
Tried multiple combinations without much luck. Managed to get 2 figures but only the 2-nd displays multiple lines, not the first. And can't figure how to add legends to these 2 correctly.
Save a handle to each figure, and to each axis object:
fh1 = figure;
hold all;
ah1 = gca;
fh2 = figure;
hold all;
ah2 = gca;
for i=1:N
y(:,i) = function of x;
z(:,i) = function of x;
plot(ah1, x, y(:,i)); %# tell it which axis to use (ah1)
plot(ah2, x, z(:,i)); %# (ah2)
end
legend(ah1, ...) %# legend options here
legend(ah2, ...) %# and the other legend
%# note: you can set figure properties for each using fh1, fh2 handles.
You can do this:
figHandle1 = figure(1);
figHandle2 = figure(2);
Then when you want to plot on that figure do this:
figure(figHandle1) %Plot on figure 1
%ie plot(someStuff)
figure(figHandle2) %Plot on figure 2
Also its the same for the title and stuff, you jjust need to identify which figure by doing:
figure(handle);
Hope this helps.