Matlab multiple legends on one plot 2014b - matlab

I want to have multiple legends on one plot. This solution works perfectly before 2014b version. I am trying to figure out how to make this elegantly using handles, but so far no success. Any ideas are welcome.
Example in 2013b:
x = 1:50;
y1 = sin(x/2);
y2 = cos(x/2);
f = figure(1);
pl(1) = plot(x,y1,'g');hold on;
pl(2) = plot(x,y2,'r');
h1 = legend('eg1','eg2');
set(h1,'Location','NorthEast')
tmp = copyobj(h1,f);
h2 = legend(pl,'sin','line');
set(h2,'Location','SouthWest')
I do get something using
ax = gca;
tmp = copyobj([h1,ax],f);
but when I set the legend again, previous legend goes under the plot.
Thanks!

Matlab by default only allows one legend per axes, so what you would have to do is create a fake/empty secondary axis in order to get your legend. Mathworks help has a good example of this
Code to produce the below chart is here
x= 0:0.01:2*pi;
y = sin(x);
hl1 = line(x, y,'Color','k','LineStyle','--');
ax1 = gca;
set(ax1,'xlim',[0, 7],'ylim',[-1,
1],'XColor','k','YColor','k');
legend_handle1 = legend(' sin');
ax2 = axes('Position',get(ax1,'Position'),...
'xlim',[0, 7],'ylim',[-1,1],...
'Visible','off','Color','none');
hl2 = line(pi/2, 1,'Color','r','Marker', 'o','Parent',ax2);
hl3 = line(pi, 0,'Color','g','Marker', 'x','Parent',ax2);
legend_handle2 = legend('peak', 'zero');
set(legend_handle2, 'Color', 'none');

