简体   繁体   中英

How to put x-grid on a plot in matlab

I have a vector x = 1:20 and another vector y.

When I plot x versus y, the x-axis has labels in increments of 2. That is, x-axis shows, 0,2,4,6,8,...,20. But I want my x-axis to show 0,1,2,3,4,...,20.

How do I do this?

Use the XTick property. Assuming your figure is open, just do:

set(gca, 'XTick', 0:20);

Here's a quick example:

x = 1:20;
y = rand(1,20);
plot(x,y)

We get this plot:

在此处输入图片说明

Changing the XTick property on the graph gives us:

在此处输入图片说明

I recommend looking at this: http://www.mathworks.com/help/matlab/creating_plots/change-tick-marks-and-tick-labels-of-graph-1.html

If you have MATLAB version 2014b or later:

ax = gca;
ax.xTick = 0:20;

A more general solution that works with any version of MATLAB:

ax = gca;
set(ax, 'XTick', 0:20);

要修改绘图上的x和y网格,可以使用Matlab文档中此处所示的XTickYTick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM