1

I am trying to plot a function while showing the four quadrants. I understand there were previous workarounds with listeners and what nots but the other "solutions" were given over 3 years ago. I liked the clean undocumented solution:

%// vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');

%// horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');

Unfortunately, it does not work any more in Matlab R2014b.

Is there an official way to achieve this kind of plot in MATLAB R2014b?

1 Answer 1

1

Yes, the undocumented option for Matlab 2014b is:

h=get(gca);
h.YBaseline.Visible = 'on';
h.XBaseline.Visible = 'on'; 
h.YBaseline.Color =  [0.7 0.7 0.7];
h.XBaseline.Color =  [0.7 0.7 0.7];
h.XBaseline.LineStyle = ':'
h.YBaseline.LineStyle = ':'

a different solution you can use is this (no need for the h=get(gca) line):

matlab.graphics.axis.decorator.Baseline('BaseValue',0, 'Parent',gca, 'Axis',0, 'Visible','on','Color',[0.7 0.7 0.7],'LineStyle',':')
matlab.graphics.axis.decorator.Baseline('BaseValue',0, 'Parent',gca, 'Axis',1, 'Visible','on','Color',[0.7 0.7 0.7],'LineStyle',':')

Alternatively, you can use this documented trick after your code, as it automatically scales according to the plots you have:

h=get(gca); hold on
line(h.XLim,[0 0],'LineStyle',:,'Color',[0.7 0.7 0.7]); 
line([0 0],h.YLim,'LineStyle',:,'Color',[0.7 0.7 0.7]); hold off

As usual, credit to Yair Altman that uncovers these undocumented features in his blog.

1
  • Thanks, the second option was just what I was looking for. Note that the third option won't scale dynamically if you change the axis later or let the user drag the plot (with the hand tool)
    – codekitty
    Feb 7, 2015 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.