- 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?
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Hassaan
2024-1-18
编辑:Hassaan
2024-1-18
@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.
更多回答(1 个)
Amish
2024-1-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!