{"id":186,"date":"2009-06-03T18:37:50","date_gmt":"2009-06-03T18:37:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/03\/hold-everything\/"},"modified":"2009-06-04T14:38:45","modified_gmt":"2009-06-04T14:38:45","slug":"hold-everything","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/03\/hold-everything\/","title":{"rendered":"Hold Everything!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Have you ever tried to overlap more than one plot, only to find that you had to fiddle with a few times before it looked right?\r\n         Perhaps this is because you aren't take full advantage of the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/hold.html\"><tt>hold<\/tt><\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">An Example<\/a><\/li>\r\n         <li><a href=\"#2\">Plot the Data and the Function<\/a><\/li>\r\n         <li><a href=\"#3\">Try Another Way<\/a><\/li>\r\n         <li><a href=\"#8\">Why Use Hold?<\/a><\/li>\r\n         <li><a href=\"#9\">A Better Way<\/a><\/li>\r\n         <li><a href=\"#11\">Will hold all Help You?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>An Example<a name=\"1\"><\/a><\/h3>\r\n   <p>Suppose I want to plot two sets of data or functions on the same plot. How might I do that?  There are several ways.  Let's\r\n      create some data and a function.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">t = 0:0.005:1;\r\nf = @(t) sin(2*pi*10*t);\r\ns = f(t);\r\nsn = s + 0.3*randn(size(t));<\/pre><h3>Plot the Data and the Function<a name=\"2\"><\/a><\/h3>\r\n   <p>If I have everything assembled up front, I can plot all the values at once.  The function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/plot.html\"><tt>plot<\/tt><\/a> is smart enough to cycle through colors for multiple lines.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t,s,t,sn)\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_01.png\"> <h3>Try Another Way<a name=\"3\"><\/a><\/h3>\r\n   <p>Suppose we have the first data values.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t,s)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_02.png\"> <p>And then need to add some more values later.  Here's a way I can do this.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,sn)\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_03.png\"> <p>But the plot is a bit confusing, no?  The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/legend.html\"><tt>legend<\/tt><\/a> isn't very helpful since each plot has the same color and linestyle. Instead, I can specify the color(s) I want with the\r\n      <tt>plot<\/tt> commands.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t,s)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,sn,<span style=\"color: #A020F0\">'g'<\/span>)\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_04.png\"> <p>That green is too bright.  To use the one from the first plot, I can find out the correct green color by looking at the defaults.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">allcolors = get(0,<span style=\"color: #A020F0\">'defaultAxesColorOrder'<\/span>)<\/pre><pre style=\"font-style:oblique\">allcolors =\r\n            0            0            1\r\n            0          0.5            0\r\n            1            0            0\r\n            0         0.75         0.75\r\n         0.75            0         0.75\r\n         0.75         0.75            0\r\n         0.25         0.25         0.25\r\n<\/pre><p>You can see that the green is definitely toned down (the color represented by RGB values in the <tt>allcolors(2,:)<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">plot(t,s)\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,sn,<span style=\"color: #A020F0\">'color'<\/span>,allcolors(2,:))\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_05.png\"> <h3>Why Use Hold?<a name=\"8\"><\/a><\/h3>\r\n   <p>Why use <tt>hold<\/tt> at all if it's giving me headaches?  Because sometimes I want to add data to the plot using a different plot function.  Here's\r\n      an example.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fplot(f, [0 1])\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(t,sn,<span style=\"color: #A020F0\">'color'<\/span>,allcolors(2,:))\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_06.png\"> <h3>A Better Way<a name=\"9\"><\/a><\/h3>\r\n   <p>Surely there must be a better way than managing the colors on your own. And there is!  The <tt>all<\/tt> option to <tt>hold<\/tt> magically (quoting from the doc)\r\n   <\/p>\r\n   <p><html><blockquote> holds the plot and the current line color and line style so that subsequent plotting commands do not reset\r\n      the ColorOrder and ColorOrder property values to the beginning of the list. Plotting commands continue cycling through the\r\n      predefined colors and linestyles from where the last plot stopped in the list. <\/blockquote><\/html>\r\n   <\/p>\r\n   <p>Let's try it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fplot(f, [0 1])\r\nhold <span style=\"color: #A020F0\">all<\/span>\r\nplot(t,sn)\r\nlegend(<span style=\"color: #A020F0\">'signal'<\/span>,<span style=\"color: #A020F0\">'signal with noise'<\/span>,<span style=\"color: #A020F0\">'Location'<\/span>,<span style=\"color: #A020F0\">'SouthEast'<\/span>)\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/186\/holdEverything_07.png\"> \r\n<p> NOTE: I updated this since I didn't use <tt>hold off<\/tt> in my examples earlier so old lines were overwriting new ones in the legend.  Much better now. <\/p>\r\n<h3>Will hold all Help You?<a name=\"11\"><\/a><\/h3>\r\n   <p>I'd be curious to hear if the behavior of <tt>hold all<\/tt> helps you out.  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=186#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9a2431f2b46845e78f55fa2a73a67340() {\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='9a2431f2b46845e78f55fa2a73a67340 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9a2431f2b46845e78f55fa2a73a67340';\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 2009 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 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>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \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_9a2431f2b46845e78f55fa2a73a67340()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.8<br><\/p>\r\n<\/div>\r\n<!--\r\n9a2431f2b46845e78f55fa2a73a67340 ##### SOURCE BEGIN #####\r\n%% Hold Everything!\r\n% Have you ever tried to overlap more than one plot, only to find that you\r\n% had to fiddle with a few times before it looked right?  Perhaps this is\r\n% because you aren't take full advantage of the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/hold.html |hold|>.\r\n%% An Example\r\n% Suppose I want to plot two sets of data or functions on the same plot.\r\n% How might I do that?  There are several ways.  Let's create some data and\r\n% a function.\r\nt = 0:0.005:1;\r\nf = @(t) sin(2*pi*10*t);\r\ns = f(t);\r\nsn = s + 0.3*randn(size(t));\r\n%% Plot the Data and the Function\r\n% If I have everything assembled up front, I can plot all the values at\r\n% once.  The function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/plot.html |plot|>\r\n% is smart enough to cycle through colors for multiple lines.\r\nplot(t,s,t,sn)\r\nlegend('signal','signal with noise','Location','SouthEast')\r\n%% Try Another Way\r\n% Suppose we have the first data values.\r\nplot(t,s)\r\n%%\r\n% And then need to add some more values later.  Here's a way I can do this.\r\nhold on\r\nplot(t,sn)\r\nlegend('signal','signal with noise','Location','SouthEast')\r\nhold off\r\n%%\r\n% But the plot is a bit confusing, no?  The\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/legend.html |legend|>\r\n% isn't very helpful since each plot has the same color and linestyle.\r\n% Instead, I can specify the color(s) I want with the |plot| commands.\r\nplot(t,s)\r\nhold on\r\nplot(t,sn,'g')\r\nlegend('signal','signal with noise','Location','SouthEast')\r\nhold off\r\n%%\r\n% That green is too bright.  To use the one from the first plot, I can find\r\n% out the correct green color by looking at the defaults.\r\nallcolors = get(0,'defaultAxesColorOrder')\r\n%%\r\n% You can see that the green is definitely toned down (the color\r\n% represented by RGB values in the |allcolors(2,:)|.\r\nplot(t,s)\r\nhold on\r\nplot(t,sn,'color',allcolors(2,:))\r\nlegend('signal','signal with noise','Location','SouthEast')\r\nhold off\r\n%% Why Use Hold?\r\n% Why use |hold| at all if it's giving me headaches?  Because sometimes I\r\n% want to add data to the plot using a different plot function.  Here's an\r\n% example.\r\nfplot(f, [0 1])\r\nhold on\r\nplot(t,sn,'color',allcolors(2,:))\r\nlegend('signal','signal with noise','Location','SouthEast')\r\nhold off\r\n%% A Better Way\r\n% Surely there must be a better way than managing the colors on your own.\r\n% And there is!  The |all| option to |hold| magically (quoting from the \r\n% doc)\r\n%\r\n% <html><blockquote> \r\n% holds the plot and the current line color and line style\r\n% so that subsequent plotting commands do not reset the ColorOrder and\r\n% ColorOrder property values to the beginning of the list. Plotting\r\n% commands continue cycling through the predefined colors and linestyles\r\n% from where the last plot stopped in the list.\r\n% <\/blockquote><\/html>\r\n%\r\n%%\r\n% Let's try it.\r\nfplot(f, [0 1])\r\nhold all\r\nplot(t,sn)\r\nlegend('signal','signal with noise','Location','SouthEast')\r\nhold off\r\n%% Will hold all Help You?\r\n% I'd be curious to hear if the behavior of |hold all| helps you out.  Let\r\n% me know <https:\/\/blogs.mathworks.com\/loren\/?p=186#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 9a2431f2b46845e78f55fa2a73a67340\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Have you ever tried to overlap more than one plot, only to find that you had to fiddle with a few times before it looked right?\r\n         Perhaps this is because you aren't... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/06\/03\/hold-everything\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,15,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/186"}],"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=186"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}