Placing Text in a Plot

16 次查看(过去 30 天)
Sameer
Sameer 2014-9-10
编辑: Stephen23 2014-9-11
Hello
I am trying to place some text at upper left corner of my Plot so I used the following code but as there is difference between the data so the text placed is sometimes overlaping, as its using the data to determine the location. So can anyone please sugesst the different way to acheive the same.
Here is the code I used:
P_p=plot(Totalpressure_given,Totalpressure_given,'*g');
hold on
P_p2=plot(Totalpressure_given,Totalpressure_obtained,'*r');
X_p=get(P_p,'XData');
Y_p=get(P_p,'YData');
X_p2=get(P_p2,'XData');
Y_p2=get(P_p2,'YData');
imax=find(max(Y_p)==Y_p);
imin=find(min(Y_p)==Y_p);
imax2=find(max(Y_p2)==Y_p2);
imin2=find(min(Y_p2)==Y_p2);
text(X_p(imin(1,1)),Y_p(imax),['Average Difference: ',num2str(Avg_Difference_P)]);
text(X_p2(imin2(1,1)),Y_p2(imax2),['Standard Deviation: ',num2str(SD_P)]);
There is also a way using cell but that I am not able to implement properly.
How I can properly place the text?
Please Guide
Regards
  2 个评论
dpb
dpb 2014-9-10
编辑:dpb 2014-9-10
More specifically, where are you trying to locate the text--just out of the way of the data or relative to the data points?
BTW, there's no need for all the get calls, you've already got the data that you plotted in the two arrays. I'd recommend to use shorter variable names for the convenience factor of not having so much steenkin' typing and long lines in the subsequent usage. You can simplify the plotting call somewhat, too...
hL=plot(Tpr_g,Tpr_g,'*g', Tpr_g,Tpr_o,'*r');
Sameer
Sameer 2014-9-11
Hi...Thanks fo letting me know and I will keep this in mind....I am trying to place the text at the upper left portion of the plot.As there are many plots and each vary in magnitude with a huge amount. so I am looking for a way so that it places the text at the desired position irrespective of the magnitude of the data...I used the magnitude because I was not able to find the way involving no use of data...but my approach is also not working properlyas it is data dependent and as the text is in different line so sometime its overlapping and sometimes the gap between two lines is huge...Can you suggest any approach?
Regards

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2014-9-10
Guessing here because I can’t run your code.
Experiment with the Text Alignment and 'Extent' options in Text Properties.
Those might solve your problem.

更多回答(1 个)

Stephen23
Stephen23 2014-9-11
编辑:Stephen23 2014-9-11
MATLAB lets you control all of the text properties , particularly you will want to look at Extent , Position , Units , HorizontalAlignment , VerticalAlignment ,...
For example:
  • If you want your text to stay in a particular location, regardless of the data, then look at the units and Position properties.
  • If you want to adjust how the text is aligned relative to the data, look at Extent.
Keep in mind that this is MATLAB: there is no "intelligent" tool that knows exactly where you want your text to be, you have to calculate that text location yourself. And keep an eye on the units!

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by