- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
How can I incorporate both the x-axis and a reversed y-axis without altering the plot but only adjusting the orientation of the axes?
7 views (last 30 days)
Show older comments
Hello everyone,
I hope you're all doing well. I attempted to run the provided code and noticed that associating an x and y axis, or an x-axis with a -y axis, results in the matrix plot being flipped, causing the y-axis to display in descending order. My goal is to keep the matrix unchanged and only flip the labeling of the y-axis based on the provided y values.
Thank you in advance for your assistance.
Best regards.
Code:
clear all
clc
a=[0 0 1; 1 1 0; 1 0 1];
x=linspace(-1,1,3);
y=linspace(-2,2,3);
subplot 121
imagesc(x,y,a)
subplot 122
imagesc(x,-y,a)
0 Comments
Accepted Answer
Hassaan
on 18 Jan 2024
Edited: Hassaan
on 18 Jan 2024
@Rabih Sokhen Try this.
clear all;
clc;
a = [0 0 1; 1 1 0; 1 0 1];
x = linspace(-1, 1, 3);
y = linspace(2, -2, 3); % Define y from positive to negative
% First subplot with y-axis from 2 to -2
subplot(121);
imagesc(x, y, a); % Plot the data
set(gca, 'YDir', 'normal'); % Make sure the y-axis starts from the bottom
title('Y Axis from 2 to -2');
xlabel('x');
ylabel('y');
% Second subplot with y-axis labels reversed
subplot(122);
imagesc(x, y, a); % Plot the data
set(gca, 'YDir', 'normal'); % Keep the y-axis direction normal
% Get the current y-tick locations
yticks = get(gca, 'YTick');
% Map the y-tick locations to the reversed y-values
% The new y-tick labels should match the reversed y values but the data should stay the same
new_yticklabels = linspace(max(y), min(y), numel(yticks));
% Set the y-tick labels to the reversed y-values
set(gca, 'YTickLabel', new_yticklabels);
title('Y Axis Reversed');
xlabel('x');
ylabel('y');
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
2 Comments
More Answers (1)
Amish
on 18 Jan 2024
Hi Rabih,
I understand that you want the matrix to reamin the same while the y-axis is flipped. In order to flip the labeling of the y-axis while keeping the matrix plot unchanged, you can use the "set(gca, 'YDir', 'normal')" or "set(gca, 'YDir', 'reverse')" command after plotting your data with "imagesc" function. This command changes the direction of the y-axis without altering the data in the matrix.
Here is the modified code to do the same:
clear all
clc
a = [0 0 1; 1 1 0; 1 0 1];
x = linspace(-1, 1, 3);
y = linspace(-2, 2, 3);
% Plot with normal y-axis
subplot(121)
imagesc(x, y, a);
set(gca, 'YDir', 'normal'); % This keeps the y-axis direction as is
title('Normal Y-Axis');
% Plot with flipped y-axis labels
subplot(122)
imagesc(x, y, a); % Keep the matrix 'a' the same
set(gca, 'YDir', 'reverse'); % This flips the y-axis direction
title('Flipped Y-Axis');
Here are some documentation links for your reference:
imagesc : https://www.mathworks.com/help/matlab/ref/imagesc.html
gca : https://www.mathworks.com/help/matlab/ref/gca.html
set :https://www.mathworks.com/help/matlab/ref/set.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Graphics Object Properties 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!