Question about the axis of a figure

1 次查看(过去 30 天)
On the x-axis I have 'number of subjects'. But the axis goes from 0 - 0.5 - 1 - 1.5 - ... Since 1.5 subjects doesn't exist, I want to get rid of these number with the comma. How do I do this?

采纳的回答

Star Strider
Star Strider 2015-1-4
编辑:Star Strider 2015-1-4
You need to set the specific 'XTick' locations.
Example:
figure(1)
subplot(2,1,1)
scatter(rand(10,1)*3, rand(10,1)*2, 'pb')
grid
subplot(2,1,2)
scatter(rand(10,1)*3, rand(10,1)*2, 'pb')
grid
xt = get(gca, 'XTick');
set(gca, 'XTick', floor(min(xt)):ceil(max(xt)))
To illustrate:

更多回答(1 个)

Geoff Hayes
Geoff Hayes 2015-1-4
编辑:Geoff Hayes 2015-1-4
Sam - you will need to maipulate then ticks and tick labels for the x-axis. Try running the following statements
xticks = get(gca,'XTick');
xtickLabels = get(gca,'XTickLabel');
Both of these statements should return vectors/arrays of the positions and labels of the x-axis ticks respectively. Since you want to remove every other label (due to the 0.5), then just reset these two properties if the x-axis using every other element only as
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end));
Note that 1:2:end means start at the first element (at index 1) and jump by two until the end is reached. gca is the handle to the current axis (g is for get). The above may require some changes given your version of MATLAB, but try it out anyway.
EDIT
If xtickLabels is a character array, then the above set should be replaced with
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end,:));
  2 个评论
Sam
Sam 2015-1-4
I tried it, but it doesn't work...
Geoff Hayes
Geoff Hayes 2015-1-4
Sam - could you clarify what you mean by it doesn't work? If xtickLabels is a character array, then you may need to replace the set with
set(gca,'XTick',xticks(1:2:end), 'XTickLabel',xtickLabels(1:2:end,:));

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by