{"id":76,"date":"2007-02-08T11:38:17","date_gmt":"2007-02-08T16:38:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=76"},"modified":"2017-08-27T19:22:44","modified_gmt":"2017-08-28T00:22:44","slug":"string-annotations-for-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/08\/string-annotations-for-plots\/","title":{"rendered":"String Annotations for Plots"},"content":{"rendered":"<div class=\"content\">\r\n\r\nDid you know that you can customize the direction of text in MATLAB plots? There are times when such annotations work better\r\nthan if they were purely horizontal or vertical.\r\n\r\n&nbsp;\r\n<h3>Contents<\/h3>\r\n<div>\r\n<ul>\r\n\t<li><a href=\"#1\">Sunspot Data<\/a><\/li>\r\n\t<li><a href=\"#4\">Annotate the Year with the Maximum Number of Sunspots<\/a><\/li>\r\n\t<li><a href=\"#8\">Use Handle Graphics to Alter the Annotation Orientation<\/a><\/li>\r\n\t<li><a href=\"#10\">Note Other Maxima<\/a><\/li>\r\n\t<li><a href=\"#12\">Label Extra Peaks<\/a><\/li>\r\n\t<li><a href=\"#13\">Write Around a Circle<\/a><\/li>\r\n\t<li><a href=\"#14\">References<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h3>Sunspot Data<a name=\"1\"><\/a><\/h3>\r\nLet's look at some sunspot data. First I'll load it into MATLAB.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">s = load(<span style=\"color: #a020f0;\">'sunspot.dat'<\/span>);<\/pre>\r\nSeparate the year from the actual number of sunspots.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">year = s(:,1);\r\nspots = s(:,2);<\/pre>\r\nShow the data in a bar plot.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">bar(year,spots)<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Annotate the Year with the Maximum Number of Sunspots<a name=\"4\"><\/a><\/h3>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[smax, indmax] = max(spots);<\/pre>\r\nThe historical record shows the year\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">yearmax = year(indmax)<\/pre>\r\n<pre style=\"font-style: oblique;\">yearmax =\r\n        1957\r\n<\/pre>\r\nwith the maximum number of sunspots, 190.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">hold <span style=\"color: #a020f0;\">on<\/span>\r\nplot(yearmax, smax, <span style=\"color: #a020f0;\">'m*'<\/span>)\r\nhold <span style=\"color: #a020f0;\">off<\/span>\r\nht = text(yearmax, smax,[<span style=\"color: #a020f0;\">'  '<\/span>,int2str(round(smax)), <span style=\"color: #a020f0;\">' sunspots  '<\/span>]);<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_02.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Use Handle Graphics to Alter the Annotation Orientation<a name=\"8\"><\/a><\/h3>\r\nLet's try labeling the maximum so the label is right-justified now.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">set(ht,<span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'right'<\/span>)<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_03.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nNow change the label so it is at an angle, something I might want if I were planning to label several more points on the graph.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">set(ht,<span style=\"color: #a020f0;\">'Rotation'<\/span>,45)<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_04.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Note Other Maxima<a name=\"10\"><\/a><\/h3>\r\nThe <a href=\"http:\/\/en.wikipedia.org\/wiki\/Solar_variation\">sunspot cycle<\/a> has a periodicity of about 11 years. Let's see which other years have a high number of sunspots.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">[maxssa, indssa] = sort(spots,<span style=\"color: #a020f0;\">'descend'<\/span>);\r\nyear(indssa(1:10))<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n        1957\r\n        1958\r\n        1959\r\n        1979\r\n        1980\r\n        1778\r\n        1947\r\n        1956\r\n        1981\r\n        1870\r\n<\/pre>\r\nWe see some years near the maximum year, 1957, then another cluster near 1979, and the next at 1778. Let me grab the first\r\nfive \"distinct\" clusters.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">idxdistinct = [1 4 6 7 8];\r\nyear(indssa(idxdistinct))<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n        1957\r\n        1979\r\n        1778\r\n        1947\r\n        1956\r\n<\/pre>\r\n<h3>Label Extra Peaks<a name=\"12\"><\/a><\/h3>\r\nLet's label these few other peaks in a similar manner to the first one.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">nearMaxYears = year(indssa(idxdistinct(2:end)))\r\nssaMax = spots(indssa(idxdistinct(2:end)))\r\nhold <span style=\"color: #a020f0;\">on<\/span>\r\nplot(nearMaxYears, ssaMax, <span style=\"color: #a020f0;\">'mo'<\/span>)\r\nhold <span style=\"color: #a020f0;\">off<\/span>\r\nhn = text(nearMaxYears, ssaMax,[<span style=\"color: #a020f0;\">'  high sunspot activity  '<\/span>],<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'Right'<\/span>,<span style=\"color: #a020f0;\">'Rotation'<\/span>,25);\r\nset(ht,<span style=\"color: #a020f0;\">'Rotation'<\/span>,0)<\/pre>\r\n<pre style=\"font-style: oblique;\">nearMaxYears =\r\n        1979\r\n        1778\r\n        1947\r\n        1956\r\nssaMax =\r\n  155.4000\r\n  154.4000\r\n  151.6000\r\n  141.7000\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_05.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>Write Around a Circle<a name=\"13\"><\/a><\/h3>\r\nJust for fun now, let's write a phrase around a circle of radius 1.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">clf, box <span style=\"color: #a020f0;\">on<\/span>\r\nphrase = <span style=\"color: #a020f0;\">'ring around the collar '<\/span>;\r\nnum = length(phrase)\r\nangles = 0:(360\/num):359;\r\nx = cos(angles*pi\/180);\r\ny = sin(angles*pi\/180);\r\n<span style=\"color: #0000ff;\">for<\/span> ind = 1:length(phrase)\r\n    text(x(ind),y(ind),phrase(ind),<span style=\"color: #a020f0;\">'Rotation'<\/span>,angles(ind),<span style=\"color: #0000ff;\">...<\/span>\r\n        <span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'center'<\/span>)\r\n<span style=\"color: #0000ff;\">end<\/span>\r\naxis <span style=\"color: #a020f0;\">equal<\/span>, axis([-1.1 1.1 -1.1 1.1])<\/pre>\r\n<pre style=\"font-style: oblique;\">num =\r\n    23\r\n<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/76\/stringArt_06.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n<h3>References<a name=\"14\"><\/a><\/h3>\r\nThere is a whole lot more you can do with <tt>text<\/tt> annotation in MATLAB. Here are some references for the language aspects of working with text.\r\n<div>\r\n<ul>\r\n\t<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/text.html\"><tt>text<\/tt><\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\nThough I don't want to turn this blog into one chiefly about graphics, I think I can cover some aspects here and still talk\r\nabout it in terms of the language. Any suggestions? Please post them <a href=\"?p=76#respond\">here<\/a>.\r\n\r\n<script language=\"JavaScript\">\/\/ <![CDATA[\r\n\r\n\r\n    function grabCode_3a6ebda7446f4ab498aedb54d1bca2e8() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='3a6ebda7446f4ab498aedb54d1bca2e8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3a6ebda7446f4ab498aedb54d1bca2e8';\r\n\r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n\r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\r\n\r\n\r\n\\n');\r\n\r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\r\nPublished with MATLAB\u00ae 7.4\r\n\r\n<\/div>\r\n<!--\r\n3a6ebda7446f4ab498aedb54d1bca2e8 ##### SOURCE BEGIN #####\r\n%% String Annotations for Plots\r\n% Did you know that you can customize the direction of text in MATLAB\r\n% plots?  There are times when such annotations work better than if they\r\n% were purely horizontal or vertical.\r\n%% Sunspot Data\r\n% Let's look at some sunspot data.  First I'll load it into MATLAB.\r\ns = load('sunspot.dat');\r\n%%\r\n% Separate the year from the actual number of sunspots.\r\nyear = s(:,1);\r\nspots = s(:,2);\r\n%%\r\n% Show the data in a bar plot.\r\nbar(year,spots)\r\n%% Annotate the Year with the Maximum Number of Sunspots\r\n[smax, indmax] = max(spots);\r\n%%\r\n% The historical record shows the year\r\nyearmax = year(indmax)\r\n%%\r\n% with the maximum number of sunspots, 190.\r\n%%\r\nhold on\r\nplot(yearmax, smax, 'm*')\r\nhold off\r\nht = text(yearmax, smax,['  ',int2str(round(smax)), ' sunspots  ']);\r\n%% Use Handle Graphics to Alter the Annotation Orientation\r\n% Let's try labeling the maximum so the label is right-justified now.\r\nset(ht,'HorizontalAlignment','right')\r\n%%\r\n% Now change the label so it is at an angle, something I might want if I\r\n% were planning to label several more points on the graph.\r\nset(ht,'Rotation',45)\r\n%% Note Other Maxima\r\n% The <http:\/\/en.wikipedia.org\/wiki\/Solar_variation sunspot cycle> has a\r\n% periodicity of about 11 years.  Let's see which other years have a high\r\n% number of sunspots.\r\n[maxssa, indssa] = sort(spots,'descend');\r\nyear(indssa(1:10))\r\n%%\r\n% We see some years near the maximum year, 1957, then another cluster near\r\n% 1979, and the next at 1778.  Let me grab the first five \"distinct\"\r\n% clusters.\r\nidxdistinct = [1 4 6 7 8];\r\nyear(indssa(idxdistinct))\r\n%% Label Extra Peaks\r\n% Let's label these few other peaks in a similar manner to the first one.\r\nnearMaxYears = year(indssa(idxdistinct(2:end)))\r\nssaMax = spots(indssa(idxdistinct(2:end)))\r\nhold on\r\nplot(nearMaxYears, ssaMax, 'mo')\r\nhold off\r\nhn = text(nearMaxYears, ssaMax,['  high sunspot activity  '],...\r\n'HorizontalAlignment','Right','Rotation',25);\r\nset(ht,'Rotation',0)\r\n%% Write Around a Circle\r\n% Just for fun now, let's write a phrase around a circle of radius 1.\r\nclf, box on\r\nphrase = 'ring around the collar ';\r\nnum = length(phrase)\r\nangles = 0:(360\/num):359;\r\nx = cos(angles*pi\/180);\r\ny = sin(angles*pi\/180);\r\nfor ind = 1:length(phrase)\r\ntext(x(ind),y(ind),phrase(ind),'Rotation',angles(ind),...\r\n'HorizontalAlignment','center')\r\nend\r\naxis equal, axis([-1.1 1.1 -1.1 1.1])\r\n%% References\r\n% There is a whole lot more you can do with |text| annotation in MATLAB.\r\n% Here are some references for the language aspects of working with text.\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/matlab\/ref\/text.html |text|>\r\n% * <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/text_props.htm text properties>\r\n%\r\n% Though I don't want to turn this blog into one chiefly about graphics,\r\n% I think I can cover some aspects here and still talk about it in terms of\r\n% the language.  Any suggestions?  Please post them <?p=76#respond here>.\r\n\r\n##### SOURCE END ##### 3a6ebda7446f4ab498aedb54d1bca2e8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nDid you know that you can customize the direction of text in MATLAB plots? There are times when such annotations work better\r\nthan if they were purely horizontal or... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/02\/08\/string-annotations-for-plots\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":2404,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/76\/revisions\/2404"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}