MATLAB Answers

Change Y-Axis position to desired origin?

67 views (last 30 days)
Elnaz P
Elnaz P on 9 Jul 2020 at 11:06
Commented: Steven Lord on 10 Jul 2020 at 13:09
Hi,
could someone help me how I can change origin point of y axis to (0-20) point. In fact I want to shift Y axis to point (0-20) but I don't have any idea.
hold on
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
origin = (0-20);
n= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140";"-100-120";"-80-100";"-60-80";"-40-60";"-20-40";"0-20";"0+20";"+20+40";"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";"+180+200";"+200+220";"+220+240"];
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.032258064516129,0.548387096774194,0.387096774193548,0.032258064516129,0,0,0,0,0,0,0,0,0,0];
y2 = [0.032258065,0.064516129,0,0,0.064516129,0.064516129,0.032258065,0.064516129,0.129032258,0.225806452,0.161290323,0.096774194,0.032258065,0.032258065,0,0,0,0,0,0,0,0,0,0,0];
y3 = [0,0,0,0.025641026,0.025641026,0.051282051,0.128205128,0,0,0.051282051,0,0.076923077,0.307692308,0.128205128,0,0.025641026,0,0.051282051,0.025641026,0,0.051282051,0.025641026,0.025641026,0,0];
x = categorical(n, n);
plot(x,y1,'-ko', x,y2,'-ks',x,y3,'-k^','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',7);
hold off

  5 Comments

Show 2 older comments
madhan ravi
madhan ravi on 9 Jul 2020 at 11:40
It would be better if you illustrate how it should look like using paint or something.
Elnaz P
Elnaz P on 9 Jul 2020 at 12:02
Ok, I hope this will be more clear

Sign in to comment.

Answers (2)

Sugar Daddy
Sugar Daddy on 9 Jul 2020 at 11:09
Edited: Sugar Daddy on 9 Jul 2020 at 11:13

Scale Up data

hold on
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
origin = (0-20);
n= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140";"-100-120";"-80-100";"-60-80";"-40-60";"-20-40";"0-20";"0+20";"+20+40";"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";"+180+200";"+200+220";"+220+240"];
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.032258064516129,0.548387096774194,0.387096774193548,0.032258064516129,0,0,0,0,0,0,0,0,0,0];
y2 = [0.032258065,0.064516129,0,0,0.064516129,0.064516129,0.032258065,0.064516129,0.129032258,0.225806452,0.161290323,0.096774194,0.032258065,0.032258065,0,0,0,0,0,0,0,0,0,0,0];
y3 = [0,0,0,0.025641026,0.025641026,0.051282051,0.128205128,0,0,0.051282051,0,0.076923077,0.307692308,0.128205128,0,0.025641026,0,0.051282051,0.025641026,0,0.051282051,0.025641026,0.025641026,0,0];
x = categorical(n, n);
mv = max(max([y1;y2;y3]));
y1 = y1/mv*20;
y2 = y2/mv*20;
y3 = y3/mv*20;
plot(x,y1,'-ko', x,y2,'-ks',x,y3,'-k^','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',7);

Change Ytick Label

hold on
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
origin = (0-20);
n= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140";"-100-120";"-80-100";"-60-80";"-40-60";"-20-40";"0-20";"0+20";"+20+40";"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";"+180+200";"+200+220";"+220+240"];
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.032258064516129,0.548387096774194,0.387096774193548,0.032258064516129,0,0,0,0,0,0,0,0,0,0];
y2 = [0.032258065,0.064516129,0,0,0.064516129,0.064516129,0.032258065,0.064516129,0.129032258,0.225806452,0.161290323,0.096774194,0.032258065,0.032258065,0,0,0,0,0,0,0,0,0,0,0];
y3 = [0,0,0,0.025641026,0.025641026,0.051282051,0.128205128,0,0,0.051282051,0,0.076923077,0.307692308,0.128205128,0,0.025641026,0,0.051282051,0.025641026,0,0.051282051,0.025641026,0.025641026,0,0];
x = categorical(n, n);
mv = max(max([y1;y2;y3]));
plot(x,y1,'-ko', x,y2,'-ks',x,y3,'-k^','LineWidth',1,'MarkerEdgeColor','k','MarkerFaceColor','k','MarkerSize',7);
ax.YTickLabel= string(linspace(0,20,length(ax.YTickLabel)));

  2 Comments

madhan ravi
madhan ravi on 9 Jul 2020 at 11:10
+1, Ah ok now it’s clear 👌
Elnaz P
Elnaz P on 9 Jul 2020 at 11:14
Yes I gonna shift Y axis from left on point 0-20, but by change Ytick Label it doesn't work yet

Sign in to comment.


Steven Lord
Steven Lord on 9 Jul 2020 at 12:12
I don't believe the axis ruler that is used when you plot using categorical data supports moving the axis location. In the situation you described we humans can see a reasonable definition for what 'origin' should probably mean (the bin whose label includes 0) but MATLAB can't see that (why is the bin whose name starts with the character '0' special?) In addition, if I were to try to place the axis at the 'origin' for the following plot where should the axis be placed and why should it be placed there?
c = ["cat", "broccoli", "sapphire"];
x = categorical(c, c);
h = plot(x, [4 7 1]);
ax = ancestor(h, 'axes');
ax.YAxisLocation = "origin";

  2 Comments

Elnaz P
Elnaz P on 9 Jul 2020 at 12:42
If I had ability to move the axis, it was more easy to analyze the data, in this plot data between 0 to -20 is origin. After zero, positive starts and before that negative. However thanks for your replay
Steven Lord
Steven Lord on 10 Jul 2020 at 13:09
You can't move the axis, but you could put a dividing line.
n= ["-240-260";"-220-240";"-200-220";"-180-200";"-160-180";"-140-160";"-120-140"; ...
"-100-120";"-80-100";"-60-80";"-40-60";"-20- 40";"0-20";"0+20";"+20+40";...
"+40+60";"+60+80";"+80+100";"+100+120";"+120+140";"+140+160";"+160+180";...
"+180+200";"+200+220";"+220+240"];
y1 = [0,0,0,0,0,0,0,0,0,0,0,0.04,0.32,0.56,0.08,0,0,0,0,0,0,0,0,0,0];
y2 = [0,0.05,0.05,0.05,0,0.1,0,0,0.2,0.3,0.1,0.1,0.05,0,0,0,0,0,0,0,0,0,0,0,0];
x=categorical(n, n);
plot(x,y1,'r--o', x,y2,'k--o');
% Category 13 in x is "0-20"
xline(x(13))

Sign in to comment.