Customized Legend in Matlab - matlab

Here I have a for loop to plot the content of my matrix.
Based on 'type' value I determine the plot object (ks,bO,rX)
for k = 1:length(data1)
if(type(k)==1)
h=plot(data1(k,1),data1(k,2),'ks');set(h,'linewidth',2);hold on;
elseif(type(k) ==0)
h=plot(data1(k,1),data1(k,2),'bO');set(h,'linewidth',2); hold on;
else
h=plot(data1(k,1),data1(k,2),'rX');set(h,'linewidth',2); hold on;
end
end
I am little bit confused to find a way to put legend in my final figure, which shows my own explanation regarding each object(ks,bO,rX).

By default, MATLAB will see the output of this loop as not three plots but as many individual plotted points. Even though some of the points are plotted with the same settings, it doesn't automatically recognise them as part of the same series. If you give it three legend entries, it will assign them to whatever the first three points plotted were.
The simplest way around this is to change the way you plot and use logical indexing, rather than a loop:
h=plot(data1(type==1,1),data1(type==1,2),'ks'); set(h,'linewidth',2);
hold on;
h=plot(data1(type==0,1),data1(type==0,2),'bO'); set(h,'linewidth',2);
h=plot(data1(type==-1,1),data1(type==-1,2),'rX'); set(h,'linewidth',2);
Now we have only three plots, so giving legend the three should give us the correct match between those plots (in the order they were plotted) and our labels:
legend({'Type 1'; 'Type 0' ; 'Type -1'})

Related

Hide MATLAB legend entries for some graphical objects in plots

