help with adding the text location

171 ビュー (過去 30 日間)
Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019 年 6 月 5 日
編集済み: Sven Merk 2023 年 7 月 14 日
is possible to add a text to a figure in the northeast or northwest of th axes?
when reading the documentation i found this , text(x,y,txt)
but for some reason i don't always know what xlim and ylim are so i cannot predicte what to put in x and y
so any ideas ?

採用された回答

Star Strider
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
Experiment to get the result you want.

その他の回答 (3 件)

Walter Roberson
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()

Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019 年 6 月 5 日
well let's me explain more
I'm working on kmeans clustering method and i'm using the function proposed by mathworks
to use it the user should precise a dataset, number of clusters,method of calculation of initial centroids and distance
and extra parameters
so what i did is that i used the function gscatter so that it gave at the end an automatic legend so that the user knows each colored set refers to which cluster (please see the attached screen shot)
now i want to add a text which shows :
number of clusters,method of calculation of initial centroids and distance
and for the text i tied to use this
Line1 = sprintf('Centr-Init Method : %s', Method);
Line2 = sprintf('Metric : %s',char(Metric));
Line3 = sprintf('Clusters : %u',Number);
% Text
Text = ...
text((x,y, {Line1,Line2,Line3},...
'HorizontalAlignment', 'Left',...
'VerticalAlignment', 'Top',...
'Fontsize', 9,...
'FontAngle', 'Normal',...
'Color', 'b',...
'EdgeColor', 'b',...
'BackGroundColor', 'w');
and i dont know how to precise the values of x and y
1.png

Sven Merk
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");

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by