{"id":4763,"date":"2013-08-16T09:00:37","date_gmt":"2013-08-16T13:00:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4763"},"modified":"2018-01-08T15:54:52","modified_gmt":"2018-01-08T20:54:52","slug":"line-plots-with-fewer-markers","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/08\/16\/line-plots-with-fewer-markers\/","title":{"rendered":"Line plots with fewer markers"},"content":{"rendered":"<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42560-line-fewer-markers\"><tt>line_fewer_markers<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/209745\">Massimo Ciacci<\/a>.<\/p><p>\"How can I set the intervals of markers in a plot?\"<\/p><p>This is a question that I've seen at various times, both in <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/\">MATLAB Answers<\/a> and in <a>MATLAB Newsgroup<\/a>. The question is in regards to the fact that when MATLAB puts markers on a line plot, it puts them on every data point. For plots with many data points, it becomes visually crowded.<\/p><pre class=\"codeinput\">t  = 0:0.005:pi;\r\nplot(t, sin(3*t).*cos(t\/2), <span class=\"string\">'p-'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_line_with_fewer_markers\/potw_line_with_fewer_markers_01.png\" alt=\"\"> <p><tt>line_fewer_markers<\/tt> allows you to specify the number of markers to show, regardless of how many data points are plotted.<\/p><pre class=\"codeinput\">clf;\r\nline_fewer_markers(t, sin(3*t).*cos(t\/2), 10, <span class=\"string\">'p-'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_line_with_fewer_markers\/potw_line_with_fewer_markers_02.png\" alt=\"\"> <p>It has some additional options, such as controlling how the spacing is calculated. This is useful especially when the line is curvey. In such cases, you may want to compute the marker spacing based on the distance on the curve, rather than based on the x distance.<\/p><pre class=\"codeinput\">clf;\r\nline_fewer_markers(t, sin(3*t).*cos(t\/2), 10, <span class=\"string\">'p-'<\/span>, <span class=\"string\">'Spacing'<\/span>, <span class=\"string\">'curve'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_line_with_fewer_markers\/potw_line_with_fewer_markers_03.png\" alt=\"\"> <p>And finally, the function can return a handle that can be used specifically for legend, so that it displays the correct marker and line style.<\/p><pre class=\"codeinput\">clf;\r\nh1 = line_fewer_markers(t, sin(3*t).*cos(t\/2), 10, <span class=\"string\">'p-'<\/span>, <span class=\"string\">'Spacing'<\/span>, <span class=\"string\">'curve'<\/span>);\r\nh2 = line_fewer_markers(t, -sin(t\/2).*cos(3*t), 10, <span class=\"string\">'o--r'<\/span>, <span class=\"string\">'Spacing'<\/span>, <span class=\"string\">'curve'<\/span>, <span class=\"keyword\">...<\/span>\r\n  <span class=\"string\">'MarkerSize'<\/span>, 6, <span class=\"string\">'MarkerFaceColor'<\/span>, <span class=\"string\">'r'<\/span>);\r\nlegend([h1, h2], <span class=\"string\">'Line 1'<\/span>, <span class=\"string\">'Line 2'<\/span>, <span class=\"string\">'Location'<\/span>, <span class=\"string\">'SouthEast'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_line_with_fewer_markers\/potw_line_with_fewer_markers_04.png\" alt=\"\"> <p>Massimo's function is well-documented, and he also includes a few example scripts to show the different use cases.<\/p><p><b>Comments<\/b><\/p><p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4763#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42560-line-fewer-markers#comments\">comment<\/a> for Massimo.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_e077c6f9c7db46769a63e5b275ca79d1() {\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='e077c6f9c7db46769a63e5b275ca79d1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e077c6f9c7db46769a63e5b275ca79d1';\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 2013 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_e077c6f9c7db46769a63e5b275ca79d1()\"><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; R2013a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p><\/div><!--\r\ne077c6f9c7db46769a63e5b275ca79d1 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42560-line-fewer-markers\r\n% |line_fewer_markers|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/209745\r\n% Massimo Ciacci>.\r\n%\r\n% \"How can I set the intervals of markers in a plot?\"\r\n%\r\n% This is a question that I've seen at various times, both in\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/ MATLAB Answers> and in\r\n% <http:\/\/ MATLAB Newsgroup>.\r\n% The question is in regards to the fact that when MATLAB puts markers on a\r\n% line plot, it puts them on every data point. For plots with many data\r\n% points, it becomes visually crowded.\r\n\r\nt  = 0:0.005:pi;\r\nplot(t, sin(3*t).*cos(t\/2), 'p-')\r\n\r\n%%\r\n% |line_fewer_markers| allows you to specify the number of markers to show,\r\n% regardless of how many data points are plotted.\r\n\r\nclf;\r\nline_fewer_markers(t, sin(3*t).*cos(t\/2), 10, 'p-');\r\n\r\n%%\r\n% It has some additional options, such as controlling how the spacing is\r\n% calculated. This is useful especially when the line is curvey. In such\r\n% cases, you may want to compute the marker spacing based on the distance\r\n% on the curve, rather than based on the x distance.\r\n\r\nclf;\r\nline_fewer_markers(t, sin(3*t).*cos(t\/2), 10, 'p-', 'Spacing', 'curve');\r\n\r\n%%\r\n% And finally, the function can return a handle that can be used\r\n% specifically for legend, so that it displays the correct marker and line\r\n% style.\r\n\r\nclf;\r\nh1 = line_fewer_markers(t, sin(3*t).*cos(t\/2), 10, 'p-', 'Spacing', 'curve');\r\nh2 = line_fewer_markers(t, -sin(t\/2).*cos(3*t), 10, 'oREPLACE_WITH_DASH_DASHr', 'Spacing', 'curve', ...\r\n  'MarkerSize', 6, 'MarkerFaceColor', 'r');\r\nlegend([h1, h2], 'Line 1', 'Line 2', 'Location', 'SouthEast');\r\n\r\n%%\r\n% Massimo's function is well-documented, and he also includes a few example\r\n% scripts to show the different use cases. \r\n%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4763#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42560-line-fewer-markers#comments\r\n% comment> for Massimo.\r\n\r\n##### SOURCE END ##### e077c6f9c7db46769a63e5b275ca79d1\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_line_with_fewer_markers\/potw_line_with_fewer_markers_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Jiro's pick this week is line_fewer_markers by Massimo Ciacci.\"How can I set the intervals of markers in a plot?\"This is a question that I've seen at various times, both in MATLAB Answers and in... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/08\/16\/line-plots-with-fewer-markers\/\">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\/4763"}],"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=4763"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4763\/revisions"}],"predecessor-version":[{"id":9324,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4763\/revisions\/9324"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}