I have a contour plot (Image1). I need to rotate it through a certain angle. If I save it as an image and then rotate it using transformation(or rotation) matrix, the whole plot including the axes are rotated(Image2). If I use imrotate for the matrix I have of the contour, the resultant contour doesn't actually rotate by the same angle which I specify (37 deg in the example, Image3) and more over sometimes the width of the plot decreases.
Can someone please help me overcome the problem. I want rotation of the contour plot without reducing the width and rotating exactly by the specified angle. Is it the aliasing effect?
Any help will be much appreciated. Many Thanks

 Respuesta aceptada

Grzegorz Knor
Grzegorz Knor el 9 de Jul. de 2014

0 votos

Look at this example:
a = 37;
[x,y] = meshgrid(linspace(-4,4,30));
z = exp(-x.^2/15-y.^2);
contour(x,y,z)
xlim([-5 5])
ylim([-5 5])
x = x*cosd(a) - y*sind(a);
y = y*cosd(a) + x*sind(a);
figure
contour(x,y,z)
xlim([-5 5])
ylim([-5 5])
Is that what you want?

6 comentarios

Mahi Nazir
Mahi Nazir el 9 de Jul. de 2014
Thank you so much for your reply
If you take a look at the results from your example, The angle(taken at the center of the plot) is only 25 deg and not 37 (Image attached).... and moreover the width of the contour is reduced as well. This is what happens to my plot as well. Any clues why does this happen.
Grzegorz Knor
Grzegorz Knor el 9 de Jul. de 2014
The angle is 25 degree, because units on axes are not equal. Type
axis equal
after plots.
Grzegorz Knor
Grzegorz Knor el 9 de Jul. de 2014
Ok, I think that now I understand your problem ;) So once again see at example:
a = 37;
[x,y] = meshgrid(linspace(-4,4,30));
z = exp(-x.^2/15-y.^2);
contour(x,y,z)
xlim([-5 5])
ylim([-5 5])
axis equal
figure
[C,h] = contour(x,y,z);
rotate(get(h,'children'),[0 0 1],a)
xlim([-5 5])
ylim([-5 5])
axis equal
Original plot:
After rotation:
Mahi Nazir
Mahi Nazir el 9 de Jul. de 2014
This is super! That's what I exactly wanted. Thank you so much :) Cheers
Mattalabba
Mattalabba el 15 de Nov. de 2018
Is it correct that this method does not work anymore in MATLAB 2015 and newer?
I recieve the error 'Index exceeds matrix dimensions. Error in rotate (line 31): ax = ancestor(h(1),'axes');'
When I change
rotate(get(h,'children'),[0 0 1],a)
to
rotate(h,[0 0 1],a)
I get no error, but also nothing changes on the contourplot.
Or am I misunderstanding something?
Alexander
Alexander el 17 de Dic. de 2025
Editada: Alexander el 17 de Dic. de 2025
In the first example the y-transformation should include the original x values (i.e. prior to assignment):
% field to be rotated
a = 80;
[x,y] = meshgrid(linspace(-4,4,30));
z = exp(-x.^2/15-y.^2);
[~,h] = contour(x,y,z);
xlim([-5 5])
ylim([-5 5])
% use original x values in y-transformation
x_orig = x;
x = x*cosd(a) - y*sind(a);
y = y*cosd(a) + x_orig*sind(a);
figure
contour(x,y,z)
xlim([-5 5])
ylim([-5 5])
Otherwise, the contour plot will be shear-transformed.
% accepted answer
[x,y] = meshgrid(linspace(-4,4,30));
x = x*cosd(a) - y*sind(a);
y = y*cosd(a) + x*sind(a);
figure
contour(x,y,z)
xlim([-5 5])
ylim([-5 5])
Furthermore, as @Mattalabba mentioned
rotate(get(h,'children'),[0 0 1],a)
has no effect on contour objects. Use
[x,y] = meshgrid(linspace(-4,4,30));
figure
g = hgtransform;
contour(x, y, z, 'Parent', g);
set(g, 'Matrix', makehgtform('zrotate', deg2rad(a)));

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 9 de Jul. de 2014

Editada:

el 17 de Dic. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by