How can I incorporate both the x-axis and a reversed y-axis without... (2024)

28 views (last 30 days)

Show older comments

Rabih Sokhen on 18 Jan 2024

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus

Commented: Rabih Sokhen on 19 Jan 2024

Accepted Answer: Hassaan

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Hassaan on 18 Jan 2024

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#answer_1392191

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#answer_1392191

Edited: Hassaan on 18 Jan 2024

Open in MATLAB Online

@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');

How can I incorporate both the x-axis and a reversed y-axis without... (3)

-----------------------------------------------------------------------------------------------------------------------------------------------------

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 Comments

Show NoneHide None

Rabih Sokhen on 18 Jan 2024

Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#comment_3033626

  • Link

    Direct link to this comment

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#comment_3033626

Edited: Rabih Sokhen on 19 Jan 2024

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

Rabih Sokhen on 19 Jan 2024

Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#comment_3034451

  • Link

    Direct link to this comment

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#comment_3034451

How can I incorporate both the x-axis and a reversed y-axis without... (6)

Sign in to comment.

More Answers (1)

Amish on 18 Jan 2024

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#answer_1392161

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2071481-how-can-i-incorporate-both-the-x-axis-and-a-reversed-y-axis-without-altering-the-plot-but-only-adjus#answer_1392161

Open in MATLAB Online

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsLine Plots

Find more on Line Plots in Help Center and File Exchange

Tags

  • axis
  • flip
  • imagesc

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How can I incorporate both the x-axis and a reversed y-axis without... (8)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How can I incorporate both the x-axis and a reversed y-axis without... (2024)
Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6642

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.