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?

9 次查看(过去 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)

采纳的回答

Hassaan
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
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
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 个评论
Rabih Sokhen
Rabih Sokhen 2024-1-18
编辑:Rabih Sokhen 2024-1-19
Thank you for your response. Your code served as inspiration, and I have created the following code based on it:
code:
clear all
clc
a = [0 0 1; 1 1 0; 1 0 1];
x = linspace(-1, 1, 3);
y = linspace(1, 5, 3);
subplot 121
img2(x,y,a)
subplot 122
img2(-x, -y, a)
function img2(varargin)
if numel(varargin) == 1
a = varargin{1};
a=squeeze(a); % squeeze will ilimate the dimention of size 1 to do the plot
[xx ,yy] = size(a);
x = linspace(1,yy,yy);
y = linspace(1,xx,xx);
elseif numel(varargin) == 3
[x ,y, a] = deal(varargin{1:3});
else
error('IMG: incorrect number of arguments')
end
[xx , yy] = size(a);
if (xx==numel(y) && yy==numel(x)) % this condition is not necessary
dx = (x(end)-x(1))/(yy-1);
dy = (y(end)-y(1))/(xx-1);
xg = linspace(x(1)-dx/2,x(end)+dx/2,yy+1);
yg = linspace(y(1)-dy/2,y(end)+dy/2,xx+1);
if y(2)>y(1) && x(2)>x(1)
imagesc(x,y,a);
set(gca,'YDir','normal');
end
if y(2)<y(1)
a=flipud(a);
end
if(x(2)<x(1))
a=fliplr(a);
end
if(x(2)<x(1) || y(2)<y(1))
imagesc(x,y,a);
set(gca,'YDir','normal');
end
if y(2)<y(1)
set(gca, 'YDir', 'reverse'); % This flips the y-axis direction
end
if x(2)<x(1)
set(gca, 'XDir', 'reverse'); % This flips the y-axis direction
end
%add pixel info with position
hp=impixelinfo;
% set(hp,"Position",[1720 1 200 20]);
hold on;
%option
hm = mesh(xg,yg,zeros([xx yy]+1));
hm.FaceColor = 'none';
hm.EdgeColor = 'k';
hm.EdgeAlpha = 0.5;
hold off
else
error('IMG: Length of X and Y must match the size of A')
end
end

请先登录,再进行评论。

更多回答(1 个)

Amish
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!

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by