Effacer les filtres
Effacer les filtres

Can I rotate a contour plot with the rotate function?

79 vues (au cours des 30 derniers jours)
Margaux le 5 Juil 2023
Commenté : ProblemSolver le 5 Juil 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 commentaires
Star Strider
Star Strider le 5 Juil 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);
[c,hcf] = contourf(X,Y,Z);
[c,hcf] = contourf(X,Y,Z);
rotate(hcf, [0 0 1],122.5)
x = 1:10;
y = randn(1,10);
hp = plot(x, y, [1 10], [0 0]);
hp = plot(x, y, [1 10], [0 0]);
rotate(hp, [0 0 1], 22.5)
Margaux le 5 Juil 2023
So we cannot use the rotate function on a contour plot. Can it be used on only one of its elements so that it can be plotted after?

Connectez-vous pour commenter.

Réponse acceptée

ProblemSolver le 5 Juil 2023
Modifié(e) : ProblemSolver le 5 Juil 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
contourf(X, Y, Z);
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
contourf(resizedRotatedXData, resizedRotatedYData, rotatedZ);
% Adjust the axes limits to fit the resized rotated plot
axis auto;
I hope this helps!
  4 commentaires
Margaux le 5 Juil 2023
Modifié(e) : Margaux le 5 Juil 2023
so, does this rotate the whole contour? Like with the axes?
Also, does it have to plot the old contour plot everytime?
ProblemSolver le 5 Juil 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;

Connectez-vous pour commenter.

Plus de réponses (1)

Aniketh le 5 Juil 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);
% 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.
  1 commentaire
Margaux le 5 Juil 2023
Does gca automatically get the axes from the figure?
Also, I tried to play around with the angle rotation and nothing is changing

Connectez-vous pour commenter.


En savoir plus sur Contour Plots dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by