How can I position my textbox exact position in my plot?

139 次查看(过去 30 天)
Hi,
I wonder if there is a proper technique to position my 'textbox' to the exact position I wanted.
For example,
I'd like to place textbox position like below, and those boxes should be located from year 2010 ~ 2018, for example.
(My intuitive idea says, it will be possible if I measure x position of white lines next to the boxes... but I am not sure how to do so too)

采纳的回答

Jim Riggs
Jim Riggs 2019-9-4
编辑:Jim Riggs 2019-9-6
Yes, this is possible to do, but it is complicated. When you place graphics in a figure window, they are located based on the figure window axes. But you want to position this relative to a set of axes in the figure window, so you have to compute the position in the axes relative to the figure window axes.
The axis system attached to the figure window is in "normalized" units. [0, 0] is the lower left corner of the figure window, and [1, 1] is the upper right corner of the figure window. Note that this does not include the title banner or the tool bar on top of the figure window.
In order to manipulate the figure, you need to have it's handle. The easiest way is to get the handle when you create the new figure window;
h = figure;
This command creates a new, empty figure window, and returns it's handle, h.
Or, you can get the handle of the curent figure using:
figure;
h = gcf;
The command "gcf" returns the handle to the current figure.
You will also ned the handle to your plot axes. You get this in a similar way:
plot(..);
ax = gca;
After creating your plot, gca returns the handle to the current axes.
Now you can find out the position of the axes inside the figure window:
AxesPos = get(ax, 'position');
"AxesPos" contains 4 values: AxesPos(1:4) = left position, bottom position, Axes width, Axes height. (see the first figure, above). I'll name these [Ax_xpos, Ax_ypos, Ax_width, Ax_height]
By default, these are in normalized units, so their values are all between zero and 1.
You get the axes scales using:
xscale = get(ax, 'xlim' );
yscale = get(ax, 'ylim');
xscale contains [xmin, xmax], and yscale contains [ymin, ymax]. These are in axes units, i.e. the units that pertain to your data which are displayed in the axis labels. Now compute the position of the axes scales in terms of normalized figure window units:
X_norm, Y_norm are the normalized coordinates for point X, Y in axes units. The inverse calculation converts coordinates from normalized coordinates to axes units;
X_scale, Y_scale are the plot axes coordinates for point X, Y in normalixed units.
So, this is how you get from figure window axes to plot axes and vice-versa.
Now, you can use this to position your text box.

更多回答(0 个)

类别

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

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by