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
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 have to plot 1 line plot and 3 grouped scatter plots in a single plot window.
The following is the code I tried,
figure;
t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);
plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','scatter');
%plotyy(t1,X,[ts',ts',ts'],[Y1,Y2,Y3],'plot','plot');
The following are my questions,
The above code works if I replace 'scatter' by 'plot' (see commented out line), but 'scatter' works only for 1 data set and not for 3. Why?
How to individually assign colors to the 3 grouped scatter plots or plots?
Read the error message you're given:
Error using scatter (line 44) X and Y must be vectors of the same
length.
If you look at the documentation for scatter you'll see that the inputs must be vectors and you're attempting to pass arrays.
One option is to stack the vectors:
plotyy(t1,X,[ts';ts';ts'],[Y1;Y2;Y3],'plot','scatter');
But I don't know if this is what you're looking for, it certainly doesn't look like the commented line. You'll have to clarify what you want the final plot to look like.
As for the second question, I would honestly recommend not using plotyy. I may be biased but I've found it far to finicky for my tastes. The method I like to use is to stack multiple axes and plot to each one. This gives me full control over all of my graphics objects and plots.
For example:
t1=0:0.1:10;
X = 2*sin(t1);
ts = 0:1:10;
Y1 = randi([0 1],length(ts),1);
Y2 = randi([0 1],length(ts),1);
Y3 = randi([0 1],length(ts),1);
% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');
% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');
% Plot data
h.plot(1) = plot(h.ax1, t1, X);
h.scatter(1) = scatter(h.ax2, ts', Y1);
h.scatter(2) = scatter(h.ax2, ts', Y2);
h.scatter(3) = scatter(h.ax2, ts', Y3);
Gives you:
And now you have full control over all of the axes and line properties. Note that this assumes you have R2014b or newer in order to use the dot notation for accessing the Position property of h.ax1. If you are running an older version you can use get(h.ax1, 'Position') instead.
I'm new to MATLAB, and I've been searching around for what I'm trying to do, but the results don't fit quite well.
I'm graphing plots of variations of transfer functions, the code I've done is below:
omega = 3;
K = omega * omega;
for zeta = 0.1:0.1:2
sys = tf(K,[1 2*zeta*omega omega]);
figure();
subplot(1,2,1);
step(sys);
title('Step response');
[num,den] = tfdata(sys, 'v');
disp(den);
r = roots(den);
subplot(1,2,2);
%hold (subplot(1,2,2), 'on');
plot(real(r), imag(r), 'o');
title('Pole Locations in Complex Plane');
end
Each time the loop runs, it will create a new figure. The first subplot should be unique for every figure, but the second subplot should plot the accumulation of all points (roots of denominator of all transfer functions) from figures before it. I tried to use hold (subplot(1,2,2), 'on'); to keep the second subplot, but it didn't work. My thought is that because the subplots are different figures, hold on cannot be used.
How can I solve this problem? Any help will be great.
A solution is to use 'Tag' in your subplot. I am using your code to edit:
omega = 3;
K = omega * omega;
for zeta = 0.1:0.1:2
sys = tf(K,[1 2*zeta*omega omega]);
figure();
sb = subplot(1,2,1);
set(sb, 'Tag', 'daddy') % Something to tag with - memorable
step(sys);
title('Step response');
[num,den] = tfdata(sys, 'v');
disp(den);
r = roots(den);
sb = subplot(1,2,2);
set(sb, 'Tag', 'child')
sb = findobj('Tag','child'); % Use MATLAB methods to find your tagged obj
set(sb,'NextPlot','add'); % set 'NextPlot' property to 'add'
plot(real(r), imag(r), 'o');
title('Pole Locations in Complex Plane');
end
DOes this work for you? btw. This is also in MATLAB central. You should use that too.
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 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.