How to shift xaxis location to origin (0,0) ?

10 ビュー (過去 30 日間)
Nada Zamri
Nada Zamri 2016 年 6 月 20 日
編集済み: dpb 2016 年 6 月 20 日
Attached is my graph that I have plotted. How to shift the x axis position so that it will be plotted on y=0? Anyway I am using R2013a version of MATLAB.
thank you

採用された回答

dpb
dpb 2016 年 6 月 20 日
編集済み: dpb 2016 年 6 月 20 日
OK, got a minute now...
hAx=axes; % a new axes
plot(randn(10,1)) % put some data on it...
yl=ylim; % get y-axis limits
pos=get(hAx,'position'); % and axes position vector
yrat=yl(end)/(yl(end)-yl(1)); % fractional position of origin relative y range
yht=yrat*pos(4); % adjusted second axes height
pos(2)=pos(2)+pos(4)-yht; % new bottom is top position {pos(2)+pos(4)} less height
pos(4)=yht; % and new height into vector
hAx(2)=axes('position',pos,'color','none'); % second axes with this position an no color
set(hAx(2),'ytick',[]) % clear unwanted ticks from showing...
set(hAx(1),'xticklabel',[]) % clear labels on first x-axis
linkaxes(hAx,'x') % and synchronize x-axes
grid(hAx(1),'on') % just a touch
This produced
for a particular set of random data; your result will vary...

その他の回答 (1 件)

dpb
dpb 2016 年 6 月 20 日
Set 'YAxisLocation' to 'Origin' -- require HG2 R2014a(?) or later...
  2 件のコメント
Nada Zamri
Nada Zamri 2016 年 6 月 20 日
Anyway,I'm using R2013 version. Is there any other ways to shift the x axis?
dpb
dpb 2016 年 6 月 20 日
Not until the introduction of HG2 was the 'origin' value introduced; prior is only 'top','bottom'. You can draw an axes line wherever you want it w/ line of course, and label it with text but afaik that'd be the only way...
Oh, wait a minute -- use a second axis whose size/position is at the proper set of values to put its axis where you need it. I've gotta' run run now, but that'll work. Remember have to make the second one transparent so first will show through. There's example of using two axes in the doc's for graphics although they overlay them exactly, you've got to compute the ratio of the height wanted relative to the overall from the y-axis range.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by