MATLAB legends list everything in a plot, including guidelines that you have put on a plot.
A fudge to get around that is to do
*Plot
*Add legend
*Add guidelines
However, MATLAB puts the most recent lines in the front, meaning the guidelines then sit over the displayed data; ugly and distracting.
Similar problems occur any time you build up a complicated plot, legend freaks out and grabs everything, and workarounds with plotting order can be ugly
Example code:
%**** Optional guidelines
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
%* Plot
figure(1)
plot(x,y(plotLoop,:));
hold on
end
%*****LEGEND
hLegend = legend(LegTxt,...
'interpreter','latex',...
'location','eastoutside')
(move the code block order to replicate the situations mentioned above)
How to reasonably fix this?
If you want a certain graphics object to not produce a legend (and that will work even if you toggle the legend off and on again), you can modify the LegendInformation:
%# plot something that shouldn't show up as legend
handleWithoutLegend = plot(something);
%# modify the LegendInformation of the Annotation-Property of the graphical object
set(get(get(handleWithoutLegend,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
%# toggle legend on and off at will, and never see the something-object appear
If you try to turn off the legend on an array of handles, the best way is just to loop over them, with a try-wrapper for graphical objects that cannot produce a legend:
for h = listOfHandles(:)'
try
set(get(get(h,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off');
end
end
Craft a custom handle that you feed into the legend. Plot handles can be concatenated to form an object that legend is happy to accept as input.
The required code isn't pretty, but it does work.
%**** Optional guidelines for periodicity
figure(1)
plot([2 2],[0,1],'k--'); hold on
%**** DATA
N = 4;
y=rand(5,N);
x=1:1:5;
for plotLoop=1:N;
LegTxt{plotLoop} = num2str(plotLoop);
%* Plot
figure(1)
% if statement to construct a handle for the legend later
if plotLoop==1
htot=plot(x,y(plotLoop,:));
else
h=plot(x,y(plotLoop,:));
% Append this info to the figure handle
htot= [htot, h];
end
hold on
end
%*****LEGEND
hLegend = legend(htot,LegTxt,...
'interpreter','latex','FontSize',16,...
'location','eastoutside')
For the pedantic or curious, the loop for plotLoop=1:N; is here because I extracted the example from some rather complex code where the data is extracted from cell arrays. Obviously you could eliminate that loop for a lot of usage scenarios, I just decided to keep the code in its most flexible format!
You can also hide plot from legend in another way. Here's the sample:
figure(1)
hold on
x=1:10;
y1=x;
y2=x.^2/10;
y3=x.^3/100;
plot(x,y1);
plot(x,y2,'HandleVisibility','off');
plot(x,y3);
legend('x','x^3')
You just need to put 'HandleVisibility', 'off' to your plot that you don't want to show up in legend. That's how result looks like:
HandleVisibility is a line property so it might now work if you create plot in some other way. But for most use cases its enough and it is much simpler.

Best way to plot marks on a line in Matlab

This question is quite basic, but I am looking for the best way to do this.
For a plotted line in matlab, how can a few points on the line be marked. I know it is possible to plot directly using marks. But, in case the line has been plotted and the marks are just to differentiate the plots. Instead of selecting a few points and plotting the second time. An example is shown in this figure below
Right after creating the initial plot you can use the command 'hold on' to keep that plot 'live' and then replot the points as markers. For example:
x=1:10;
y=2*x+4;
plot(x,y)
hold on
plot(x,y,'+')
You can use '*', '.' or any other marker instead of '+'.
If you want the markers to be evenly spaced (and your data is not ,originally) you may create an evenly spaced x vector and then, assuming your plot is not too extreme, interpolate the y values and add just the points. For example:
x=[1,1.5,2,2.3,3,4,4.8,5,6.1,6,7,8,9,10];
y=2*x.^2+4;
plot(x,y)
hold on
x_lin=linspace(min(x),max(x),20);
y_lin=interp1(x,y,x_lin,'linear');
plot(x_lin,y_lin,'+')
hold off
With the following result:
If 'linear' doesn't give a good enough result you can try other interpolation methods like 'cubic', 'spline'...
If you don't want to "add" the marked points in a second moment (but I don't think you will have some advantage), consider to
A) use two plot instructions, separating the array of points "to be marked" using
C = setdiff(A,B)
alternatively
B) plot every point in a for cycle with counter i under a condition
%not tested solution
c1 = '.r' %red point, if they are near they seems a line
c2 = '*b' %blue marker
if (marker_condition == true)
plot(x(i), y(i), c2)
else
plot(x(i), y(i), c1)
end

How can I multiple plot in one figure at Matlab?

Hi I'm trying to implement as following code.
plot(bins,r);
plot(bins,g);
plot(bins,b);
But I want to plot in one figure.
Is there any way?
For multiple plots in the same figure and not the same axis. You have to use subplot(x,y,z). The first argument 'x' is the number of plot you want to produce, in your case 3. Second 'y' just adjusts the size of the plots, you can use 1. The third 'z' is the position of the plot, whether a certain plot comes first, second or third.
subplot(3,1,1)
plot(bins,r);
subplot(3,1,2)
plot(bins,g);
subplot(3,1,3)
plot(bins,g);
To distinguish between all three plot you can add another argument to plot() so that you can change colors. For example:
plot(bins,r,'r')
'r' will make the color of the plot red, 'b' makes it blue, 'k' makes it black...so on.
Yes, you can plot everything in one go:
plot(bins,r,bins,g,bins,b)
or use hold on after the first call to plot.
You need to use hold on
hold on retains plots in the current axes so that new plots added to
the axes do not delete existing plots. New plots use the next colors
and line styles based on the ColorOrder and LineStyleOrder properties
of the axes. MATLAB® adjusts axes limits, tick marks, and tick labels
to display the full range of data.
hold on
plot(bins,r)
plot(bins,g)
plot(bins,b)

how to remove argument labels from polar plot in MATLAB

Does anyone know a simple way to remove the labels from the argument axis in a polar plot in MATLAB? My figures are small and I have to zoom in to see them, but when I do this the argument labels from one subplot get in the way of the other subplots.
Equally, if anyone knows of a way to solve this issue by rescaling the axis in the radial direction, or of any solution for that matter, then I would be very grateful. For the mean time I'll have to trim and re-size in Latex.
I tried the method here
How to remove Rho labels from Matlab polar plot?
but it didn't work.
here is some sample code
subplot(1,3,1,'align');
r = 10;
polar(t,abs((r.*exp(t.*1i) - (1+1i)).^2));
set(gca, 'fontsize', 20)
set(findall(gca, 'String', '0'),'String', ' ') %remove theta labels
hold on
Given the following example input:
t = 0:.01:2*pi;
polar(t,sin(2*t).*cos(2*t),'--r')
You can remove the vertical labels (along rho) by keeping only the legit ones.
% Find all handles to text labels
h = findall(gca,'type','text');
% Define what to keep
legit = {'0','30','60','90','120','150','180','210','240','270','300','330','360',''};
% Take the others and set them to empty string
idx = ~ismember(get(h,'string'),legit);
set(h(idx),'string','')
EDIT addressing comments
One approach would be simply setting to empty string '' those labels defined IN legit, i.e. by removing ~ near ismember. However, this approach is not robust in the sense that it will remove all of the labels that match. Suppose you have some other label with 120, then it will be removed as well.
One way to solve this issue is to edit polar() on line 155 and 162 by adding a 'Tag' to the radial annotations:
'HandleVisibility', 'off', 'Parent', cax,'Tag','spoke');
Then, save the edited function, plot, retrieve those text labels, and set to empty string:
h = findall(gca,'Type','text','-and','Tag','spoke');
set(h,'string','')

MATLAB - How to zoom subplots together?

I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)