Can I rotate a contour plot with the rotate function?
59 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Margaux
el 5 de Jul. de 2023
Comentada: ProblemSolver
el 5 de Jul. de 2023
Good morning, I have been attempting to rotate my contourf plot using the rotate function with no success. I am currently rotating the Z element by using the rot90 function but now, I want to be able to rotate a different angle, a custom angle such as 22.5 or 19 or something like that. Is it possible?
Thank you!
2 comentarios
Star Strider
el 5 de Jul. de 2023
The documentation states: ‘Specify h as a surface, patch, line, text, or image object.’
Apparently it doesn’t work on contour plots —
[X,Y,Z] = peaks(50);
figure
[c,hcf] = contourf(X,Y,Z);
figure
[c,hcf] = contourf(X,Y,Z);
rotate(hcf, [0 0 1],122.5)
x = 1:10;
y = randn(1,10);
figure
hp = plot(x, y, [1 10], [0 0]);
figure
hp = plot(x, y, [1 10], [0 0]);
rotate(hp, [0 0 1], 22.5)
.
Respuesta aceptada
ProblemSolver
el 5 de Jul. de 2023
Editada: ProblemSolver
el 5 de Jul. de 2023
As @Star Strider said, that it does not work on the contour plots. However you can do something like this as an example:
% Create example data
x = linspace(-10, 10, 61);
y = linspace(-10, 10, 61);
[X, Y] = meshgrid(x, y);
Z = peaks(X, Y);
% Create contour plot
figure(1)
contourf(X, Y, Z);
colorbar;
Once you have generated a simple contour plot, then you are rotate something like, but this include extrapolation of certain values which might not be an exceptable as this will be a complete prediction.
% Rotate the plot by a custom angle
angle = 22.5; % Custom angle in degrees
% Convert angle to radians
theta = deg2rad(angle);
% Define rotation matrix
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
% Get the current plot data
xData = get(gca, 'XLim');
yData = get(gca, 'YLim');
% Adjust axes limits to include all data
xMin = min(xData);
xMax = max(xData);
yMin = min(yData);
yMax = max(yData);
% Create new grid covering the adjusted axes limits
adjustedX = linspace(xMin, xMax, size(Z, 2));
adjustedY = linspace(yMin, yMax, size(Z, 1));
[adjustedXData, adjustedYData] = meshgrid(adjustedX, adjustedY);
% Rotate the adjusted grid coordinates
rotatedData = R * [adjustedXData(:)'; adjustedYData(:)'];
% Reshape the rotated data back to the original grid size
rotatedXData = reshape(rotatedData(1, :), size(adjustedXData));
rotatedYData = reshape(rotatedData(2, :), size(adjustedYData));
% Interpolate Z values on the rotated grid
rotatedZ = interp2(X, Y, Z, rotatedXData, rotatedYData, 'linear', 0);
This the part that you need to include for adjusting the roatated grid to mathc the original size
% Resize the rotated grid to match the original size
resizedRotatedX = linspace(xMin, xMax, size(Z, 2));
resizedRotatedY = linspace(yMin, yMax, size(Z, 1));
[resizedRotatedXData, resizedRotatedYData] = meshgrid(resizedRotatedX, resizedRotatedY);
And then plot it:
% Plot the resized rotated contour
figure(2)
contourf(resizedRotatedXData, resizedRotatedYData, rotatedZ);
colorbar;
% Adjust the axes limits to fit the resized rotated plot
axis auto;
I hope this helps!
4 comentarios
ProblemSolver
el 5 de Jul. de 2023
so, does this rotate the whole contour? Like with the axes?
The axes are not rotating, the matrix values are being rotated by 22.5 degress.
Also, does it have to plot the old contour plot everytime?
No, you can remove or comment these lines from the code to not plot the original contour.
% Create contour plot
% figure(1)
% contourf(X, Y, Z);
% colorbar;
Más respuestas (1)
Aniketh
el 5 de Jul. de 2023
Yes, it is possible to rotate a contour plot in MATLAB by custom angles using the rotate function. Instead of using rot90 which rotates by 90 degrees, you can use the rotate function with a custom angle of rotation.
Here's an example of how you can rotate a contourf plot by a custom angle:
% Generate example data
[X, Y] = meshgrid(-5:0.5:5);
Z = peaks(X, Y);
% Create the contour plot
contourf(X, Y, Z);
colorbar;
% Custom rotation angle (in degrees)
rotationAngle = 22.5;
% Get the current axes handle
ax = gca;
% Apply rotation to the axes
rotate(ax, [0 0 1], rotationAngle);
In this example, we first generate example data using peaks, and then create a contourf plot using contourf(X, Y, Z). After that, we define a custom rotation angle (rotationAngle) of 22.5 degrees.
Next, we obtain the current axes handle using gca. The rotate function is then called on the axes object (ax) with the rotation parameters: [0 0 1] specifies the rotation axis as the Z-axis (perpendicular to the plot), and rotationAngle is the desired rotation angle.
Ver también
Categorías
Más información sobre Contour Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!