Matlab Grid lines with different color on one axis

大城市里の小女人 提交于 2019-12-02 05:48:30

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/10227412/matlab-grid-lines-with-different-color-on-one-axis

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