Position or Coordinates of Labels
조회 수: 170 (최근 30일)
이전 댓글 표시
Hi,
I want to know the Position of the xlabel (ylabel) of my plot, to insert an arrow at this position. However, when I call that property, it is some strange position format I do not understand, and I cannot use that to insert my arrow:
plot(1) % example plot
xlabel('')
axes = gca;
position = axes.XLabel.Position % strange format with values bigger than one
annotation(gcf,'arrow', [postion(1) position(1)+ arrowlength], [position(2) position(2)+arrowwidth]) % error
How must the last line of code look to work?
댓글 수: 0
채택된 답변
Asvin Kumar
2020년 6월 10일
Let me establish a couple of details first before I go ahead and explain how to solve your problem.
1. axes.XLabel.Position
axes.XLabel, axes.YLabel, axes.ZLabel are text objects as mentioned here. They follow text properties as detailed here. The axes.XLabel.Position property has three values as given in this section on the same page. They are at position [x, y, z] where the units for these values is ‘Data’, by default, as mentioned right below. So, if your xlabel is located below the x tick with value 1, then axes.XLabel.Position(1) would be 1 and so on.
Final detail to note here is that, since the values are w.r.t Data, these values are all part of the axes and not the figure. This will come in handy when we try to solve your problem.
2. annotation
annotation takes two input arguments x and y along with others. Their units are defined by the default mentioned in their respective Name-Value pairs as given here. The units are ‘normalized’ for Arrows and for TextArrows. Since annotations are added to a figure, all the locations x and y are normalized values w.r.t the figure.
Now, for your requirement of adding an arrow annotation to a label. Here’s a good starting example which explains how to add annotations: Creating Text Arrow Annotations. For your problem of drawing an arrow annotation, you will need the location of the label w.r.t to the figure in normalized units. Since the current label position units are ‘Data’, you can change them by doing the following:
ax = gca;
ax.XLabel.Units = ‘normalized’;
The next detail is that these values that we get from ax.XLabel.Position are w.r.t the axes. So, that needs to be transformed to give the position w.r.t the figure. That can be done with:
label_xcoord_wrt_fig = ax.Position(1)+ax.Position(3)*ax.XLabel.Position(1);
label_ycoord_wrt_fig = ax.Position(2)+ax.Position(4)*ax.XLabel.Position(2);
Now that we have the location of the label w.r.t the figure, we can use the annotation function as used in the example. Here, I have created an arrow annotation of arbitrary length and orientation. You can change this to fit your requirements.
annotation('Arrow',[label_xcoord_wrt_fig-0.1 label_xcoord_wrt_fig], [label_ycoord_wrt_fig+0.1 label_ycoord_wrt_fig])
That should give you the desired outcome. Having said this, I want to draw attention to our two main steps. First, we match the units for the position of the label with the units for the annotation function. Once we are dealing in the same units, we transform the position of the label w.r.t axes to get position w.r.t figure. As an exercise, you can try doing this process with the common unit being ‘pixel’. That should be equally simple.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!