# rotating this curve 20degrees

5 ビュー (過去 30 日間)
Kang Min Kim 2020 年 1 月 17 日

t = 0:500;
dailyFluct = gallery('normaldata',size(t),2);
sdata = cumsum(dailyFluct) + 20 + t/1000;
mean(sdata)
figure
plot(t,sdata);
What should I do??
Thank you

#### 0 件のコメント

サインイン to comment.

### 回答 (3 件)

John D'Errico 2020 年 1 月 17 日

I would normally ask why not try it yourself, but I realize there are some issues that can easily catch you here.
H = plot(t,sdata);
First, you need to capture the handle to the object you just plotted. rotate uses that to do its work.
Next, you need to recognize the units on the axes are relatively different. When you plot a figure, MATLAB auto-scales the axes to make the plot look good, at least by some guess as to what looks good. So in my first figure, the y axis varies by roughly 40 units in y, but over a distance that is here roughly the same as about 400 units of travel on the x axis. So while we see all of the stuff that happens. you need to be VERY careful in what you see when you rotate that plot, with aes that are auto-scaled, by 20 degrees.
So, first, I'll force MATLAB to essentially disable that auto-scaling.
axis equal
axis tight
Now the figure is plotted with axes that have the same "units". As you can see in this version, there really is little variation in y, compared to the variation seen in x.
I did not really need to make the axes tight. But I did so here to make it clear what MATLAB did. Here, one unit of distance traveled in y is now comparable to distance traveled in x. That distinction is crucial to understanding what it means to rotate the plot.
The above difference is crucial, for example, if you plot a circle on a new figure in MATLAB, you will see what appears to be an ellipse! For example:
t = linspace(0,2*pi,100); plot(cos(t),sin(t),'-')
While we know the circle I plotted MUST absolutely be a circle, it looks like an ellipse. The plotting artifact is purely one of axis scaling, that even though the data is circular, if you are not careful with the plot, it need not look circular.
Anyway, now rotating the original figure by 20 degrees should makes sense. How do we use rotate? BE VERY CAREFUL! Why do I say that? Because if you do not, then you will get a result that does not actually look as if it has been rotated by 20 degrees.
What is the axis of rotation? What origin will the plot be rotated around?
I'll assume you wanted to rotate the curve itself, not the entire figure. So I'll arbitrarily choose the center of rotation to be the very first data point, with z==0 there, because rotate really thinks in 3 dimensions. If you don't supply a center of rotation, the result will be rotated around the center of the plot window! And that point need not even be on the curve.
The axis of rotation will be around the z axis. So we will spin the plot in the (x,y) plane. That axis of rotation will be given by the vector [0 0 1]. This time, I'll leave out the axis tight call, to make it clear that the first data point plotted is still the same. That could become confusing if I used axis tight.
H = plot(t,sdata);
axis equal
rotate(H,[0 0 1],20,[t(1),sdata(1),0])
So now the curve has been clearly rotated by +20 degrees around the origin [0,0] in the x-y plane. The result is a curve with still relatively little deviation from a straight line, but now at an inclination of 20 degrees more than it had in the beginning.
If you don't do as I said in the above steps, you can get what may be a confusing result, but really, it comes down to understanding how MATLAB does the scaling of their axes, and understanding what that implies when you want to rotate a curve in space.

#### 0 件のコメント

サインイン to comment.

Guillaume 2020 年 1 月 17 日
You seem to have found the correct function, so it's not clear what the problem is:
hline = plot(t, sdata);
rotate(hline, [0 0 1], 20); %or -20
You could also use a rotation matrix to rotate the actual inputs to plot:
rmat = [cosd(20), -sind(20); sind(20), cosd(20)]; %or -20
newcoords = rmat * [t; sdata];
plot(newcoords(1, :), newcoords(2, :));
Note that the two rotate around different points. By default rotate rotates around the centre of the plot box. You can change the rotation centre to be the axis origin:
rotate(hline, [0 0 1], 20, [0 0 0]);

#### 0 件のコメント

サインイン to comment.

darova 2020 年 1 月 17 日
Use rotation matrix
a = 15;
R = [cosd(a) sind(a);
-sind(a) cosd(a)];
v = R*[t(:) sdata(:)]';
plot(v(1,:),v(2,:))

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by