{"id":11078,"date":"2019-10-11T09:44:18","date_gmt":"2019-10-11T13:44:18","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11078"},"modified":"2019-10-11T09:48:39","modified_gmt":"2019-10-11T13:48:39","slug":"programmatically-adding-data-tips","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/11\/programmatically-adding-data-tips\/","title":{"rendered":"Programmatically adding data tips"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p><em>Richard is Consulting Engineer at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace industry.<\/em><\/p><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\">Richard&#8217;s<\/a> pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips\">add_Data_Tips<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3161211-massimo-ciacci\">Massimo Ciacci<\/a>.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#04e9b3b6-bfa8-42dd-b524-9b597c2f8df8\">Pick<\/a><\/li><li><a href=\"#0ab50fc2-282e-445f-b687-67aed22b9bf6\">New in R2019b<\/a><\/li><li><a href=\"#86a05f00-6c9f-43fc-b9ec-7f42c2b85b0b\">Comments<\/a><\/li><\/ul><\/div><h4>Pick<a name=\"04e9b3b6-bfa8-42dd-b524-9b597c2f8df8\"><\/a><\/h4><p>My pick this week is the utility function <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips\">add_Data_Tips<\/a> for programmatically adding data tips to multiple lines in a plot.<\/p><p>If you write custom scripts\/functions to plot data it is often beneficial to highlight specific points in the plot such as minimums\/maximums. <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/creating_plots\/create-custom-data-tips.html\">Datatips<\/a> allow you to show the values of the data at selected locations in the figure.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/DataTipDefault.png\" alt=\"\"> <\/p><p>While you can add data tips to a plot manually, it is difficult to add data tips to multiple lines at the same location on the X-axis. <b>add_Data_Tips<\/b> solves this problem.  Using add_Data_Tips, you can insert data tips on multiple lines in a plot at a specified X-axis locations. For instance, if you want to call out the maximum value of a plot, you can use the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/max.html\">max<\/a> function to find the index of the maximum value of the Y-Axis data and specify the corresponding X-Axis data in the call to <b>add_Data_Tips<\/b> to insert a data tip in the figure at the maximum point.<\/p><pre class=\"codeinput\">n = 0:50;\r\ny = cos(.5*(n)).^3 + sin(.3*(n)).^2;\r\nfigure\r\nyh = plot(n, y);\r\n[~,indx] = max(y);\r\nadd_DataTips( n(indx) , yh, 7)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/main_datatips_01.png\" alt=\"\"> <p>If you have a figure with multiple plots, you can add data tips to any or all of the lines at the same X-Axis location by passing in the handles to the desired lines.<\/p><pre class=\"codeinput\">n = 0:50;\r\ny = cos(.5*(n)).^3 + sin(.3*(n)).^2;\r\nfigure\r\nyh(1) = plot(n, y);\r\nhold <span class=\"string\">on<\/span>\r\ny2 = sin(1.5*(n)).^3 - cos(1.3*(n)).^2;\r\nyh(2) = plot(n,y2);\r\n[~,indx] = max(y);\r\nadd_DataTips( n(indx) , yh, 7)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/main_datatips_02.png\" alt=\"\"> <p>If you already have a plot, you can get the handles to the lines by accessing the Children of the axes<\/p><pre class=\"codeinput\">CurrentAxes = gca;\r\nLinesHandles = CurrentAxes.Children;\r\nx_location = 10;\r\nadd_DataTips(x_location , LinesHandles, 7)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/main_datatips_03.png\" alt=\"\"> <p>You can use the examples above or the File Exchange submission includes a demo script, &#8216;test_add_DataTips.m&#8217;, that provides an example.<\/p><h4>New in R2019b<a name=\"0ab50fc2-282e-445f-b687-67aed22b9bf6\"><\/a><\/h4><p>If you have access to the most recent release, R2019b, you can now programmatically create datatips using the new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.datatip.datatip.html\"><tt>datatip<\/tt><\/a> function.<\/p><pre class=\"codeinput\">figure\r\nyh = plot(n, y);\r\n[~,indx] = max(y);\r\ndatatip(yh,<span class=\"string\">'DataIndex'<\/span>, indx);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/main_datatips_04.png\" alt=\"\"> <h4>Comments<a name=\"86a05f00-6c9f-43fc-b9ec-7f42c2b85b0b\"><\/a><\/h4><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11078#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips#comment\">comment<\/a> for Massimo.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b447ab04a76743fdb9e0287b6d207291() {\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='b447ab04a76743fdb9e0287b6d207291 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b447ab04a76743fdb9e0287b6d207291';\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 2019 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_b447ab04a76743fdb9e0287b6d207291()\"><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; R2019b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2019b<br><\/p><\/div><!--\r\nb447ab04a76743fdb9e0287b6d207291 ##### SOURCE BEGIN #####\r\n%%\r\n% _Richard is Consulting Engineer at MathWorks focused on the Embedded\r\n% Coder product for code generation, primarily in the Aerospace industry._\r\n%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1016860-richard-ruff\r\n% Richard's> pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips\r\n% add_Data_Tips> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3161211-massimo-ciacci\r\n% Massimo Ciacci>.\r\n%\r\n%% Pick\r\n% My pick this week is the utility function\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips\r\n% add_Data_Tips> for programmatically adding data tips to multiple lines in\r\n% a plot.\r\n%\r\n% If you write custom scripts\/functions to plot data it is often beneficial\r\n% to highlight specific points in the plot such as minimums\/maximums.\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/creating_plots\/create-custom-data-tips.html\r\n% Datatips> allow you to show the values of the data at selected locations\r\n% in the figure.\r\n%\r\n%%\r\n%\r\n% <<DataTipDefault.png>>\r\n%\r\n%%\r\n%\r\n% While you can add data tips to a plot manually, it is difficult to add\r\n% data tips to multiple lines at the same location on the X-axis.\r\n% *add_Data_Tips* solves this problem.  Using add_Data_Tips, you can insert\r\n% data tips on multiple lines in a plot at a specified X-axis locations.\r\n% For instance, if you want to call out the maximum value of a plot, you\r\n% can use the\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2019a\/matlab\/ref\/max.html max>\r\n% function to find the index of the maximum value of the Y-Axis data and\r\n% specify the corresponding X-Axis data in the call to *add_Data_Tips* to\r\n% insert a data tip in the figure at the maximum point.\r\n%\r\nn = 0:50;\r\ny = cos(.5*(n)).^3 + sin(.3*(n)).^2;\r\nfigure\r\nyh = plot(n, y);\r\n[~,indx] = max(y);\r\nadd_DataTips( n(indx) , yh, 7)\r\n\r\n%%\r\n% If you have a figure with multiple plots, you can add data tips to any or\r\n% all of the lines at the same X-Axis location by passing in the handles to\r\n% the desired lines.\r\nn = 0:50;\r\ny = cos(.5*(n)).^3 + sin(.3*(n)).^2;\r\nfigure\r\nyh(1) = plot(n, y);\r\nhold on\r\ny2 = sin(1.5*(n)).^3 - cos(1.3*(n)).^2;\r\nyh(2) = plot(n,y2);\r\n[~,indx] = max(y);\r\nadd_DataTips( n(indx) , yh, 7)\r\n\r\n%%\r\n% If you already have a plot, you can get the handles to the lines by\r\n% accessing the Children of the axes\r\nCurrentAxes = gca;\r\nLinesHandles = CurrentAxes.Children;\r\nx_location = 10;\r\nadd_DataTips(x_location , LinesHandles, 7)\r\n\r\n%%\r\n% You can use the examples above or the File Exchange submission includes a\r\n% demo script, 'test_add_DataTips.m', that provides an example.\r\n%\r\n\r\n%% New in R2019b\r\n%\r\n% If you have access to the most recent release, R2019b, you can now\r\n% programmatically create datatips using the new\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.datatip.datatip.html\r\n% |datatip|> function.\r\n\r\nfigure\r\nyh = plot(n, y);\r\n[~,indx] = max(y);\r\ndatatip(yh,'DataIndex', indx);\r\n\r\n%% Comments\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11078#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72849-add_data_tips#comment\r\n% comment> for Massimo.\r\n\r\n##### SOURCE END ##### b447ab04a76743fdb9e0287b6d207291\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/main_datatips\/DataTipDefault.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nRichard is Consulting Engineer at MathWorks focused on the Embedded Coder product for code generation, primarily in the Aerospace industry.Richard&#8217;s pick this week is add_Data_Tips by Massimo&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/11\/programmatically-adding-data-tips\/\">read more >><\/a><\/p>","protected":false},"author":131,"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\/11078"}],"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\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=11078"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11078\/revisions"}],"predecessor-version":[{"id":11086,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11078\/revisions\/11086"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}