How to pin annotations (or text) to a graph below the x-axis?

58 Ansichten (letzte 30 Tage)
I would like to add vertical lines (or arrows) and text-labels below the x-axis at particular x-axis-values in my plots. Here is an example-plot:
I would like to draw vertical lines or arrows downwards from the x-axis exactly where the white lines are in the graph (basically 'extending' the white lines below the x-axis). At the bottom end of those lines/arrows I would like to add labels ('LHS', 'RTO', 'RHS', 'RTO'; as shown in graph). How can I do that?
My current solution is to use text instead of annotation (as shown in graph) because it allows me to add the labels below the x-axis (see part of my script below). However, this way I cannot add lines/arrows with the labels and when resizing the image the labels are moving in y-direction (they seem to have a relative position rather than being pinned to an absolute value in the graph?). Thus, I would like to improve my current solution. I am looking for a programmatic solution, not interactive.
One problem with using annotation seems to be that one is required to give the coordinates in normalised units, which, in turn, does not allow me to add objects like lines/arrows/textboxes below the x-axis. I have tried to convert the units, and to use other units like pixels or centimeters but I could not get it to work (but maybe I am doing something wrong). Another problem is to pin the annotation to the axes data. I have been reading through Yair Altman's undocumented matlab site on this topic as well as checked other answers here and in other forums but I cannot quite work out how to adjust these suggestions for my particular problem.
This is a snapshot of my code that creates the figure:
figure
imagesc(T, F, P(:,:,3))
set(gca,'ylim', [0 60],'ydir','normal')
hold on
plot([0 0],get(gca,'ylim'),'w:') % draw vertical line at RHS
text(0,-3,'RHS')
plot([x_RTO x_RTO],get(gca,'ylim'),'w:') % draw line at RTO
text(x_RTO,-3,'RTO')
plot([x_LHS x_LHS],get(gca,'ylim'),'w:') % draw line at LHS
text(x_LHS,-3,'LHS')
plot([x_LTO x_LTO],get(gca,'ylim'),'w:') % draw line at LTO
text(x_LTO,-3,'LTO')
xlabel('time [s]')
ylabel('frequency [Hz]')
colorbar
In this example, T is a 41x1 double (listing values from -0.8 to 0.2), F is a 1025x1 double (listing values from 0 to 1000), and P(:,:,3) is a 1025x41 double; x_RTO = -0.4675; x_LHS = -0.5919; x_LTO = 0.1353.
Many thanks for your help!

Akzeptierte Antwort

Star Strider
Star Strider am 6 Okt. 2015
You can use the ‘\uparrow’ text character to plot the arrows. It may not be quite the result you want, but it’s likely the only option you have.
For example:
d = rand(20);
figure(1)
imagesc(d)
text(5, 22, sprintf('\\uparrow\nLTO'), 'HorizontalAlignment','center', 'FontWeight','bold')

Weitere Antworten (1)

Yair Altman
Yair Altman am 10 Jan. 2018
Bearbeitet: Yair Altman am 10 Jan. 2018
The undocumented ability to pin annotations to a specific axes location is explained here: https://undocumentedmatlab.com/blog/pinning-annotations-to-graphs
Note: This is an undocumented feature, but it was directly referenced by an official Mathworks solution, so it can't be too bad :-)
Keep in mind that my post was written back in 2012, many Matlab releases ago, and before HG2 was released (in R2014b), so if you're using a recent Matlab release this functionality may quite possibly fail.
Yair Altman

Community Treasure Hunt

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

Start Hunting!

Translated by