{"id":228,"date":"2010-04-30T11:54:57","date_gmt":"2010-04-30T11:54:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/30\/coordinating-views\/"},"modified":"2010-04-30T11:56:58","modified_gmt":"2010-04-30T11:56:58","slug":"coordinating-views","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/30\/coordinating-views\/","title":{"rendered":"Coordinating Views"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Ever wanted to have coordinated views of multiples plots when you pan or zoom?  You can do this in MATLAB.<\/p>\r\n   <\/introduction>\r\n   <p>Let's suppose I have two plots and I want to be sure they keep their x-axes synchronized.  I'll start by loading the sunspot\r\n      data.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sdata = load(<span style=\"color: #A020F0\">'sunspot.dat'<\/span>);\r\ntime = sdata(:,1);\r\nspots = sdata(:,2);<\/pre><p>I've heard that there is an 11-year cycle for sunspots.  So let me generate a sinusoid with a period of 11 years so we can\r\n      then do a visual comparison (vs. the more appropriate Fourier analysis!).\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">cycle11 = sin(time*pi*2\/11);<\/pre><p>Now let's plot the two curves, but in different axes since the scalings are quite different and I don't want to worry about\r\n      them.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">hspots = subplot(2,1,1);\r\nplot(time,spots)\r\nh11 = subplot(2,1,2);\r\nplot(time,cycle11)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/228\/linkingAxes_01.png\"> <p>Here I ensure the plots have the same x-axis by explicitly plotting the data using the same x-coordinates.  But let's say\r\n      I want to zoom in now. I can achieve this by setting both x-axes with the same limits.  I could do them one at a time, or\r\n      together.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">set([hspots h11],<span style=\"color: #A020F0\">'xlim'<\/span>,[1900 1950]);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/228\/linkingAxes_02.png\"> <p>I can still make \"mistakes\" though, by setting the limits on only one of the axes.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">set(h11,<span style=\"color: #A020F0\">'xlim'<\/span>,[1850 1900])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/228\/linkingAxes_03.png\"> <p>I can achieve the synchronization, with a little less mental effort than carrying around all affected axes, by using the function\r\n      <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/linkaxes.html\">|linkaxes<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">linkaxes([h11 hspots],<span style=\"color: #A020F0\">'x'<\/span>)\r\nset(h11,<span style=\"color: #A020F0\">'xlim'<\/span>,[1800 1850])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/228\/linkingAxes_04.png\"> <p>Do you have a simple way to coordinate your plots?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=228#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d9fbdc28669e449a94821340a5b75161() {\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='d9fbdc28669e449a94821340a5b75161 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d9fbdc28669e449a94821340a5b75161';\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 2010 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_d9fbdc28669e449a94821340a5b75161()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\nd9fbdc28669e449a94821340a5b75161 ##### SOURCE BEGIN #####\r\n%% Coordinating Views\r\n% Ever wanted to have coordinated views of multiples plots when you pan or\r\n% zoom?  You can do this in MATLAB.\r\n%%\r\n% Let's suppose I have two plots and I want to be sure they keep their\r\n% x-axes synchronized.  I'll start by loading the sunspot data.\r\nsdata = load('sunspot.dat');\r\ntime = sdata(:,1);\r\nspots = sdata(:,2);\r\n%%\r\n% I've heard that there is an 11-year cycle for sunspots.  So let me\r\n% generate a sinusoid with a period of 11 years so we can then do a visual\r\n% comparison (vs. the more appropriate Fourier analysis!).\r\ncycle11 = sin(time*pi*2\/11);\r\n%%\r\n% Now let's plot the two curves, but in different axes since the scalings\r\n% are quite different and I don't want to worry about them.\r\nhspots = subplot(2,1,1);\r\nplot(time,spots)\r\nh11 = subplot(2,1,2);\r\nplot(time,cycle11)\r\n%% \r\n% Here I ensure the plots have the same x-axis by explicitly plotting the\r\n% data using the same x-coordinates.  But let's say I want to zoom in now.\r\n% I can achieve this by setting both x-axes with the same limits.  I could\r\n% do them one at a time, or together.\r\nset([hspots h11],'xlim',[1900 1950]);\r\n%%\r\n% I can still make \"mistakes\" though, by setting the limits on only one of\r\n% the axes.\r\nset(h11,'xlim',[1850 1900])\r\n%%\r\n% I can achieve the synchronization, with a little less mental effort than\r\n% carrying around all affected axes, by using the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/linkaxes.html\r\n% |linkaxes>.\r\nlinkaxes([h11 hspots],'x')\r\nset(h11,'xlim',[1800 1850])\r\n%%\r\n% Do you have a simple way to coordinate your plots?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=228#respond here>.\r\n\r\n\r\n##### SOURCE END ##### d9fbdc28669e449a94821340a5b75161\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Ever wanted to have coordinated views of multiples plots when you pan or zoom?  You can do this in MATLAB.\r\n   \r\n   Let's suppose I have two plots and I want to be sure they keep... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/30\/coordinating-views\/\">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],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/228"}],"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=228"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}