Here is a simple alternative approach
t = linspace(0,2*pi,200);
frequencies=1:3;
for w=frequencies;
y = sin(w*t);
plot(t,y)
hold on
end
legend("w = " + num2str(frequencies'));

Related

Is it possible to subplot confusion matrices?

I'm plotting several confusion matrices using plot_confusion() function and I want to put them in a subplot (2x5 figures), but it does not seem to work.It displays every confusion matrix separately. Are there any restriction for plotting confusion? Thanks!
figure
Subplot(2,1,1);
plotconfusion(targets,outputs,'train');
subplot(2,1,2);
plotconfusion(targets1,outputs1,'test')
You're "not supposed" to do that (the functionality is not included), but you can trick Matlab a little, because at the end of a day it's just an axes object:
%% First Example Data
[x,t] = cancer_dataset;
net = patternnet(10);
net = train(net,x,t);
y = net(x);
%// plot
plotconfusion(t,y)
%// get handle and enable second plöt
cp1 = gcf;
cp1.NextPlot = 'new'
ax1 = findobj(cp1,'Type','Axes')
%% Second Example Data
[x,t] = cancer_dataset;
net = patternnet(5);
net = train(net,2*x,t);
y = net(x);
%// plot
plotconfusion(t,y)
%// get handle and enable third plöt
cp2 = gcf;
cp2.NextPlot = 'new'
ax2 = findobj(cp2,'Type','Axes')
%% combine plots
f1 = figure(42)
f1s1 = subplot(121)
copyobj(allchild(ax1),f1s1)
f1s2 = subplot(122)
copyobj(allchild(ax2),f1s2)
You loose the labels and titles and may need to adjust the axis, but I guess you're able to do that.

Speed up creation of impoint objects

I have to create some draggable points on an axes. However, this seems to be a very slow process, on my machine taking a bit more than a second when done like so:
x = rand(100,1);
y = rand(100,1);
tic;
for i = 1:100
h(i) = impoint(gca, x(i), y(i));
end
toc;
Any ideas on speed up would be highly appreciated.
The idea is simply to provide the user with the possibility to correct positions in a figure that have been previously calculated by Matlab, here exemplified by the random numbers.
You can use the the ginput cursor within a while loop to mark all points you want to edit. Afterwards just click outside the axes to leave the loop, move the points and accept with any key.
f = figure(1);
scatter(x,y);
ax = gca;
i = 1;
while 1
[u,v] = ginput(1);
if ~inpolygon(u,v,ax.XLim,ax.YLim); break; end;
[~, ind] = min(hypot(x-u,y-v));
h(i).handle = impoint(gca, x(ind), y(ind));
h(i).index = ind;
i = i + 1;
end
Depending on how you're updating your plot you can gain a general speedup by using functions like clf (clear figure) and cla (clear axes) instead of always opening a new figure window as explained in this answer are may useful.
Alternatively the following is a very rough idea of what I meant in the comments. It throws various errors and I don't have the time to debug it right now. But maybe it helps as a starting point.
1) Conventional plotting of data and activating of datacursormode
x = rand(100,1);
y = rand(100,1);
xlim([0 1]); ylim([0 1])
f = figure(1)
scatter(x,y)
datacursormode on
dcm = datacursormode(f);
set(dcm,'DisplayStyle','datatip','Enable','on','UpdateFcn',#customUpdateFunction)
2) Custom update function evaluating the chosen datatip and creating an impoint
function txt = customUpdateFunction(empt,event_obj)
pos = get(event_obj,'Position');
ax = get(event_obj.Target,'parent');
sc = get(ax,'children');
x = sc.XData;
y = sc.YData;
mask = x == pos(1) & y == pos(2);
x(mask) = NaN;
y(mask) = NaN;
set(sc, 'XData', x, 'YData', y);
set(datacursormode(gcf),'Enable','off')
impoint(ax, pos(1),pos(2));
delete(findall(ax,'Type','hggroup','HandleVisibility','off'));
txt = {};
It works for the, if you'd just want to move one point. Reactivating the datacursormode and setting a second point fails:
Maybe you can find the error.

Adding a second x-axis to my graph, which I can scale myself

I am having a problem with Matlab at the moment and I hope you can help me. I have written this code below to display a graph, but now I want to add another x-axis above the image, which I can scale myself. Is there any possibility to add another line like: set(gca, 'XTick2', [bla:bla:bla]); and another label?
EDIT: I have solved a part of the problem, hope you can help me with the rest, too... I have now 2x-axes, but still a few problems.
The labels are in wrong positions, the y-label is inside the scale and I want two different labels for the two x-axes.
Also I would like to remove the scale of the lower x-axis from the upper one.
Code is also the new one:
x1 = [0, 421.441, 842.882, 1264.323, 1685.764, 2107.205, 2528.646, 2950.087, 3371.528, 3792.969, 4214.41, 4635.851, 5057.29];
y1 = [55.659, 55.856, 56.081, 56.279, 56.312, 56.169, 56.038, 55.903, 55.75, 55.604, 55.512, 55.534, 55.661];
y2 = [51.231, 51.735, 52.063, 52.152, 51.632, 51.16, 51.014, 50.911, 50.721, 50.596, 50.597, 50.858, 51.242];
y3 = [50.939, 51.381, 51.644, 51.687, 51.353, 50.944, 50.829, 50.706, 50.538, 50.43, 50.412, 50.614, 50.948];
y4 = [50.023, 50.328, 50.506, 50.535, 50.352, 50.113, 50.032, 49.938, 49.801, 49.705, 49.672, 49.801, 50.03];
plot(x1,y1, 'ks-',x1,y2, 'bx--',x1,y3, 'gd-.',x1,y4, 'c.-'),
ax1 = gca;
ax1.XLim = [0, 5075];
ax1.XTick = 0:1000:5075;
ay1 = gca;
ay1.YLim = [49.5, 56.5];
ay1.YTick = 49.5:0.5:56.5;
ax2 = axes('Position',ax1.Position,'Color','none');
ax2.XLim = [0, 360];
ax2.XTick = 0:30:360;
ax2.XAxisLocation = 'top';
ax2.YTick = [];
grid off
xlabel('Time [s]')
ylabel('Temperature [°C]')
legend('A','B','C','D')
Just use the function text,
text(x,y,'string')
where x and y are the coordinates of you new x-axis and string your blabla.
UPDATE: use this sample code and adjust it to your needs
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
figure
line(x1,y1,'Color','r')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
which gives the plot,

MATLAB - How to make multiple markers moving simultaneous in 3d plot in MATLAB

I am currently working on a project simulating the movement of two spacecraft and a moon (Phobos) around Mars. A MATLAB tool called SPICE gives me an array with the x, y and z distances and I have used these to plot the orbit which works fine. Now I want to get markers for each of the spacecraft and Phobos to see when they flyby each other. I got the markers working but not at the same time, they run after each other. I found an example on youtube so it must be possible but he has not released the code how to do it (https://www.youtube.com/watch?v=nArR2P0o4r4).
This is the code I have:
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans = makehgtform('translate',[x(i) y(i) z(i)]);
set(ht,'Matrix',trans);
pause(0.001);
end
This will run a nice animated plot of the trajectory of Phobos in time but only Phobos and not simultaneous with MEX and MAVEN (spacecraft from ESA and NASA).
I tried this but does not work:
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans1 = makehgtform('translate',[x(i) y(i) z(i)]);
set(ht,'Matrix',trans1);
trans2 = makehgtform('translate',[a(i) b(i) c(i)]);
set(ht,'Matrix',trans2);
pause(0.001);
end
I also tried merging the arrays so that it plots them each one step after each other but that makes the animation not smooth and is not satisfying for the project.
a = position_MEX_Mars(1,:);
b = position_MEX_Mars(2,:);
c = position_MEX_Mars(3,:);
k = position_MAVEN_Mars(1,:);
l = position_MAVEN_Mars(2,:);
m = position_MAVEN_Mars(3,:);
x = position_Phobos_Mars(1,:);
y = position_Phobos_Mars(2,:);
z = position_Phobos_Mars(3,:);
tempx = [position_MEX_Mars(1,:); position_Phobos_Mars(1,:); position_MAVEN_Mars(1,:)];
xt = tempx(:);
tempy = [position_MEX_Mars(2,:); position_Phobos_Mars(2,:); position_MAVEN_Mars(2,:)];
yt = tempy(:);
tempz = [position_MEX_Mars(3,:); position_Phobos_Mars(3,:); position_MAVEN_Mars(3,:)];
zt = tempz(:);
ah = axes;
set(ah,'XLim',[min(x) max(x)],'YLim',[min(y) max(y)],...
'ZLim',[min(z) max(z)]);
plot3(0,0,0,'ro-',x,y,z,a,b,c,k,l,m);
grid on;
hold on;
hpoint = line('XData', 0,'YData', 0,'ZData', 0,'Color','black','Marker',...
'o','MarkerSize',10);
ht = hgtransform('parent',ah);
set(hpoint,'Parent',ht);
for i =2:length(x)
trans = makehgtform('translate',[xt(i) yt(i) zt(i)]);
set(ht,'Matrix',trans);
pause(0.001);
end
I think I am close but I seem to be missing something and my knowledge of MATLAB is not that great yet. I hope you can help me out.
Cheers Jeroen
Here's a simplified (and not physically correct) example that could perhaps be useful:
t = linspace(0,2,1000); %// time parameter
x1 = 10*cos(2*pi*t+1);
y1 = 5*sin(2*pi*t+1); %// trajectory of object 1
x2 = 2*cos(6*pi*t-2);
y2 = 3*sin(6*pi*t-2); %// trajectory of object 1
plot(x1,y1,'color',[.5 .5 .5]); %// plot trajectory of object 1
hold on
plot(x2,y2,'color',[.5 .5 .5]); %// plot trajectory of object 2
h1 = plot(x1(1),y1(1),'ro'); %// plot initial position of object 1
h2 = plot(x2(1),y2(1),'b*'); %// plot initial position of object 2
axis([-12 12 -12 12]) %// freeze axis size
grid on
for n = 1:numel(t)
set(h1, 'XData', x1(n), 'YData', y1(n)); %// update position of object 2
set(h2, 'XData', x2(n), 'YData', y2(n)); %// update position of object 2
drawnow %// refresh figure
end
The thing that you tries to do is not really as easy as you think. The main issue is that you need to update everything at the same time. This have been implemented in several ways during the years. One way to do this is by using a so called a double buffer.
This means that you have two "surfaces" to paint on. In matlab this is translated to 2 axes (or 2 figures maybe). However, you do only have one axes visible at the same time. This means that you will have time to paint everything on the "hidden surface" before displaying the content. When you are done with everything you need you just switch which surface being visible.
It is possible that this can be done in a simpler way, but I am not familiar with the hgtransfrom functions in matlab.
EDIT
This is an example how it can be done
function test()
fig = figure;
ax1 = axes;
plot(1:10);
ax2 = axes;
plot(1:10,'r');
setVisibility(ax2,'off');
pause(1);
for k = 1:5
setVisibility(ax2,'on');
setVisibility(ax1,'off');
pause(1);
setVisibility(ax1,'on');
setVisibility(ax2,'off');
pause(1);
end
function setVisibility(ax, visibility)
set(ax, 'visible', visibility)
set(findall(ax), 'visible', visibility)

Nicer errorbars when multiple data is shown

So I need to plot some errobar plots in a figure. Specifically I need 4 errorbar plots in each figure, the problem is that the figure gets a bit unreadable when several data is plotted.
Example:
clear all
close all
clc
x = 0:pi/10:pi;
y = sin(x);
y2=cos(x);
y3=atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
figure
hold on
errorbar(x,y,e)
errorbar(x,y2,e2)
errorbar(x,y3,e3)
My idea to solve the problem is to fill the area that the corners of the errorbars delimit with the same color of the plot and low alpha, so the overlapping of the areas is visible.
The problem is that the only way I can imagine of doing this is to create a mesh in the area delimited by the errorbar corners and then fill them with patch. This is indeed possible, but quite annoying, as a plot will not have a convex hull, therefore I will need to iteratively go creating the triangles one by one. So the question is : Is there a more elegant way of doing this?
Additionally, I am open to suggestions of a better way of visualizing this data, if anyone has.
Approach 1
Plot the graphs normally, and then plot the errorbars manually using patches. The data for the patches (coordinates and color) is taken from the plotted graphs, and the alpha of the patch can be set to any desired value.
clear all
close all
clc
error_alpha = .4;
error_width_factor = .01;
x = 0:pi/10:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w, hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w',...
'FaceColor', hp(m).Color, 'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end
Approach 2
Similar as before, but use narrower patches and plot them with a graph-dependent horizontal shift (as suggested by #Patrik). Applying an alpha value helps keep the figure lighter.
The code is a modified version of that of approach 1. The example shown here contains 101 data values, and is still rather visible.
clear all
close all
clc
error_alpha = .4;
error_width_factor = .003;
x = 0:pi/50:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
m0 = (numel(hp)+1)/2;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w+w*(m-m0),...
hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w', 'FaceColor', hp(m).Color, ...
'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end