Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a second graph instead of color coding in matlab

i just started with my master thesis and i already am in trouble with my capability/understanding of matlab.

The thing is, i have a trajectory on a surface of a planet/moon whatever (a .mat with the time, and the coordinates. Then i have some .mat with time and the measurement at that time.

I am able to plot this as a color coded trajectory (using the measurement and the coordinates) in scatter(). This works awesomely nice.

However my problem is that i need something more sophisticated. I now need to take the trajectory and instead of color-coding it, i am supposed to add the graph (value) of the measurement (which is given for each point) to the trajectory (which is not always a straight line). I will added a little sketch to explain what i want. The red arrow shows what i want to add to my plot and the green shows what i have.

enter image description here

like image 623
IceQueeny Avatar asked Dec 27 '12 11:12

IceQueeny


2 Answers

You can always transform your data yourself: (using the same notation as @Shai)

x = 0:0.1:10;
y = x;
m = 10*sin(x);

So what you need is the vector normal to the curve at each datapoint:

dx = diff(x); % backward finite differences for 2:end points
dx = [dx(1) dx]; % forward finite difference for 1th point
dy = diff(y);
dy = [dy(1) dy];
curve_tang = [dx ; dy];
% rotate tangential vectors 90° counterclockwise
curve_norm = [-dy; dx];
% normalize the vectors:
nrm_cn = sqrt(sum(abs(curve_norm).^2,1));
curve_norm = curve_norm ./ repmat(sqrt(sum(abs(curve_norm).^2,1)),2,1);

Multiply that vector with the measurement (m), offset it with the datapoint coordinates and you're done:

mx = x + curve_norm(1,:).*m;
my = y + curve_norm(2,:).*m;

plot it with:

figure; hold on
axis equal;
scatter(x,y,[],m);
plot(mx,my)

straightline_sine

which is imo exactly what you want. This example has just a straight line as coordinates, but this code can handle any curve just fine:

x=0:0.1:10;y=x.^2;m=sin(x);

parabole_sine

t=0:pi/50:2*pi;x=5*cos(t);y=5*sin(t);m=sin(5*t);

circle_sine

like image 126
Gunther Struyf Avatar answered Sep 22 '22 17:09

Gunther Struyf


If I understand your question correctly, what you need is to rotate your actual data around an origin point at a certain angle. This is pretty simple, as you only need to multiply the coordinates by a rotation matrix. You can then use hold on and plot to overlay your plot with the rotated points, as suggested in the comments.

Example

First, let's generate some data that resembles yours and create a scatter plot:

% # Generate some data
t = -20:0.1:20;
idx = (t ~= 0);
y = ones(size(t));
y(idx) = abs(sin(t(idx)) ./ t(idx)) .^ 0.25;

% # Create a scatter plot
x = 1:numel(y);
figure
scatter(x, x, 10, y, 'filled')

Now let's rotate the points (specified by the values of x and y) around (0, 0) at a 45° angle:

P = [x(:) * sqrt(2), y(:) * 100] * [1, 1; -1, 1] / sqrt(2);

and then plot them on top of the scatter plot:

hold on
axis square
plot(P(:, 1), P(:, 2))

Note the additional things have been done here for visualization purposes:

  1. The final x-coordinates have been stretched (by sqrt(2)) to the appropriate length.
  2. The final y-coordinates have been magnified (by 100) so that the rotated plot stands out.
  3. The axes have been squared to avoid distortion.

This is what you should get:

enter image description here

like image 36
Eitan T Avatar answered Sep 23 '22 17:09

Eitan T