3

I'm currently trying to shift the axes positions in a Matlab figure. I'd like to achieve something similar to this (which was done in gnuplot):

enter image description here

I don't have any idea whether this is possible at all, or where I might find an answer, so any help will be much appreciated.

Schnigges
  • 1,284
  • 2
  • 24
  • 48
  • This link would help I hope.http://www.mathworks.co.kr/kr/help/matlab/creating_plots/individual-axis-control.html – Suvi Murugan Jul 31 '13 at 12:48
  • Post your code so far? – Dan Jul 31 '13 at 12:49
  • I haven't any code so far. I guess a simple surface plot will be sufficient to test this. – Schnigges Jul 31 '13 at 12:52
  • `plot3` is probably a good start – Buck Thorn Jul 31 '13 at 13:33
  • 2
    hm...the `Individual Axes Control` Matlab Doc site has a lot of stuff in it, but not exactly what I'm looking for. I need to re-position the whole axes and not just the ticks, tick names, etc. And I don't really understand why `plot3` should be a great start, since I know how plotting in Matlab works. My initial question was a bit more specific. – Schnigges Jul 31 '13 at 14:01

1 Answers1

6

Hmm....

So let's plot:

x = zeros(1,21); y = -10:10; z = y/2;
figure; plot3(x,y,z); % a line from (0,-10,-5) to (0,10,5) similar to the example

just the line plotted

Well, one problem is that matlab doesn't automatically plot the coordinate axis as you've shown there. This is discussed here: How to show x and y axes in a MATLAB graph?

To plot those (in 3D), a cheap solution is:

locs = axis; % get current axis boundaries
hold on; 
plot3([locs(1) locs(2)], [0 0], [0 0]); %plot xaxis, a line between(-x,0,0) and (x,0,0);
plot3([0 0], [locs(3) locs(4)], [0 0]); %plot y axis, the line (0,-y,0) and (0,y,0);
plot3([0 0], [0 0], [locs(5) locs(6)]); % plot z axis
hold off

now with axis added

Just like that Gnu plot, the 3D matlab plot is "in a box." Unlike the Gnu plot, the matlab box isn't outlined. If you want to outline that you'll have to draw those lines too...ugh.

% lets plot the 12 lines to make this box in black ('k');
hold on;
% hold x constant and plot 4 parallel-to-x lines;
plot3([locs(1) locs(2)], [locs(3) locs(3)], [locs(5) locs(5)],'k'); % (-x,-y,-z) to (x,-y,-z)
plot3([locs(1) locs(2)], [locs(3) locs(3)], [locs(6) locs(6)],'k');
plot3([locs(1) locs(2)], [locs(4) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(1) locs(2)], [locs(4) locs(4)], [locs(6) locs(6)],'k');
% plot parallel-to-y lines
plot3([locs(1) locs(1)], [locs(3) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(1) locs(1)], [locs(3) locs(4)], [locs(6) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(4)], [locs(6) locs(6)],'k');
% plot parallel-to-z lines
plot3([locs(1) locs(1)], [locs(3) locs(3)], [locs(5) locs(6)],'k');
plot3([locs(1) locs(1)], [locs(4) locs(4)], [locs(5) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(3)], [locs(5) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(4) locs(4)], [locs(5) locs(6)],'k');
hold off;

Now we have the box;

now with a box

If we want just the area y>0,z>0; we can use the axis command. Using axis after plotting all those other lines really messed everything up for me, so I would decide on your limits at the beginning.

All together:

figure; 
plot3(x,y,z); % a line from (0,-10,-5) to (0,10,5) similar to the example
locs = axis;
axis([locs(1) locs(2) 0 locs(4) 0 locs(6)]);
locs = axis;

hold on; 
% plot axis
plot3([locs(1) locs(2)], [0 0], [0 0]); %plot xaxis, a line between(-x,0,0) and (x,0,0);
plot3([0 0], [locs(3) locs(4)], [0 0]); %plot y axis, the line (0,-y,0) and (0,y,0);
plot3([0 0], [0 0], [locs(5) locs(6)]); % plot z axis

% plot box
% hold x constant and plot 4 parallel-to-x lines;
plot3([locs(1) locs(2)], [locs(3) locs(3)], [locs(5) locs(5)],'k'); % (-x,-y,-z) to (x,-y,-z)
plot3([locs(1) locs(2)], [locs(3) locs(3)], [locs(6) locs(6)],'k');
plot3([locs(1) locs(2)], [locs(4) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(1) locs(2)], [locs(4) locs(4)], [locs(6) locs(6)],'k');
% plot parallel-to-y lines
plot3([locs(1) locs(1)], [locs(3) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(1) locs(1)], [locs(3) locs(4)], [locs(6) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(4)], [locs(5) locs(5)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(4)], [locs(6) locs(6)],'k');
% plot parallel-to-z lines
plot3([locs(1) locs(1)], [locs(3) locs(3)], [locs(5) locs(6)],'k');
plot3([locs(1) locs(1)], [locs(4) locs(4)], [locs(5) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(3) locs(3)], [locs(5) locs(6)],'k');
plot3([locs(2) locs(2)], [locs(4) locs(4)], [locs(5) locs(6)],'k');
hold off;

all done

I'm sure you can make it better, but I think that's a really good start. I would put all that mess into a function to save the typing.

Community
  • 1
  • 1
Frederick
  • 1,271
  • 1
  • 10
  • 29