Rotate the plot at 45 degree
61 views (last 30 days)
Show older comments
Can anyone tell me how to rotate the plot from the following image so that the now the linear reference line will be a straight vertical line, and the red and green line will fall on the left side of the plot?
for a reference the red and green line is just a data that I plot with different slope.
1 Comment
Answers (2)
Jakub Rysanek
on 4 Oct 2016
I would simply rotate all data using the planar (2D) transform with respect to the origin:
angle = pi/4;
reference_line = [0 1 2 3 4 5 6];
data_x = [0 1 2 3 4 5 6];
data_y = [0 1.1 2.2 3.3 4.4 5.5 6.6];
% 2D rotation
x2 = data_x*cos(angle)-data_y*sin(angle);
y2 = data_y*cos(angle)+data_x*sin(angle);
figure;
hold on;
p1=plot(reference_line,reference_line,'marker','none','linestyle','--');
p2=plot(data_x,data_y,'-r');
p3=plot(x2,y2,'-b');
p4=plot(zeros(1,7),reference_line,'-k');
set(gca,'ylim',[0 6],'xlim',[-6 6]);
axis square;
legend([p1,p2,p3,p4],{'Reference line','Original data','Rotated data','Vertical line'},'Location','southwest');
0 Comments
Walter Roberson
on 29 Sep 2016
Create an hgtransform, set the axes parent to be the transform, set the rotation matrix for the transform to have the appropriate rotation.
Note: this will transform the axes too, rotating the entire plot.
If you want just the data to be rotated then the easiest single way would probably:
[th, r] = cart2pol(x, y);
[nx, ny] = pol2cart(th+pi/4, r);
Your x and y arrays will need to be the same size for this to work, so if you have multiple columns in your y array (one column per line) and a vector x, then use repmat(x(:), 1, size(y,2)) as the x for the rotation.
1 Comment
Ryan Webb
on 1 Sep 2022
Can you clarify on "set the axes parent to be the transform"
set(ax,'Parent',t)
gives the error:
Error using matlab.graphics.axis.Axes/set
Axes cannot be a child of Transform.
See Also
Categories
Find more on 2-D and 3-D Plots 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!