How to change the Ytick in the secondary y axis?

조회 수: 21 (최근 30일)
Fred
Fred 2015년 4월 17일
답변: Star Strider 2015년 4월 17일
I have a figure with the following code. How can I add 20 and 80 to the secondary y label? Now it looks like this:
axes1 = axes('Parent',figure(15),'YTick',[8.5 9.5 10.5],'Color',[0 0 0]);
[ax,p1,p2] = plotyy(annual(:,1),annual(:,7),lc2(:,1),lc2(:,2:3));
hold on
set(p1,'LineStyle',':','Marker','v','DisplayName','DO')
set(p2(1),'LineStyle','--','Marker','*','Color',[1 0 0],'DisplayName','Developed')
set(p2(2),'LineStyle','--','Marker','*','Color',[0 0.5 0],'DisplayName','Forest')
[p,~,mu] = polyfit(annual(8:20,1),annual(8:20,7),3)
f = polyval(p,annual(8:20,1),[],mu);
hold on
plot(annual(8:20,1),f,'k','DisplayName','DO Trendline')
xlabel('Time')
ylabel(ax(1),'DO (mg/l)') % left y-axis
ylabel(ax(2),'Area (%)') % right y-axis
ylim(ax(1), [8.5 10.5]);
ylim(ax(2), [0 100]);
legend(axes1,'show');

채택된 답변

Star Strider
Star Strider 2015년 4월 17일
I can’t reproduce your plot because I don’t have your data, but this code snippet offers a workable approach:
x = [1992:2012];
y1 = randi([10 35], 1, length(x));
y2 = randi([65 80], 1, length(x));
figure(1)
[ax,p1,p2] = plotyy(x,y1, x,y2);
ylim(ax(2), [0 100]);
set(ax(2), 'YTick',[0, 20, 50, 80, 100])
The last line sets the ticks where you want them and labels them.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by