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

134 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-5-15
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 2020-5-15
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.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-15
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 个评论
G Dalzell
G Dalzell 2021-7-29
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 2021-7-29
编辑:Adam Danz 2021-7-29
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')

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2020-5-18
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 个评论
Adam Danz
Adam Danz 2020-5-24
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 2023-8-23
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Formatting and Annotation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by