{"id":246,"date":"2008-08-25T06:13:38","date_gmt":"2008-08-25T11:13:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/08\/25\/overlaying-information-on-a-plot\/"},"modified":"2016-04-03T15:26:36","modified_gmt":"2016-04-03T19:26:36","slug":"overlaying-information-on-a-plot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/08\/25\/overlaying-information-on-a-plot\/","title":{"rendered":"Overlaying Information on a Plot"},"content":{"rendered":"<div class=\"content\">\n<p>On a previous posting entitled <a href=\"https:\/\/blogs.mathworks.com\/community\/2007\/12\/10\/focused-on-zooming\/\">Focused on Zooming<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/community\/2007\/12\/10\/focused-on-zooming\/#comment-5471\">Bao posed a great question<\/a> that I thought would be useful to answer in a separate post. The question concerned the ability to overlay data on a plot. At its simplest, an overlay can be implemented in terms of placing a non-visible axes above the visible one. There are, however different scenarios for needing an overlay. In this post, I&#8217;ll examine the different overlays and how you might go about creating them in MATLAB.<\/p>\n<p>&nbsp;<\/p>\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#1\">Decorating an Object<\/a><\/li>\n<li><a href=\"#7\">Annotating a Plot<\/a><\/li>\n<li><a href=\"#11\">Adding Transient Information<\/a><\/li>\n<\/ul>\n<\/div>\n<h3>Decorating an Object<a name=\"1\"><\/a><\/h3>\n<p>One of the most common reasons for an overlay is to draw attention to a plot of interest. For example, if we have four subplots containing data we may wish to draw attention to one of the subplots over the others.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">figure;\r\nsubplot(2,2,1);\r\nplot(rand(3));\r\ninterestingAxes = subplot(2,2,2);\r\nplot(magic(3));\r\nsubplot(2,2,3);\r\nplot(rand(3));\r\nsubplot(2,2,4);\r\nplot(rand(3));<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_01.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>In the above plot, there is nothing to indicate whether one axes is more important than another. It would be useful to highlight the axes we wish to emphasize.<\/p>\n<p>We begin by creating a non-visible axes whose position covers the whole figure.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">invisibleAxes = axes(<span style=\"color: #a020f0;\">'Visible'<\/span>,<span style=\"color: #a020f0;\">'off'<\/span>,<span style=\"color: #a020f0;\">'Position'<\/span>,[0 0 1 1],<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XLim'<\/span>,[0 1],<span style=\"color: #a020f0;\">'YLim'<\/span>,[0 1],<span style=\"color: #a020f0;\">'HitTest'<\/span>,<span style=\"color: #a020f0;\">'off'<\/span>);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_02.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>Setting the <tt>XLim<\/tt> and <tt>YLim<\/tt> properties ensure that when we deal with coordinates normalized to the figure, they will also be normalized to the axes. The <tt>HitTest<\/tt> property tells tools such as ZOOM to ignore this axes when interacting with the figure.<\/p>\n<p>We will now create a pink border around our interesting axes using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rectangle.html\"><tt>rectangle<\/tt><\/a> command:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">border = rectangle(<span style=\"color: #a020f0;\">'Parent'<\/span>,invisibleAxes,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Position'<\/span>,get(interestingAxes,<span style=\"color: #a020f0;\">'Position'<\/span>), <span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'EdgeColor'<\/span>, [1 .4 .4],<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'LineWidth'<\/span>,4,<span style=\"color: #a020f0;\">'HitTest'<\/span>,<span style=\"color: #a020f0;\">'off'<\/span>);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_03.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>The pink rectangle now indicates which area of the figure may be important.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">close;<\/pre>\n<h3>Annotating a Plot<a name=\"7\"><\/a><\/h3>\n<p>In some cases, the figure is too large a canvas on which to communicate. In this case, we may want to focus on aspects of the data. These annotations<a> will appear on top of the data. For this case, we will use a smaller overlay axes. We will ensure that this axes has the same X and Y limits as the original plot using the <\/a><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/linkaxes.html\"><tt>linkaxes<\/tt><\/a><a> command. <\/a><\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">figure;\r\ndataAxes = axes;\r\nplot(magic(3));\r\ninvisibleAxes = axes(<span style=\"color: #a020f0;\">'Visible'<\/span>,<span style=\"color: #a020f0;\">'off'<\/span>,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Position'<\/span>,get(dataAxes,<span style=\"color: #a020f0;\">'Position'<\/span>),<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'HitTest'<\/span>,<span style=\"color: #a020f0;\">'off'<\/span>,<span style=\"color: #a020f0;\">'XLimMode'<\/span>,<span style=\"color: #a020f0;\">'manual'<\/span>,<span style=\"color: #a020f0;\">'YLimMode'<\/span>,<span style=\"color: #a020f0;\">'manual'<\/span>);\r\nlinkaxes([dataAxes invisibleAxes]);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_04.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>If we wish to draw attention to an area, we can now place information into the invisible axes using annotations.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">rectangle(<span style=\"color: #a020f0;\">'Parent'<\/span>,invisibleAxes,<span style=\"color: #a020f0;\">'Position'<\/span>,[1.6 5 .2 1],<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'EdgeColor'<\/span>,[1 .4 .4],<span style=\"color: #a020f0;\">'LineWidth'<\/span>,4);\r\ntext(<span style=\"color: #a020f0;\">'Parent'<\/span>,invisibleAxes,<span style=\"color: #a020f0;\">'String'<\/span>,<span style=\"color: #a020f0;\">'No Data Here'<\/span>,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'HorizontalAlignment'<\/span>,<span style=\"color: #a020f0;\">'Center'<\/span>,<span style=\"color: #a020f0;\">'VerticalAlignment'<\/span>,<span style=\"color: #a020f0;\">'Bottom'<\/span>,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'Position'<\/span>,[1.7 6 0]);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_05.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>Since the limits have been linked, the pink box and the associated text will travel with the data when the plot is panned or zoomed.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">zoom(2);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_06.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">close;<\/pre>\n<h3>Adding Transient Information<a name=\"11\"><\/a><\/h3>\n<p>Sometimes, the information we wish to provide may be transient. For example, if a user clicks on a line, we may want to see more information about the line. After the user is satisfied, this information should go away. This sort of interaction model can also be implemented in terms of an overlay as well in conjunction with figure callbacks.<\/p>\n<p>A more complex case may be that of a jeweler&#8217;s loupe tool. In this case, we want to create a visible axes that shows a zoomed in portion of the data being examined. The following code will create a loupe without the need for user interaction.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">figure;\r\ndataAxes = axes;\r\nplot(magic(3));\r\n<span style=\"color: #228b22;\">% Compute the center of the axes:<\/span>\r\ndataAxesPosition = get(dataAxes,<span style=\"color: #a020f0;\">'Position'<\/span>);\r\nxc = dataAxesPosition(1) + dataAxesPosition(3)\/2;\r\nyc = dataAxesPosition(2) + dataAxesPosition(4)\/2;\r\n<span style=\"color: #228b22;\">% Use this information to compute the center of an overview:<\/span>\r\nloupeAxesPosition = [xc-.125, yc-.125, .25, .25];\r\nloupeAxes = axes(<span style=\"color: #a020f0;\">'Position'<\/span>,loupeAxesPosition,<span style=\"color: #0000ff;\">...<\/span>\r\n    <span style=\"color: #a020f0;\">'XTickLabel'<\/span>,<span style=\"color: #a020f0;\">''<\/span>,<span style=\"color: #a020f0;\">'YTickLabel'<\/span>,<span style=\"color: #a020f0;\">''<\/span>,<span style=\"color: #a020f0;\">'XTick'<\/span>,[],<span style=\"color: #a020f0;\">'YTick'<\/span>,[],<span style=\"color: #a020f0;\">'Box'<\/span>,<span style=\"color: #a020f0;\">'on'<\/span>);\r\n<span style=\"color: #228b22;\">% Copy the children of the data axes over:<\/span>\r\ncopyobj(get(dataAxes,<span style=\"color: #a020f0;\">'Children'<\/span>),loupeAxes);\r\n<span style=\"color: #228b22;\">% Zoom the loupe axes by a factor of 4:<\/span>\r\nzoom(loupeAxes,4);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/overlaypost_07.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p>A fully featured jeweler&#8217;s loupe tool which uses figure callbacks may be found on the File Exchange <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=21119&amp;objectType=FILE (link no longer works)\">here<\/a>.<\/p>\n<p>In general, the axes can be used for more than displaying plots. It can also be used as a window into different aspects of the data. What sort of information have you tried to add to plots in the past?<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\nOn a previous posting entitled Focused on Zooming, Bao posed a great question that I thought would be useful to answer in a separate post. The question concerned the ability to overlay data on a&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/08\/25\/overlaying-information-on-a-plot\/\">read more >><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/246"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":3394,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/246\/revisions\/3394"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}