Can I rotate a contour plot with the rotate function?
Show older comments
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 Comments
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)
.
Margaux
on 5 Jul 2023
Accepted Answer
More Answers (1)
Aniketh
on 5 Jul 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.
Categories
Find more on Contour 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!




