Custom x-axis values in a matlab plot

∥☆過路亽.° 提交于 2019-12-01 15:23:27

问题


Currently when I plot a 9 by 6 array, the x-axis of the figure is just 1, 2, 3 up to 9. The Y-axis shows the correct values.

Instead of 1 to 9 I would like the x-axis values to be custom. They should be

100 200 400 1000 2000 5000 10000 20000 50000

instead. I tried

set(gca,'XTick', [100 200 400 1000 2000 5000 10000 20000 50000])

But that's not the correct way to do it. Is there a Matlab option to have these custom values for the x-axis? Why is Matlab just using 1 to 9 anyway?


回答1:


You should be using xTickLabel instead of XTick.

MATLAB plots every column as a seperate curve. So, that means you have 6 curves and 9 data points for each curve. x-axis data is 1-9 because you did not provide any data for MATLAB to plot with.

Furthermore, you probably want the wrong thing. Doing this will give you equal spacing. It will just replace 1-9 with your array. Since your x-axis data is not equally spaced, it will be weird.

You may want to do it like this:

xdat = [100 200 400 1000 2000 5000 10000 20000 50000];
ydat = rand(9,6); % Your y-axis data
plot(xdat, ydat)



回答2:


If you want to keep distances between x-values (e.g. 1:9) and only change the labels (not the distances between x-values), try this:

y = rand(9,6);
labels = [100 200 400 1000 2000 5000 10000 20000 50000];
plot(y);
set(gca, 'XTick', 1:length(labels)); % Change x-axis ticks
set(gca, 'XTickLabel', labels); % Change x-axis ticks labels.



回答3:


Try using

x = [100 200 400 1000 2000 5000 10000 20000 50000];
y = rand(9,6); % Your y-axis data
plot(x, y);
set(gca,'XTick',x); % Change x-axis ticks
set(gca,'XTickLabel',x); % Change x-axis ticks labels to desired values.

Please note that due to the very different values/magnitudes to use un x-axis you can get some x-labels very close (and unreadable)



来源:https://stackoverflow.com/questions/13568190/custom-x-axis-values-in-a-matlab-plot

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