How to display a few xticklabels from a set of selected xticks?

1 次查看(过去 30 天)
How to display a few xticklabels, like [4, 53] in the following example, from a set of selected xticks?
selected_ticks = [2 3 4 5 6 8 11 1 19 29 53 119];
sorted_selected_ticks = sort(selected_ticks);
plot(1:120,1:120)
xticks(sorted_selected_ticks)
Indeed, just by adding xticklabels([4 53]) after xticks(sorted_selected_ticks) I do not get what expected, i.e. tick lables in position 4 and position 53, and labelled as 4 and 53, respectively, as in the following figure (which is my desired output):

采纳的回答

Voss
Voss 2023-10-31
selected_ticks = [2 3 4 5 6 8 11 1 19 29 53 119];
selected_labels = [4 53];
sorted_selected_ticks = sort(selected_ticks);
sorted_selected_labels = sort(selected_labels);
[ism,idx] = ismember(sorted_selected_labels,sorted_selected_ticks);
assert(all(ism))
xlabels = strings(1,numel(sorted_selected_ticks));
xlabels(idx) = string(sorted_selected_labels);
plot(1:120,1:120)
xticks(sorted_selected_ticks)
xticklabels(xlabels)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by