Modify x-axis position

我与影子孤独终老i 提交于 2019-12-19 04:02:16

问题


When you use plot function in Matlab, you are presented with y and x axis on the left and at the bottom respectively. But I'd like the x-axis to strike through in the middle of the figure with the scales and numbers like so:

I beg a pardon for my amateur paint skills. But basically I want the x-axis to move to the top for example and I want to have numbers there and those small bumps like indicated on the red line but all the way through to the right, but I do not want a number under each "bump" just the whole numbers. I tried to google the answer but couldn't find anything.

Clearly, I do not want two x-axis, so ideally the one at the bottom would be gone.


回答1:


Generally, apart from some ugly hackish solutions (can be found on FEX), the functionality you want is not applicable in Matlab. And it doesn't seem so, that it changed with the recent update. So if you really need that, save your figure as vector graphic and edit it with Inkscape or Illustrator, or just draw it with Latex/pgfplots/Matlab2Tikz from the beginning.

However the 2014b update of the graphics engine introduced the following solution using some undocumented features. Maybe this is already sufficient, I dare to say it's the closest you can get without coding handstands.

Matlab R2014b or higher is required!

%// example
t = linspace(0,4*pi);
plot(t,sin(t))
ylim([-1.5,1.5]); xlim([0,4*pi]);

%// get handle
h = gca;

%// modify y-axis
h.YBaseline.BaseValue = 0.5;
h.YBaseline.Visible = 'on'; 
h.XRuler.Axle.Visible = 'off';

%// modify x-axis
h.XBaseline.BaseValue = 2;
h.XBaseline.Visible = 'on';
h.YRuler.Axle.Visible = 'off';




回答2:


that's a bit trickier than expected because matlab doesn't provide this feature. You could just do it manually by drawing lines and switching off the original axes.

I found some example on the Matlab Central

t=linspace(0,10,100);plot(t,sin(2*pi*t));
axis([-10 10 -1 1]);

y=get(gca,'ytick');
x=get(gca,'xtick');
hold on
Lx=line([x(1) x(11)],[0 0]);
Ly=line([0 0],[y(1) y(11)]);
set(Lx,'color',[1 0 0]);
set(Ly,'color',[1 0 0]);
for i=1:length(x)
    plot(x(i),0,'k*',0,y(i),'k*');
    text(x(i),-.05,num2str(x(i)));
    text(-1,y(i),num2str(y(i)));
end
set(gca,'yticklabel',[],'xticklabel',[],'ytick',[],'xtick',[]);
set(gca,'visible','off')

but you should also check on fileexchange as there is probably some better solution already around.



来源:https://stackoverflow.com/questions/33708402/modify-x-axis-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!