How to include arrows as pointers of a particular point in plot?

134 vues (au cours des 30 derniers jours)
Abhishek Singh
Abhishek Singh le 15 Mai 2020
Commenté : Tongyao le 23 Août 2023
I have a graph for 3 variables as shown here. I know the saturation point co-ordinates and want arrows of the same color to be pointing them exactly like I have hand drawn in the figure. I tried annotations(arrow) function but it did not work even though I used correct co-ordinates it makes the arrow out of the boundary or maybe I was using it in a wrong way. Please help me if I can draw the arrows at a particular point I need. I would also have to declare 3 set of (x,y) co-odrinates since my main function needs to work simultanously for the three variables.
Code I tried:
plot(T,ih,T,iv,T,sh)
legend('Infected Human','Infected Mosquito','Susceptible Human','Location','east')
xlabel('Time (days)')
ylabel('Population')
x=[39636/40000 39636/40000];
y=[1 180/200];
annotation('arrow',x,y)
Thank you very much in advance.
  4 commentaires
Rik
Rik le 15 Mai 2020
It looks like your coordinates are around (1,1), which doesn't match the graph you've drawn. They are also relatively close, so the arrow will be small. Did you zoom in?
Abhishek Singh
Abhishek Singh le 15 Mai 2020
It is drawing but outside the boundaries. Even if my co-ordinates are at the end it should not be drawing outside is what I thought. See the arrow incircled below.

Connectez-vous pour commenter.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 15 Mai 2020
annotation() takes position in figure() coordinates. The position has no relation with the x-values and y-values of your axes. If you want to specify the position of your arrows in the axes() coordinates, then you need to do a transformation. The following code shows an example. The arrow_x and arrow_y specify the position of arrows according to the x-axis and y-axis of the axes() and then use interp1 to do the transformation
fig = figure('Units', 'normalized');
ax = axes();
plot(1:10, 0.1:0.1:1);
pos = ax.Position;
pos(3:4) = pos(3:4) + pos(1:2);
arrow_x = [5 5];
arrow_y = [0.7 0.5];
arrow_x_fig = interp1(ax.XLim, pos([1 3]), arrow_x);
arrow_y_fig = interp1(ax.YLim, pos([2 4]), arrow_y);
annotation('arrow', arrow_x_fig, arrow_y_fig);
  17 commentaires
G Dalzell
G Dalzell le 29 Juil 2021
This is a good clear answer but it would be good if mathworks could expand the annotation functionality so that a specific data point that has been plotted can be specified as the arrow end point.
Adam Danz
Adam Danz le 29 Juil 2021
Modifié(e) : Adam Danz le 29 Juil 2021
Annotation support for data units has been requested numerous times over the years but has not been addressed by MathWorks.
Arrows can be added with the text() function which operates in data units. One option is using TeX markup,
hold on; xlim([0,1])
plot(.5, .5, 'o')
text(.5, .5, '\downarrow', 'FontSize', 24, 'VerticalAlignment', 'bottom', 'HorizontalAlignment','Center')
another option is using unicode characters,
plot([.2 .8], [.5 .5], 's')
text(.2, .5, char(8595), 'FontSize', 24, 'VerticalAlignment', 'bottom', 'HorizontalAlignment','Center')
text(.8, .5, char(8675), 'FontSize', 24, 'VerticalAlignment', 'bottom', 'HorizontalAlignment','Center')

Connectez-vous pour commenter.

Plus de réponses (1)

Rik
Rik le 18 Mai 2020
A completely different strategy would be to use the LaTeX interpreter to insert an arrow as a text object:
x = 0:pi/20:2*pi;
y = sin(x);
plot(x,y)
text([pi pi*2],[0 0],'\downarrow',...
'FontSize',40,...
'VerticalAlignment','bottom','HorizontalAlignment','center')
  20 commentaires
Adam Danz
Adam Danz le 24 Mai 2020
jet(n) produces an nx3 matrix of R,G,B color values. The range of colors is always the same. The only input, n, controls the interval.
I recommended running "doc jet" which explains this. As I mentioned earlier, you can create any color map with an nx3 matrix of values between 0 and 1.
Red is [1 0 0] because it's 100% red, 0% green, 0% blue.
Tongyao
Tongyao le 23 Août 2023
Just wanted to add a note here. I attempted to add tilted arrow such as `\nwarrow`. It is part of the Latex default arrow but MATLAB does not support it without additional packages.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Formatting and Annotation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by