How to 1:1 map axis entries in a simple plot to another vector?

3 次查看(过去 30 天)
Dear matlab forum,
Please consider the following case:
v=1:10;
plot(1:10,v(1,:))
I would like to 1:1 map the entries on the horizontal axis in the figure to another vector, e.g. w=71:80. That is, I want the vector entries of w displayed on the axis of the figure, but keeping the same plot.
I could of course do v2=[zeros(1,70) v]
v2=[zeros(1,70) v];
plot(71:80,v2(1,71:80))
But is there another way, without adding the zeros and creating a new vector?
Thank you very much in advance
P.S.: for some reason I cannot run the code - is this a general problem at the moment in the matlab forum?

回答(1 个)

Piyush Patil
Piyush Patil 2023-10-6
Hello,
As per my understanding, you would like to perform 1 on 1 mapping of the entries on the horizontal axis of the figure to another vector (say "w") such that entries of vector "w" should be displayed on figure but keeping the plot same. And you want to perform this task without adding the zeros and creating new vector.
There is a way to achieve the desired mapping without creating new vector by using xticks function in MATLAB. This function allows you to specify the positions of the tick marks on x-axis.
Here is an example showing how you can do it:
v = 1:10;
w = 71:80;
plot(1:10, v(1,:))
% Set the x-tick positions and labels
xticks(1:numel(w))
xticklabels(w)
% Adjust the x-axis limits
xlim([1, numel(w)])
% Keep the existing plot
hold on
% Plot a vertical line to indicate the mapping
line([1, 1], ylim, 'Color', 'green')
% Release the hold
hold off
I hope this resolves the issue you are facing.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by