Matlab x-axis scaling – evenly spaced custom axis

怎甘沉沦 提交于 2019-12-08 07:42:25

问题


Using the variables below, I need to plot data (x_values, y_values) on an x axis with ticks at ‘x_labels’ values. Each ‘x_labels’ tick must be evenly spaced on the x-axis (e.g. 1 cm).

I have been playing with 'XTick' and 'XTickLabel' variables and although I searched online all other examples are related to when the ‘x_labels’ and ‘x_values’ are identical. The challenge here is that there is different spacing between ‘x_labels’ and ‘x_values’. Please help!

x_labels = [4 8 16 32 64 128];
x_values = [5 10 35 50 60 70 90 120];
y_values = rand(1,length(x_values))

Thanks!


回答1:


Using the set command you can specify the xTick and xTickLabel (near the bottom of the link) properties of the axis separately, xTick defines the tick marks locations and xTickLabel defines the labels.
So after plotting the data:

plot(x_values,y_values);

we can freely set the locations, in this example they are spaced evenly between zero and the max of x_values. The set command is then be used to set the axis properties, (gca is the current axis handle)

x_label_locations = linspace(0,max(x_values),numel(x_labels));

set(gca,'xTick',x_label_locations,'xTickLabel',x_labels)


来源:https://stackoverflow.com/questions/23423062/matlab-x-axis-scaling-evenly-spaced-custom-axis

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