Coming up with a Unique Matlab Graphics / Plotting Solution
2 views (last 30 days)
Show older comments
Hello,
I currently have Matlab plotting a series of lines, showing the line moving in time according to data I have collected. The plot simply pauses for a timestep between each new plot. I have provided an example in the screenshots below.
What I would like to do, however, is ultimately plot that line as the bottom edge of a box (see photoshopped example). I don't have a good idea of the route I would take to do this efficiently though. I could use some more experienced Matlab-user insight, even to point me in the right direction.
Thanks very much
Current MATLAB solution:
Photoshopped example of what I want:
0 Comments
Answers (2)
Star Strider
on 11 Nov 2015
I’m not certain that I understand the question, but the patch function is the obvious answer. To use it, you have to give the function a circumscribed area in x and y to fill. This is generally not difficult, but can take some experimentation to get to work correctly.
4 Comments
Steven Lord
on 11 Nov 2015
If you want to use a patch, the hgtransform approach described in this post on Mike's blog may be of interest and/or use to you.
Mike Garrity
on 11 Nov 2015
Edited: Mike Garrity
on 11 Nov 2015
You've got some good ideas above, but I'd like to mention one thing that you're going to have to worry about. When you've got a rotated rectangle, you want the angles to all come out at 90 degrees. To do this, you're going need to worry about the DataAspectRatio.
Let's look at an example.
I wrote the following simple function:
function box_beside(x,y,w)
valong = [x(2)-x(1), y(2)-y(1)];
vperp = [-valong(2), valong(1)] * w / norm(valong);
fill([x(1), x(2), x(2)+vperp(1), x(1)+vperp(1)], ...
[y(1), y(2), y(2)+vperp(2), y(1)+vperp(2)], ...
[.75 .75 .75])
It takes a 1x2 X and a 1x2 Y (just like you'd give to line) and draws a rectangle of width w next to it. Let's try calling it:
rng default
x = randn(1,2);
y = randn(1,2);
line(x,y,'Color','r','LineWidth',3)
hold on
box_beside(x,y,1/10)
The result looks like this:
That doesn't look like a rectangle, does it? The problem is that our YLim is more than twice as big as our XLim. Rotating objects in a space with a non-uniform scale doesn't preserve angles.
The easiest way out of this is define the problem away. You can do this like so:
axis equal
The axis equal command simply tells MATLAB that you want the scale to be uniform. If you do this, you won't have to worry about this issue.
If you can't get away with axis equal, then you're going to have to compensate for the nonuniform scale. This involves looking at the DataAspectRatio and PlotBoxAspectRatio properties of the axes and compensating for them in that line where I computed vperp.
Does that make sense?
0 Comments
See Also
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!