How to set grid space in plot ?

17 次查看(过去 30 天)
Rahim Rahim
Rahim Rahim 2022-7-23
回答: Voss 2022-7-23
I want to add a grid on my plot. Unofrtunetely, I found only Grid normal and Grid miror,
I want to add a grid that appear every 5 value on y-axis, This is my code:
function DisplayNumber(x,y1,y2,Name,Method)
plot(x,y1,'-s','MarkerSize',9,...
'MarkerEdgeColor','green',...
'MarkerFaceColor',[0 0.4470 0.7410])
hold on
legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...
'Location','northwest','NumColumns',2)
p=plot(x,y2,'-s','MarkerSize',9,...
'MarkerEdgeColor','yellow',...
'MarkerFaceColor',[0.6350 0.0780 0.1840])
hold off
xtickangle(0)
max(y1);
Max= max( max(y1),max(y2));
ylim([0 Max+10]);
Max=max(x);
xlim([1 Max+1]);
legend( strcat(Method,"-AHP"),strcat(Method,"-MOWOA-NIS"));
ylabel('Rank reversal ratio %');
xlabel('Number of networks');
xticks(0 : 20)
grid on
set(gcf,'Units','Inches');
pos = get(gcf,'Position');
set(gcf,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(gcf,Name,'-dpdf','-r0')
end
I tried yticks(0:5:Max+10) but it did not work ... !

回答(1 个)

Voss
Voss 2022-7-23
What's the problem with yticks(0:5:Max+10)?
DisplayNumber(0:9,100*rand(1,10),100*rand(1,10),'output.pdf','')
function DisplayNumber(x,y1,y2,Name,Method)
plot(x,y1,'-s','MarkerSize',9,...
'MarkerEdgeColor','green',...
'MarkerFaceColor',[0 0.4470 0.7410])
hold on
% legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},...
% 'Location','northwest','NumColumns',2)
p=plot(x,y2,'-s','MarkerSize',9,...
'MarkerEdgeColor','yellow',...
'MarkerFaceColor',[0.6350 0.0780 0.1840]);
hold off
xtickangle(0)
max(y1);
Max= max( max(y1),max(y2));
ylim([0 Max+10]);
yticks(0:5:Max+10)
Max=max(x);
xlim([1 Max+1]);
legend( strcat(Method,"-AHP"),strcat(Method,"-MOWOA-NIS"));
ylabel('Rank reversal ratio %');
xlabel('Number of networks');
xticks(0 : 20)
grid on
set(gcf,'Units','Inches');
pos = get(gcf,'Position');
set(gcf,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(gcf,Name,'-dpdf','-r0')
end

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by