{"id":8888,"date":"2017-10-13T09:00:13","date_gmt":"2017-10-13T13:00:13","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8888"},"modified":"2017-11-03T18:34:17","modified_gmt":"2017-11-03T22:34:17","slug":"labeling-data-points","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/10\/13\/labeling-data-points\/","title":{"rendered":"Labeling Data Points"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46891-labelpoints\"><tt>labelpoints<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3753776\">Adam Danz<\/a>.<\/p><p>This week&#8217;s entry caught my attention for two reasons. One is that this entry does the task that I usually dread doing, which is making finishing touches to my plots. When I want to label some data points, I use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/text.html\"><tt>text<\/tt><\/a> function. It gives me some control of how to align the text, but it is basically limited to the extent of the text. For example, I can do this<\/p><pre class=\"codeinput\">x = [0 0 0];\r\ny = [.8 .7 .6];\r\nlabels = {<span class=\"string\">'label 1'<\/span>,<span class=\"string\">'label 2'<\/span>,<span class=\"string\">'label 3'<\/span>};\r\nplot(x,y,<span class=\"string\">'o'<\/span>)\r\ntext(x,y,labels,<span class=\"string\">'VerticalAlignment'<\/span>,<span class=\"string\">'bottom'<\/span>,<span class=\"string\">'HorizontalAlignment'<\/span>,<span class=\"string\">'right'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_labelpoints\/potw_labelpoints_01.png\" alt=\"\"> <p>Or this.<\/p><pre class=\"codeinput\">plot(x,y,<span class=\"string\">'o'<\/span>)\r\ntext(x,y,labels,<span class=\"string\">'VerticalAlignment'<\/span>,<span class=\"string\">'top'<\/span>,<span class=\"string\">'HorizontalAlignment'<\/span>,<span class=\"string\">'left'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_labelpoints\/potw_labelpoints_02.png\" alt=\"\"> <p>But both of these aren&#8217;t exactly what I want because the labels slightly overlap the data. Also in the second one, the label goes beyond the bounds of the axes.<\/p><p>Usually at this point, I fiddle around with the coordinates of the text placements. Very doable, but tedious.<\/p><p>This is where Adam&#8217;s entry comes into play.<\/p><pre class=\"codeinput\">plot(x,y,<span class=\"string\">'o'<\/span>)\r\nlabelpoints(x,y,labels,<span class=\"string\">'SE'<\/span>,0.2,1)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_labelpoints\/potw_labelpoints_03.png\" alt=\"\"> <p><tt>'SE'<\/tt> means southeast placement, 0.2 refers to the offset for the labels, and 1 means &#8220;adjust the x\/y limits of the axes&#8221;. Perfect!<\/p><p>He also includes a wealth of options, one of which I found interesting being the option to detect outliers and only placing labels on the outliers. By making use of the <a href=\"https:\/\/www.mathworks.com\/products\/statistics.html\">Statistics and Machine Learning Toolbox<\/a>, he provides different methods for detecting outliers,<\/p><pre class=\"codeinput\">x = 0:0.01:1;\r\ny = (0:0.01:1)+rand(1,101);\r\nlabs = 1:101;\r\nplot(x,y,<span class=\"string\">'o'<\/span>)\r\nlabelpoints(x,y,labs,<span class=\"string\">'outliers_lin'<\/span>,{<span class=\"string\">'sd'<\/span>, 1.5})\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_labelpoints\/potw_labelpoints_04.png\" alt=\"\"> <p>The other reason Adam&#8217;s entry caught my attention was the amount of help and information he included in the entry. The first 300 lines of his code are help comments!! He includes many examples to test out all of the various options. As you can see from the File Exchange page, he has been keeping the file up-to-date periodically. I can tell he has put in a lot of time and effort into making and maintaining this code. I&#8217;m grateful that he has shared this with the community. Thanks Adam!<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8888#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46891-labelpoints#comment\">comment<\/a> for Adam.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_03701d5686464f9f82f77285f28e72b1() {\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='03701d5686464f9f82f77285f28e72b1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 03701d5686464f9f82f77285f28e72b1';\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        copyright = 'Copyright 2017 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_03701d5686464f9f82f77285f28e72b1()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><\/div><!--\r\n03701d5686464f9f82f77285f28e72b1 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46891-labelpoints |labelpoints|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3753776 Adam\r\n% Danz>.\r\n%\r\n% This week's entry caught my attention for two reasons. One is that this\r\n% entry does the task that I usually dread doing, which is making finishing\r\n% touches to my plots. When I want to label some data points, I use the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/text.html |text|> function. It\r\n% gives me some control of how to align the text, but it is basically\r\n% limited to the extent of the text. For example, I can do this\r\n\r\nx = [0 0 0];\r\ny = [.8 .7 .6];\r\nlabels = {'label 1','label 2','label 3'};\r\nplot(x,y,'o')\r\ntext(x,y,labels,'VerticalAlignment','bottom','HorizontalAlignment','right')\r\n\r\n%%\r\n% Or this.\r\nplot(x,y,'o')\r\ntext(x,y,labels,'VerticalAlignment','top','HorizontalAlignment','left')\r\n\r\n%%\r\n% But both of these aren't exactly what I want because the labels slightly\r\n% overlap the data. Also in the second one, the label goes beyond the\r\n% bounds of the axes.\r\n%\r\n% Usually at this point, I fiddle around with the coordinates of the text\r\n% placements. Very doable, but tedious.\r\n%\r\n% This is where Adam's entry comes into play.\r\n\r\nplot(x,y,'o')\r\nlabelpoints(x,y,labels,'SE',0.2,1)\r\n\r\n%%\r\n% |'SE'| means southeast placement, 0.2 refers to the offset for the\r\n% labels, and 1 means \"adjust the x\/y limits of the axes\". Perfect!\r\n%\r\n% He also includes a wealth of options, one of which I found interesting\r\n% being the option to detect outliers and only placing labels on the\r\n% outliers. By making use of the\r\n% <https:\/\/www.mathworks.com\/products\/statistics.html Statistics and\r\n% Machine Learning Toolbox>, he provides different methods for detecting\r\n% outliers,\r\n\r\nx = 0:0.01:1;\r\ny = (0:0.01:1)+rand(1,101);\r\nlabs = 1:101;\r\nplot(x,y,'o')\r\nlabelpoints(x,y,labs,'outliers_lin',{'sd', 1.5})\r\n\r\n%%\r\n% The other reason Adam's entry caught my attention was the amount of help\r\n% and information he included in the entry. The first 300 lines of his code\r\n% are help comments!! He includes many examples to test out all of the\r\n% various options. As you can see from the File Exchange page, he has been\r\n% keeping the file up-to-date periodically. I can tell he has put in a lot\r\n% of time and effort into making and maintaining this code. I'm grateful\r\n% that he has shared this with the community. Thanks Adam!\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8888#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46891-labelpoints#comment\r\n% comment> for Adam.\r\n\r\n##### SOURCE END ##### 03701d5686464f9f82f77285f28e72b1\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_labelpoints\/potw_labelpoints_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro&#8216;s pick this week is labelpoints by Adam Danz.This week&#8217;s entry caught my attention for two reasons. One is that this entry does the task that I usually dread doing, which is making&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/10\/13\/labeling-data-points\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8888"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8888"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8888\/revisions"}],"predecessor-version":[{"id":8928,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8888\/revisions\/8928"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}