Change Y-Axis position to desired origin?
Afficher commentaires plus anciens
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 commentaires
madhan ravi
le 9 Juil 2020
What do you mean by 0-20? By the way format your code by pressing the code button so that it’s easy to read.
Elnaz P
le 9 Juil 2020
madhan ravi
le 9 Juil 2020
It would be better if you illustrate how it should look like using paint or something.
Elnaz P
le 9 Juil 2020
Sugar Daddy
le 10 Juil 2020
something like this

Réponses (2)
Sugar Daddy
le 9 Juil 2020
Modifié(e) : Sugar Daddy
le 9 Juil 2020
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 commentaires
madhan ravi
le 9 Juil 2020
Modifié(e) : madhan ravi
le 9 Juil 2020
+1, Ah ok now it’s clear 👌
Steven Lord
le 9 Juil 2020
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 commentaires
Elnaz P
le 9 Juil 2020
Steven Lord
le 10 Juil 2020
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))
Catégories
En savoir plus sur Orange dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
