Moving a curve inside a figure - matlab
As you can see, in the first picture I have two subplots, two the same, with the same curve position. But I need to change the curve position/or view angle of one of the subplot (doesn't matter now if the left one or the right one). The only working command is CameraViewAngle but it changes for both of it, I don't know why, and I need to be able to change for each one seperately.
As you can see in picture two, the view angle changes for both of it. I would rather chagne only the curve position.
The code I'm using is:
function [pointsQRS, pointsP, pointsT] = VCG (pointsQRS,pointsP,pointsT)
global ax1 ax2 h
figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8])
ax1=subplot(1,2,1)
set(ax1,'Position',[0.10,0.11,0.3346590909090909,0.815],'CameraPosition',
[50.197132152696216,49.50150052184264,-7.57531754730548],'CameraTarget',
[0.4, 0.7, -0.33],'View',[-184.4219, -8.9326],'CameraViewAngle',
[9.256133109732078])
title('Vektorkardiogram')
hold on
grid on
view(3)
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-
1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'-
r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'Color',
[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'-
b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'Color',
[0 0 0],'LineWidth',1);
end
end
xlabel('Vx');ylabel('Vy');zlabel('Vz');
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
ax2=subplot(1,2,2)
set(ax2,'Position',[0.565,0.11,0.33465909090909096,0.815],'CameraPosition',
[-13.197132152696216,-59.50150052184264,10.57531754730548])
title('Vektorkardiogram')
hold on
grid on
h=linkprop([ax1, ax2],
{'CameraPosition','CameraUpVector','XTick','YTick','ZTick'});
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-
1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'-
r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'Color',
[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'-
b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'Color',
[0 0 0],'LineWidth',1);
end
end
xlabel('Vx');ylabel('Vy');zlabel('Vz');
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
linkaxes([ax1, ax2])
I need it to change it in order to achieve 3D stereoscopic view using Google Cardboard.
Thank you in advance for any help!
PS: For example I want to achieve something as in the picture 3
But in the picture three the 3D curves are not connected, by connected I mean that while I move with one of them, the other one will be moved in the same way.
I managed to partially solve it.
Changing CameraViewAngle, modifies the size of the curve, so I decided to keep it unmodified.
Instead of setting the angle, set CameraPosition of ax2 to be different than ax1, but keeping the same distance from the target.
Instead of using linkprop, I used addlistener, and implemented a callback function that sets the CameraPosition of ax2 when CameraPosition of ax1 is modified.
Read the following reference pages:
addlistener
View Control with the Camera Toolbar
Camera Graphics Terminology
Axes Properties
Share Data Among Callbacks
Here is the code:
function [pointsQRS, pointsP, pointsT] = VCG (pointsQRS,pointsP,pointsT)
%Clear global varabiles - for testing, we don't want any leftovers of ax1 and ax2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear global
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Set some input data.
pointsQRS = 1:3:78;
pointsP = 1:2:92;
pointsT = 1:1:85;
%global ax1 ax2 h
fig1 = figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8]);
ax1=subplot(1,2,1);
%Don't use view command, fill all parameters manually.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%view(ax1, 3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Set ax1 parameters values to be same as result as after executing view(ax1, 3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(ax1, 'XLim', [-0.5, 2.0], 'YLim', [-1, 3.0], 'ZLim', [-1.5, 1.0]);
set(ax1, 'Position',[0.13, 0.11, 0.3346590909090909, 0.815]);
set(ax1, 'CameraViewAngle', 10.339584907201974);
set(ax1, 'CameraTarget', [0.75, 1.0, -0.25]);
set(ax1, 'View',[-184.4219, -8.9326]);
set(ax1, 'DataAspectRatio', [1, 1.6, 1.0]);
set(ax1, 'PlotBoxAspectRatio', [1, 0.95521169259094, 0.8871534463389812], 'PlotBoxAspectRatioMode', 'Manual');
set(ax1, 'CameraPosition', [-10.664276793913514,-22.800600208737055,10.57531754730548]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax2 = subplot(1,2,2);
%Set ax2 parameters values to be same as result as after executing view(ax2, 3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(ax2, 'XLim', [-0.5, 2.0], 'YLim', [-1, 3.0], 'ZLim', [-1.5, 1.0]);
set(ax2, 'Position',[0.5703409090909091, 0.11, 0.3346590909090909, 0.815]);
set(ax2, 'CameraViewAngle', 10.339584907201974);
set(ax2, 'CameraTarget', [0.75, 1.0, -0.25]);
set(ax2, 'View',[-184.4219, -8.9326]);
set(ax2, 'DataAspectRatio', [1, 1.6, 1.0]);
set(ax2, 'PlotBoxAspectRatio', [1, 0.95521169259094, 0.8871534463389812], 'PlotBoxAspectRatioMode', 'Manual');
set(ax2, 'CameraPosition', [-10.664276793913514,-22.800600208737055,10.57531754730548]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Move the camera of ax2 to be in different position compared to ax1, but keep the same distance from the Target.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cameraTarget = [0.75, 1.0, -0.25];
cameraPosition = [-10.664276793913514,-22.800600208737055,10.57531754730548];
V = cameraPosition - cameraTarget;
newV = V + [-10, 0, 0];
newV = newV/norm(newV)*norm(V);
newCameraPosition = cameraTarget + newV;
set(ax2, 'CameraPosition', newCameraPosition);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Store ax1 and ax2 in figure UserData
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(fig1, 'UserData', [ax1, ax2]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Set axes ax1 as active axes.
axes(ax1);
title('Vektorkardiogram')
hold on
grid on
%Move view(3) up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%view(3)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Use plot3 from Matlab example (fill curve with some data).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%t = -3*pi:pi/50:3*pi;st = sin(t)*2;ct = cos(t)*2;plot3(st,ct,t/6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'-b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
xlabel('Vx');ylabel('Vy');zlabel('Vz');
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%ax2 = subplot(1,2,2); Moved up, axes(ax2) is used instead.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ax2 = subplot(1,2,2);
axes(ax2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title('Vektorkardiogram')
hold on
grid on
%Move linkprop to the end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%h=linkprop([ax1, ax2],{'CameraPosition', 'CameraUpVector', 'XTick', 'YTick', 'ZTick'});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-1:i],2),pointsQRS([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'-b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
%Use plot3 from Matlab example (fill curve with some data).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%t = -3*pi:pi/50:3*pi;st = sin(t)*2;ct = cos(t)*2;plot3(ax2, st,ct,t/6)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xlabel('Vx');ylabel('Vy');zlabel('Vz');
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth', 0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%view(ax1, 3)
%view(ax2, 3)
%Matalb gives warning message: linkaxes requires 2-D axes as input. Use linkprop for generic property linking.
%call linkprop here instead.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%linkaxes([ax1, ax2])
%h=linkprop([ax1, ax2],{'CameraPosition', 'CameraUpVector', 'XTick', 'YTick', 'ZTick'});
%Don't link CameraPosition - it is linked by "listener" implementation
linkprop([ax1, ax2],{'CameraUpVector', 'XTick', 'YTick', 'ZTick'});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Add "listener", instead of using linkprop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
addlistener(ax1, 'CameraPosition', 'PostSet', #Ax1CameraPositionCallback);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Display cameratoolbar
cameratoolbar
function Ax1CameraPositionCallback(src, evnt, ~)
ax1 = evnt.AffectedObject;
fig1 = get(ax1, 'Parent');
userData = get(fig1, 'UserData');
ax2 = userData(2);
%Move the camera of ax2 to be in different position compared to ax1, but keep the same distance from the Target.
cameraTarget = get(ax1, 'CameraTarget');
cameraPosition = get(ax1, 'CameraPosition');
V = cameraPosition - cameraTarget;
newV = V + [-10, 0, 0];
newV = newV/norm(newV)*norm(V);
newCameraPosition = cameraTarget + newV;
set(ax2, 'CameraPosition', newCameraPosition);
Related
imagesc with multiple axis and ticklines
I have a [12 x 6] matrix A that I represent with imagesc and on which I impose some thick tick-lines: figure imagesc(A) set(gca,'xtick', linspace(0.5,size(A,2)+0.5,C+1),... 'ytick', linspace(0.5,size(A,1)+0.5,B*al+1),'linewidth',3); set(gca,'xgrid', 'on', 'ygrid', 'on', 'gridlinestyle', '-', 'xcolor', 'k', 'ycolor', 'k'); set(gca, 'XTickLabelMode', 'manual', 'XTickLabel', [],'YTickLabel', []); colorbar I then want to impose some thinner tick-lines to split in two each box delimited by the thicker lines: ax1 = gca; ax1_pos = get(ax1,'Position'); % position of first axes ax2 = axes('Position',ax1_pos,... 'XAxisLocation','top',... 'YAxisLocation','right',... 'Color','none'); set(gca,'xtick', linspace(0.5,size(A,2)+0.5,2*C+1),'linewidth',1); The result is clearly not satisfying. How could I fix it? I thought that 'Color','none' would have done the trick...
Instead of trying to modify tick lengths and adding a second axes, I would just plot some extra lines on top of your image. This should achieve what you want: % Some sample data: A = rand(12, 6); % Plot image: imagesc(A); set(gca, 'Visible', 'off'); hold on; colorbar; % Plot horizontal lines: yLines = repmat((0:size(A, 1))+0.5, 2, 1); xLines = repmat([0.5; size(A, 2)+0.5], 1, size(yLines, 2)); line(xLines, yLines, 'LineWidth', 3, 'Color', 'k'); % Plot thick vertical lines: xLines = repmat((0:2:size(A, 2))+0.5, 2, 1); yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2)); line(xLines, yLines, 'LineWidth', 3, 'Color', 'k'); % Plot thin vertical lines: xLines = repmat((1:2:size(A, 2))+0.5, 2, 1); yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2)); line(xLines, yLines, 'LineWidth', 1, 'Color', 'k'); And here's the plot:
Hatched bars and standard deviation errors in bar charts with Matlab
I'm trying to create a bar plot. On the x axis I have 4 different conditions: HFd, HFi, LFd, LFi. On the y axis Reaction time is displayed. I need the bar representing HFd to be hatched and blue, HFi to be blue, LFd to be hatched and red, LFi to be red. In addition, I need the standard deviation to be displayed in black on each bar. Here are my attempts. Although they work, they don't give me the bar plot I need. Any help would be very much appreciated. %% This code creates the plot, but without the standard deviation errors and the different colours. data1 = [228, 222, 229, 218]; sdev1 = [29, 30, 29, 31]; hold on; figure; bar(data1); %errorbar(data1, sdev1); %it gives me a line that links the different standard deviation errors. set(gca, 'XTickLabel',{'HFd', 'HFi', 'LFd', 'LFi'}); ylabel('Reaction time (ms)'); ylim([180 300]); %% This code creates the plot, with different colours but without the hatched patterns and the standard deviation errors. dataHFdeg = [228, 0, 0, 0]; sdev11 = [29, 0, 0, 0] dataHFid = [0, 222, 0, 0]; sdev12 = [0, 30, 0, 0] dataLFdeg = [0, 0, 229, 0]; sdev13 = [0, 0, 29, 0] dataLFid = [0, 0, 0, 218]; sdev14 = [0, 0, 0, 31] bar(dataHFd,'b'); set(gca, 'XTickLabel',{'HFd', 'HFi', 'LFd', 'LFi'}); ylabel('Reaction time (ms)'); ylim([180 300]); hold on; bar(dataHFi, 'b'); hold on; bar(dataLFd, 'r'); hold on; bar(dataLFi, 'r'); UPDATE: %% This code creates the plot, with different colours and with error bars, but without the hatched patterns y = [228; 222; 229;218]; %The data. s = [29;30;29;31]; %The standard deviation. fHand = figure; aHand = axes('parent', fHand); hold(aHand, 'on') bar(1, y(1), 'parent', aHand, 'facecolor', 'b'); bar(2, y(2), 'parent', aHand, 'facecolor', 'b'); bar(3, y(3), 'parent', aHand, 'facecolor', 'r'); bar(4, y(4), 'parent', aHand, 'facecolor', 'r'); colors = copper(numel(y) - 4); for i = 5:numel(y) bar(i, y(i), 'parent', aHand, 'facecolor', colors(i-4,:)); end set(gca, 'XTick', 1:numel(y), 'XTickLabel', {'HFd','HFi','LFd','LFi'}) ylim([180 300]); errorbar(y,s,'.'); %% To create for example the hatched pattern for the second bar - but it doesn't work applyhatch_pluscolor(bar(2,y(2)), '/', 1);
Additional axis on 3D surface in MATLAB
Suppose I plot a 3d graph using [X,Y,Z] = peaks(25); figure surf(X,Y,Z); How can I add addtional x and y axis with labels to the z=0 plane like the red ones shown in this picture? I would like to keep the original axis as is.
I would just draw my own fake axis. I'm not sure there is a better solution, but this one works. [X,Y,Z] = peaks(25); figure surf(X,Y,Z); hold on; %define the plot limits xlim_arr = [-4 4]; ylim_arr = [-5 5]; zlim_arr = [-10 10]; %fix the limits of the real axes xlim(xlim_arr); ylim(ylim_arr); zlim(zlim_arr); %add grid and labels through all x-points for i=xlim_arr(1):xlim_arr(2) plot3([i i],[ylim_arr(1) ylim_arr(2)], [0 0], 'Color', [0.7 0.7 0.7], 'LineWidth',0.4); text(i, ylim_arr(1)-0.4, 0, num2str(i)); text(i, ylim_arr(2)+0.4, 0, num2str(i)); end %add grid and labels through all y-points for i=ylim_arr(1):ylim_arr(2) plot3([xlim_arr(1) xlim_arr(2)], [i i], [0 0], 'Color', [0.7 0.7 0.7], 'LineWidth',0.4); text(xlim_arr(1)-0.4, i, 0, num2str(i)); text(xlim_arr(2)+0.4, i, 0, num2str(i)); end %add the bold frame to highlight the fake axes px = [xlim_arr(1) xlim_arr(1) xlim_arr(2) xlim_arr(2) xlim_arr(1)]; py = [ylim_arr(1) ylim_arr(2) ylim_arr(2) ylim_arr(1) ylim_arr(1)]; pz = [0 0 0 0 0]; plot3(px,py,pz, 'k', 'LineWidth', 0.5);
How do I plot a triangle in three dimensions? [duplicate]
This question already has answers here: How can I plot a 3D-plane in Matlab? (4 answers) Closed 8 years ago. I would like to draw different triangles by using MATLAB shown in the figure below. Suppose that I have 3 vectors V1=[1 1 1], V2=[-1 1 1], v3=[-2 -2 -2]. How can I draw triangle with these vectors in 3D? ![enter image description here][1]
You can use plot3() like this: v1=[1 1 1]; v2=[-1 1 1]; v3=[-2 -2 -2]; triangle = [v1(:), v2(:), v3(:), v1(:)]; plot3(triangle(1, :), triangle(2, :), triangle(3, :)) xlabel('x'); ylabel('y'); zlabel('z'); This is the output: Edit: This is in order to plot axis: val = 5; %// Max value of axis axX = [0 0 0; val 0 0]; axY = [0 0 0; 0 val 0]; axZ = [0 0 0; 0 0 val]; plot3(axX(:, 1), axX(:, 2), axX(:, 3), 'k'); plot3(axY(:, 1), axY(:, 2), axY(:, 3), 'k'); plot3(axZ(:, 1), axZ(:, 2), axZ(:, 3), 'k'); text(val, 0, 0, 'x') text(0, val, 0, 'y') text(0, 0, val, 'z') view(3) In addition you can make the plot look like your reference image, adding these commands to above code: set(gca,'xtick',[], 'xcolor', 'w') set(gca,'ytick',[], 'ycolor', 'w', 'YDir','reverse') set(gca,'ztick',[], 'zcolor', 'w') view(45, 30) This is the result:
How can I make a contour plot doesn't overlap on the line plot in one axes?
I've made 2 plot in one axes using pushbutton in matlab guide, the first plot is line plot here is the plot http://i1275.photobucket.com/albums/y443/Kaito_Aokage/Capture2_zpsbc76be37.png?t=1403148417 code for line plot % X for i = 1.5:7; cur_x = i * 3.8; line([cur_x, cur_x], [0 5], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % Y for i = 2:7; cur_y = i * 4; line([0 4],[cur_y, cur_y], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % X2 for i = 1.5:7; cur_x2 = i * 3.8; line([cur_x2, cur_x2], [25 31], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % Y2 for i = 1:8; cur_y2 = i * 3.5; line([26 31],[cur_y2, cur_y2], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % X line( [5.7 cur_x], [5 5], 'color', 'r', 'LineWidth', 1.5); % Y line( [4 4], [8 cur_y], 'color', 'r', 'LineWidth', 1.5); % X2 line( [5.6 cur_x2], [25 25], 'color', 'r', 'LineWidth', 1.5); % Y2 line( [26 26], [3.5 cur_y2], 'color', 'r', 'LineWidth', 1.5); handles.axes2; grid on; hold on; axis([0 30 0 30]); and the second plot is contour plot http://i1275.photobucket.com/albums/y443/Kaito_Aokage/Capture3_zpsfd46dedf.png?t=1403148576 code for contour plot xMove = 3; yMove = 10; r = 30; rx = -r:0.1:r; ry = r:-0.1:-r; [x_coor, y_coor] = meshgrid(rx, ry); radius = sqrt(x_coor.^2+y_coor.^2); contourf(x_coor + xMove, y_coor + yMove, radius,'edgecolor','none'); xlabel('Widht'); ylabel('Long'); axis([0 30 0 30]); colorbar; caxis([0 10]); grid on; handles.axes2; set(gca,'layer','top'); hold on; Pushbutton Floor is line plot and Pushbutton AP1 is contour plot. When i try to push plot contour button after line plot button, the line plot overlap by contour plot. I want line plot doesn't overlap by contour plot so the line plot can be seen after i push contour plot button. I already try holdor set(gca,'layer','top)but its not working. what should i do?
in what order are you executing the above codes ? I first executed second code then the first code and this is my output Here is the whole code I executed, I had to remove the line handle.axis2; as it was throwing an error.( I am using matlab 2011) close all xMove = 3; yMove = 10; r = 30; rx = -r:0.1:r; ry = r:-0.1:-r; [x_coor, y_coor] = meshgrid(rx, ry); radius = sqrt(x_coor.^2+y_coor.^2); contourf(x_coor + xMove, y_coor + yMove, radius,'edgecolor','none'); xlabel('Widht'); ylabel('Long'); axis([0 30 0 30]); colorbar; caxis([0 10]); grid on; set(gca,'layer','top'); hold on; % X for i = 1.5:7; cur_x = i * 3.8; line([cur_x, cur_x], [0 5], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % Y for i = 2:7; cur_y = i * 4; line([0 4],[cur_y, cur_y], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % X2 for i = 1.5:7; cur_x2 = i * 3.8; line([cur_x2, cur_x2], [25 31], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % Y2 for i = 1:8; cur_y2 = i * 3.5; line([26 31],[cur_y2, cur_y2], 'color', 'r', 'LineWidth', 1.5); drawnow; end; % X line( [5.7 cur_x], [5 5], 'color', 'r', 'LineWidth', 1.5); % Y line( [4 4], [8 cur_y], 'color', 'r', 'LineWidth', 1.5); % X2 line( [5.6 cur_x2], [25 25], 'color', 'r', 'LineWidth', 1.5); % Y2 line( [26 26], [3.5 cur_y2], 'color', 'r', 'LineWidth', 1.5); grid on; axis([0 30 0 30]); hold off;