![help with adding the text location - 2019 06 05.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223183/help%20with%20adding%20the%20text%20location%20-%202019%2006%2005.png)
help with adding the text location
조회 수: 126 (최근 30일)
이전 댓글 표시
댓글 수: 0
채택된 답변
Star Strider
2019년 6월 5일
You should be able to determine what xlim and ylim are after you do the plot.
Try this:
x = -10:10;
y = (rand(1,21)-0.5)*20;
figure
plot(x, y, 'pg')
NE = [max(xlim) max(ylim)]-[diff(xlim) diff(ylim)]*0.05;
SW = [min(xlim) min(ylim)]+[diff(xlim) diff(ylim)]*0.05;
NW = [min(xlim) max(ylim)]+[diff(xlim) -diff(ylim)]*0.05;
SE = [max(xlim) min(ylim)]+[-diff(xlim) diff(ylim)]*0.05;
text(NE(1), NE(2), 'NORTHEAST!', 'VerticalAlignment','top', 'HorizontalAlignment','right')
text(SW(1), SW(2), 'SOUTHWEST!', 'VerticalAlignment','bottom', 'HorizontalAlignment','left')
text(NW(1), NW(2), 'NORTHWEST!', 'VerticalAlignment','top', 'HorizontalAlignment','left')
text(SE(1), SE(2), 'SOUTHEAST!', 'VerticalAlignment','bottom', 'HorizontalAlignment','right'
producing:
![help with adding the text location - 2019 06 05.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/223183/help%20with%20adding%20the%20text%20location%20-%202019%2006%2005.png)
Experiment to get the result you want.
댓글 수: 0
추가 답변 (3개)
Walter Roberson
2019년 6월 5일
You have a small number of choices:
- use legend()
- just before doing the text(), fetch the axis xlim and ylim and use those. If you need the axis limits to be able to change then if it is due to the user adding additional data, then you can use a listener to detect that; if it is due to the user resizing then you can add a ResizeFcn callback
- use a second axes that shares position with the first one, and where you position the text in coordinates relative to that second axes
- lock the relative position of the axes relative to the figure, and use annotation()
댓글 수: 0
Sven Merk
2023년 7월 14일
편집: Sven Merk
2023년 7월 14일
What about this? Setting the unit to normalized allows you to position your text regardless of the data values.
Method = "plus";
Metric = "sqeuclidean";
Clusters = 2;
some_data = rand(100,2)* 20; % multiply with 20 just to show that the data values do not matter
class = kmeans(some_data, Clusters, Distance=Metric, Start=Method);
scatter(some_data(:,1), some_data(:,2), 10, class);
lbl = "\begin{tabular}{l l}" + ...
"Center-Init Method & " + Method + "\\" + ...
"Metric & " + Metric + "\\" + ...
"Clusters & " + string(Clusters) + "\\" + ...
"\end{tabular}";
text(0.05, 0.95, lbl, Units="normalized", Interpreter="latex", VerticalAlignment="top", HorizontalAlignment="left");
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!