2

I have reviewed the previous questions as described in Minor grid with solid lines & grey-color but It didn't help me solve my problem. My issue is entailed with xticks. I want my grid lines to appear at specific points on xaxis and some other grid lines to appear at different points with some different colors. Something like this:

plot(x,y,'--g')
set(gca,'Xcolor',[0 0 0],'Xtick',[12e3,14e3,18e3,23e3,30e3,37e3,57e3],
set(gca,'Xcolor',[0.5 0.9 0.5],'Xtick',[10e3 16 28e3]);

The problem is that the later xtick labels overwrites the previous ones. I'd like to retain the xlabels of the previous ones.

Community
  • 1
  • 1
mirage
  • 145
  • 2
  • 8
  • 19

1 Answers1

2

Create a 2nd axis.

x=-3.14:.1:3.14;
y=sin(x);

h=plot(x,y);
ax1=findobj(gcf,'Type','axes'); %save first axis handle

%set first stype
set(gca,'Xcolor',[0 0 0],'Xtick',[-3,-2,-1,1,2,3],'gridlinestyle','-','xgrid','on')

%create new axis
ax2=axes('position',get(gca,'position'),'Visible', 'on'); 
set(ax2,'YTick',[],'Xcolor','blue','Xtick',[-2.5 0 2.5],'xgrid','on','color','none'); %color none to make the axis transparent
set(ax2,'xlim',get(ax1,'xlim')) %resize 2nd axis to match 1st

Produces:

Example

Chris
  • 1,532
  • 10
  • 19
  • ??? Error using ==> set Conversion to double from cell is not possible(last set statement). I have values at xaxis in range of 10e3 to 80e3, so I tried using cell2mat but it also doesnt seem working – mirage Apr 20 '12 at 11:59
  • If you copy-paste my example, does the example work? what does get(ax1,'xlim') return? – Chris Apr 20 '12 at 15:54
  • your example works fine..but when I use it in my code it prompts an error due to xaxis that are in range of 10e3 to 80e3 – mirage Apr 23 '12 at 12:21
  • do you have a minimum working example? what does class(get(ax1,'xlim')) return? – Chris Apr 23 '12 at 15:18
  • it prompts an error ==>conversion to double from cell is not possible at set(get(ax1,'xlim')) – mirage Apr 24 '12 at 07:27
  • As I have three plot commands with further three for interpolation, but when i use ur code straight after the first plot command it gives me this error. ??? Error using ==> set Bad property value found. Object Name : axes Property Name : 'XLim' Value must be a 2 element vector. – mirage Apr 24 '12 at 08:27
  • post code, impossible to tell otherwise. Edit your original post with what you have so far. – Chris Apr 24 '12 at 15:45