How do I handle this issue
clear all; close all; clc;
r = 0:5;
zeta = 0:0.1:1;
for i = 1:size(zeta(:))
for j = 1:size(r(:))
X(i,j) = sqrt((1+(2*zeta(i)*r(j))^2)/((1-r(j)^2)^2+ (2*zeta(i)*r(j))^2));
end
plot(r,X);
xlabel('r = \omega/\omega_n');
ylabel('M = \frac{X}{Y}');
hold all
grid
[~,~,~,current_entries] = legend;
legend([current_entries {sprintf('\zeta = %i',zeta(i))}]);
end
figure
plot(r,X)
grid
The hold all command doesn't seem to be working properly. What can I do to fix this?
You will want to set the DisplayName property of the plot. Then when you create the legend, the labels will automatically be populated.
plot(r, X, 'DisplayName', 'name')
Also, the string that you were passing to sprintf for your legends needs to be escaped becuase sprintf thinks that \z is a control character.
plot(r, X, 'DisplayName', sprintf('\\zeta = %0.1f',zeta(k)))
Also, hold on is recommended over hold all. Also, it is best practice to specify the axes handle when calling hold to ensure that it is applied to the current axes.
hold(hax, 'on')
So if we incorporate these changes into your plotting code (along with #R.Falque's idea to use semilogy)
r = 0:0.001:5;
zeta = 0:0.1:1;
hax = axes();
colors = hsv(numel(zeta));
for k = 1:numel(zeta)
X = sqrt((1 + (2 * zeta(k) * r).^2) ./ ((1-r.^2).^2+ (2*zeta(k)*r).^2));
semilogy(r, X, ...
'DisplayName', sprintf('\\zeta = %0.1f',zeta(k)), ...
'Color', colors(k,:));
hold(hax, 'on')
end
grid(hax, 'on')
xlabel(hax, 'r = \omega/\omega_n', 'Interpreter', 'tex');
ylabel(hax, 'M = $\displaystyle\frac{X}{Y}$', 'Interpreter', 'latex');
L = legend('show');
You can also use cells array as follow:
clear all; close all; clc;
r = 0:0.001:5;
zeta = 0:0.1:1;
figure;
for i = 1:length(zeta)
for j = 1:length(r)
X(j) = sqrt((1+(2*zeta(i)*r(j))^2)/((1-r(j)^2)^2+ (2*zeta(i)*r(j))^2));
end
semilogy(r,X);
hold on
legend_string{i} = ['\zeta = ', num2str(zeta(i))];
end
hold off
grid
xlabel('r = \omega/\omega_n');
ylabel('M = $\frac{X}{Y}$','Interpreter', 'Latex');
legend(legend_string);
Note that you have an error in the X definition (corrected from X(i,j) to X(j)).
Related
In the following code, plot(plot_x,plot_y,'-k'); in current iteration shows the previous iterations plots.
But I want to show only the current iteration plot, i.e., previous plot should be disappeared. However, plot(N_x,N_y,'Ob','MarkerSize',10); should be there always. What kind of modification should I do?
N = 50;
L = 20;
itr = 5;
N_x = (rand(N,1)-0.5)*L;
N_y = (rand(N,1)-0.5)*L;
plot(N_x,N_y,'Ob','MarkerSize',10);
grid on;
hold on;
axis([-0.5*L 0.5*L -0.5*L 0.5*L]);
for n = 1:itr
out = randperm(N,5);
plot_x = N_x(out);
plot_y = N_y(out);
plot(plot_x,plot_y,'-k');
hold on;
pause(1);
end
You can update the XData and YData properties of a single line instead of creating a new one.
I've added comments to the below code to explain the edits
N = 50;
L = 20;
itr = 5;
N_x = (rand(N,1)-0.5)*L;
N_y = (rand(N,1)-0.5)*L;
plot(N_x,N_y,'Ob','MarkerSize',10);
grid on;
hold on;
axis([-0.5*L 0.5*L -0.5*L 0.5*L]);
p = plot(NaN, NaN, '-k'); % Create a dummy line to populate in the loop
for n = 1:itr
out = randperm(N,5);
plot_x = N_x(out);
plot_y = N_y(out);
set( p, 'XData', plot_x, 'YData', plot_y ); % Update the line data
drawnow(); % flush the plot buffer to force graphics update
pause(1);
end
In the plot loop, ask the plot function returnig the handle of the line, then, after the pause statement delete the line by calling the delete function.
for n = 1:itr
out = randperm(N,5);
plot_x = N_x(out);
plot_y = N_y(out);
%
% Get the handle of the line
%
hp=plot(plot_x,plot_y,'-k');
hold on;
pause(1);
%
% Delete the line
%
delete(hp)
end
By plotting the curves in a loop that each curve is related to its own stored data (i.e. method) in the previous question, how is it possible to change and define a new specific color and line type (e.g. "--") for each method's curve after the loop?
figure
hold on
for m = 1:length(methods)
prFileName = strcat(readpath,dataset, '_', methods{m}, '_PRCurve.txt');
R = load(prFileName);
precision = R(:, 1);
recall = R(:, 2);
plot(recall, precision,'color',methods_colors(m,:),'linewidth',2);
end
axis([0 1 0 1]);
hold off
grid on;
box on;
legend('methode one','method two')
xlabel('Recall','fontsize',12);
ylabel('Precision','fontsize',12);
set(gcf,'color','w'); %Background color
ax = gca; % current axes
ax.GridLineStyle='-';
ax.GridAlpha=0.7;
ax.XAxis.LineWidth=4;
ax.YAxis.LineWidth=4;
Grid.LineWidth = 3;
set(gca,'FontName','Arial','FontWeight','bold');
For convenience in implementation, each method's data can be assumes as rand(256x2).
You can use lines_obj = findobj(h, 'Type', 'Line'), and set(lines_obj, ....
Example:
h = figure;
%Plot something
x = -6:0.1:6;
plot(x, sin(x), x, cos(x), x, sin(x.^2), x, cos(x.^2));
%Find all lines in figure
lines_obj = findobj(h, 'Type', 'Line');
%Set style of all lines to '--'
set(lines_obj, 'LineStyle', '--');
result:
In case you need to set different style for each line, and you need to match as specific type to specific plot...
Keep an array of line objects while plotting, and use a for loop:
close all
g = gobjects(4); %Initialize and array with 4 graphics objects.
x = -6:0.1:6;
g(1) = plot(x, sin(x), 'r');hold on %Keep returned object in g(1)
g(2) = plot(x, cos(x), 'g'); %Keep returned object in g(2)
g(3) = plot(x, sin(x.^2), 'b');
g(4) = plot(x, cos(x.^2), 'y');
%Cell array of styles.
styles = {'-', '--', ':', '-.'};
%Array of width values
widths = [0.5, 1, 1.5, 2];
%Modify the style of each line:
for i = 1:length(g)
g(i).LineStyle = styles{i};
g(i).LineWidth = widths(i);
end
Result:
I have two datasets which I want to plot in the same figure, e.g. two cosine and two sine plots which just differ in the amplitude:
x = -pi:pi/20:pi;
hold all;
amplitude = 1;
plot(x,amplitude*cos(x),'-');
plot(x,amplitude*sin(x),'-');
ax = gca;
ax.ColorOrderIndex = 1;
amplitude=3;
plot(x,amplitude*cos(x),'.');
plot(x,amplitude*sin(x),'.');
legend('1*cos(x)','1*sin(x)', '2*cos(x)','2*sin(x)');
hold off;
I want to "compress" the legend so that the two line stiles (normal line and dotted line) are "merged" and appear next to the same textual entry in the legend, such as:
How can I achieve this in MATLAB? I am currently using R2015b.
This is the closest I have got having a quick look with r2015b:
%%
f = figure;
ax = axes;
x = -pi:pi/20:pi;
hold all;
amplitude = 1;
c1 = plot(x,amplitude*cos(x),'-', 'DisplayName', 'cos(x)');
s1 = plot(x,amplitude*sin(x),'-', 'DisplayName', 'sin(x)');
ax.ColorOrderIndex = 1;
amplitude=3;
c2 = plot(x,amplitude*cos(x),'.', 'DisplayName', ' ');
s2 = plot(x,amplitude*sin(x),'.', 'DisplayName', ' ');
lg = legend([c1 c2 s1 s2]);
hold off;
Manipulating legends was easier pre HG2 - so using an older version of Matlab (r2013a) I get:
%%
f = figure;
ax = handle(axes);
x = -pi:pi/20:pi;
hold all;
amplitude = 1;
c1 = plot(x,amplitude*cos(x),'r-', 'DisplayName', 'cos(x)');
s1 = plot(x,amplitude*sin(x),'b-', 'DisplayName', 'sin(x)');
amplitude=3;
c2 = plot(x,amplitude*cos(x),'r.', 'DisplayName', ' ');
s2 = plot(x,amplitude*sin(x),'b.', 'DisplayName', ' ');
lg = handle(legend([c1 c2 s1 s2]));
hold off;
% You need to find which of the children on the legend is
% each of the plots:
c1 = handle(lg.Children(1));
c1.YData = 0.3;
s1 = handle(lg.Children(7));
s1.YData = 0.75;
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)
It is beginning to annoy me that I can't get rid of this vertical line that keeps appearing when I plot the area;
[x y] = ginputExtra(4)
x = 0.1947 0.6118 0.8329 0.4136
y = 0.5746 0.8173 0.4225 0.3553
area([x x(1)],[y y(1)])
[x y] = ginputExtra(4,true)
x = 0.5087 0.6881 0.4954 0.3204
y = 0.4961 0.2382 0.1566 0.3566
hold on;
area([x x(1)],[y y(1)],'FaceColor',[1 0 0])
Is there any way to avoid this line?
BTW: the ginputExtra method call I use..
function [x y] = ginputExtra(n,booText)
% INPUT
% n: Number of points to plot
% booText: Boolean (default false) command to display point number in
% the plot.
% Author: Lasse Nørfeldt (Norfeldt)
% Date: 2012-04-09
if nargin ==2
bText = booText;
else
bText = false;
end
H = gca;
set(H, 'YLimMode', 'manual'); set(H, 'XLimMode', 'manual');
set(H, 'YLim', get(H,'YLim')); set(H, 'XLim', get(H,'XLim'));
numPoints = n; xg = []; yg = [];
for i=1:numPoints
[xi yi] = ginput(1);
xg = [xg xi]; yg = [yg yi];
if i == 1
hold on;
plot(H, xg(i),yg(i),'ro');
if bText text(xg(i),yg(i),num2str(i),'FontSize',14); end
else
plot(xg([i-1:i]),yg([i-1:i]),'r');
if bText text(xg(i),yg(i),num2str(i),'FontSize',14); end
end
end
hold off;
x = xg; y = yg;
Your issue might be in plotting by area(), as it seems to be primarily for stacking several vecotrs. If you zoom out a bit and see a similar vertical line from the first point in the blue area, the area function is most likely the issue.
The function:
fill([x x(1)],[y y(1)],COLOR)
Might do the trick for you, as it plots a filled polygon.
/Thomas