{"id":1238,"date":"2015-09-16T09:46:21","date_gmt":"2015-09-16T14:46:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1238"},"modified":"2015-09-17T13:19:59","modified_gmt":"2015-09-17T18:19:59","slug":"individual-axis-customization","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2015\/09\/16\/individual-axis-customization\/","title":{"rendered":"Individual Axis Customization"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><!--introduction--><p>The R2015b release contains some new features for individual axis customization. Ben Hinkle, who works with the graphics team sometimes, goes through a number of plotting scenarios where these features can help simplify your code. This post shows some nicer techniques for making these pretty plots using R2015b vs. those in <a href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/06\/11\/advice-for-making-prettier-plots\">Loren's recent post<\/a> using early versions of MATLAB.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#29ed940d-3c9c-4431-9231-697cd0ebeb85\">Plotting temperature data<\/a><\/li><li><a href=\"#f2562e66-eeb5-4388-a64d-aedaf4ff7621\">Tick labels for degrees<\/a><\/li><li><a href=\"#9ef1adf7-35ae-4fca-b691-3d406ba8eac5\">Minor tick control<\/a><\/li><li><a href=\"#afc4b610-6853-4e1f-adee-ca16c3970f19\">Plotting currency data<\/a><\/li><li><a href=\"#37a3ed71-13e9-471c-8b47-e8621514bd38\">Tick label precision<\/a><\/li><li><a href=\"#2e1dff59-8864-497d-8e0a-84549d8004c9\">Tick labels for currency<\/a><\/li><li><a href=\"#ba0cee50-1648-45ba-ab44-c7b4b1509598\">Formatting for large integer ticks<\/a><\/li><li><a href=\"#ffd2d2e9-091c-4880-a636-26003a33ca77\">Exponent control<\/a><\/li><li><a href=\"#2e3ab002-8e94-49d0-8973-5067c48fc2fd\">Font and line control<\/a><\/li><li><a href=\"#c8f50e29-0c8b-4bae-9a91-6c953f8daa38\">Axes crossing through the origin<\/a><\/li><li><a href=\"#bbc1b34f-d1dc-49ba-b94c-46f1c7e49e4b\">Conclusion<\/a><\/li><\/ul><\/div><h4>Plotting temperature data<a name=\"29ed940d-3c9c-4431-9231-697cd0ebeb85\"><\/a><\/h4><p>To begin, let's plot some sample temperature data across a full year.<\/p><pre class=\"codeinput\">t1=datetime(2015,1:12,1);\r\ntemp = [0 2 12 11 15 25 23 27 25 24 12 8];\r\nplot(t1,temp,<span class=\"string\">'-o'<\/span>);\r\ntitle(<span class=\"string\">'Temperature by Month'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_01.png\" alt=\"\"> <h4>Tick labels for degrees<a name=\"f2562e66-eeb5-4388-a64d-aedaf4ff7621\"><\/a><\/h4><p>To access the new features, you get the current axes object by storing the result of <tt>gca<\/tt> in a variable. Then you use the <tt>XAxis<\/tt>, <tt>YAxis<\/tt> or <tt>ZAxis<\/tt> properties to customize a particular axis. Let's customize the y-axis tick labels to contain a degree symbol. The syntax for the <tt>TickLabelFormat<\/tt> property is a <tt>printf<\/tt> format. These formats use <tt>%g<\/tt> to stand for the default numeric formatting and <tt>\\\\circ<\/tt> to get the TeX command for a degree symbol. Why <tt>\\\\circ<\/tt> and not just <tt>\\circ<\/tt>? The reason is that <tt>\\<\/tt> is the escape character for both TeX and printf formats. So we have \"escape the escape\" by writing <tt>\\\\<\/tt>.<\/p><pre class=\"codeinput\">ax = gca;\r\nax.YAxis.TickLabelFormat = <span class=\"string\">'%g\\\\circ'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_02.png\" alt=\"\"> <h4>Minor tick control<a name=\"9ef1adf7-35ae-4fca-b691-3d406ba8eac5\"><\/a><\/h4><p>Let's turn on the grids to make it easier to read off values from the chart.<\/p><pre class=\"codeinput\">grid <span class=\"string\">on<\/span>\r\nax.YMinorGrid = <span class=\"string\">'on'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_03.png\" alt=\"\"> <p>We can control the spacing of the minor tick using the new <tt>MinorTickValues<\/tt> property. For example, let's set the minor step to be one degree.<\/p><pre class=\"codeinput\">step = 1;\r\nax.YAxis.MinorTickValues = setdiff(0:step:30,ax.YTick);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_04.png\" alt=\"\"> <h4>Plotting currency data<a name=\"afc4b610-6853-4e1f-adee-ca16c3970f19\"><\/a><\/h4><p>Suppose we plot currency data instead of temperature data. Here is the same dataset as before except this time we'll customize the y-axis for currency values. The next several sections will step through the process slowly. First, let's make the plot without any y-axis customization.<\/p><pre class=\"codeinput\">income = temp;\r\nplot(t1,income,<span class=\"string\">'-o'<\/span>);\r\ntitle(<span class=\"string\">'Income by Month'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_05.png\" alt=\"\"> <h4>Tick label precision<a name=\"37a3ed71-13e9-471c-8b47-e8621514bd38\"><\/a><\/h4><p>We can show the tick labels with exactly two decimal places by customizing the tick label format. The format <tt>%.2f<\/tt> specifies exactly 2 digits of precision after the decimal point. The <tt>f<\/tt> stands for \"fixed\".<\/p><pre class=\"codeinput\">ax.YAxis.TickLabelFormat = <span class=\"string\">'%.2f'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_06.png\" alt=\"\"> <p>By the way, does anyone know the true history behind the abbreviations in printf formats? The format <tt>g<\/tt> presumably stands for \"general\" but I've never found real proof.<\/p><h4>Tick labels for currency<a name=\"2e1dff59-8864-497d-8e0a-84549d8004c9\"><\/a><\/h4><p>We can do even better by prepending a currency symbol. The unicode codepoint for the euro, for example, is 02AC in hexidecimal. The printf format <tt>\\xNNNN<\/tt> produces a single character with unicode hexidecimal value <tt>NNNN<\/tt>. Prepend the euro (and a single space) to our existing format.<\/p><pre class=\"codeinput\">ax.YAxis.TickLabelFormat = <span class=\"string\">'\\x20AC %.2f'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_07.png\" alt=\"\"> <h4>Formatting for large integer ticks<a name=\"ba0cee50-1648-45ba-ab44-c7b4b1509598\"><\/a><\/h4><p>Suppose now our income ranges from 0 to 30 million instead of just 0 to 30. We can use the format <tt>%,d<\/tt> to get integers with commas inserted every three digits. The comma flag \",\" is an extension which is only supported in graphics and is not available generally in MATLAB. The comma flag can be used with any of the format types. A common mistake is to use <tt>%d<\/tt> for doubles. Instead <tt>%d<\/tt> produces \"decimal\" integers.<\/p><pre class=\"codeinput\">plot(t1,income*1e6,<span class=\"string\">'-o'<\/span>);\r\ntitle(<span class=\"string\">'Income by Month'<\/span>)\r\nax.YAxis.Exponent = 0;\r\nax.YAxis.TickLabelFormat = <span class=\"string\">'\\x20AC %,d'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_08.png\" alt=\"\"> <h4>Exponent control<a name=\"ffd2d2e9-091c-4880-a636-26003a33ca77\"><\/a><\/h4><p>The previous section set the y-axis <tt>Exponent<\/tt> to 0 to suppress the usual graphics behavior of factoring out large powers of 10. We can set the <tt>Exponent<\/tt> to other values to specify a particular power of 10. For example, we can set the exponent to a negative value like -6.<\/p><pre class=\"codeinput\">data = temp;\r\nplot(t1,data*1e-5,<span class=\"string\">'-o'<\/span>)\r\nax.YAxis.Exponent = -6;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_09.png\" alt=\"\"> <h4>Font and line control<a name=\"2e3ab002-8e94-49d0-8973-5067c48fc2fd\"><\/a><\/h4><p>We can also customize the fonts, lines and other properties for individual dimensions. For example,<\/p><pre class=\"codeinput\">ax.YAxis.FontWeight = <span class=\"string\">'bold'<\/span>;\r\nax.YAxis.LineWidth = 2;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_10.png\" alt=\"\"> <h4>Axes crossing through the origin<a name=\"c8f50e29-0c8b-4bae-9a91-6c953f8daa38\"><\/a><\/h4><p>A final customization is the ability to draw the x-axis and y-axis through the origin. This is done using the new <tt>'origin'<\/tt> value for the <tt>XAxisLocation<\/tt> and <tt>YAxisLocation<\/tt> properties.<\/p><pre class=\"codeinput\">x = -2:.1:2;\r\nplot(x, x.^2-1)\r\ntitle(<span class=\"string\">'x^2 - 1'<\/span>)\r\nax.XAxisLocation = <span class=\"string\">'origin'<\/span>;\r\nax.YAxisLocation = <span class=\"string\">'origin'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_11.png\" alt=\"\"> <h4>Conclusion<a name=\"bbc1b34f-d1dc-49ba-b94c-46f1c7e49e4b\"><\/a><\/h4><p>That ends the tour of new R2015b features for individual axis customization. Which new feature will be most useful for you?  Let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1238#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_391bc11d622f42b4bb3aca57ee136757() {\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='391bc11d622f42b4bb3aca57ee136757 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 391bc11d622f42b4bb3aca57ee136757';\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 2015 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_391bc11d622f42b4bb3aca57ee136757()\"><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; R2015b<br><\/p><\/div><!--\r\n391bc11d622f42b4bb3aca57ee136757 ##### SOURCE BEGIN #####\r\n%% Individual Axis Customization\r\n% The R2015b release contains some new features for individual axis\r\n% customization. Ben Hinkle, who works with the graphics team sometimes,\r\n% goes through a number of plotting scenarios where these features can\r\n% help simplify your code. This post shows some nicer techniques for making\r\n% these pretty plots using R2015b vs. those in\r\n% <https:\/\/blogs.mathworks.com\/loren\/2015\/06\/11\/advice-for-making-prettier-plots\r\n% Loren's recent post> using early versions of MATLAB.\r\n\r\n%% Plotting temperature data\r\n% To begin, let's plot some sample temperature data across a full year.\r\nt1=datetime(2015,1:12,1);\r\ntemp = [0 2 12 11 15 25 23 27 25 24 12 8];\r\nplot(t1,temp,'-o');\r\ntitle('Temperature by Month')\r\n\r\n%% Tick labels for degrees\r\n% To access the new features, you get the current axes object by\r\n% storing the result of |gca| in a variable. Then you use the \r\n% |XAxis|, |YAxis| or |ZAxis| properties to customize a particular\r\n% axis. Let's customize the y-axis tick labels to contain a \r\n% degree symbol. The syntax for the |TickLabelFormat| property\r\n% is a |printf| format. These formats use |%g| to stand for the \r\n% default numeric formatting and |\\\\circ| to get the TeX command\r\n% for a degree symbol. Why |\\\\circ| and not just |\\circ|? The\r\n% reason is that |\\| is the escape character for both TeX\r\n% and printf formats. So we have \"escape the escape\" by writing\r\n% |\\\\|.\r\nax = gca;\r\nax.YAxis.TickLabelFormat = '%g\\\\circ';\r\n\r\n%% Minor tick control\r\n% Let's turn on the grids to make it easier to read off values \r\n% from the chart.\r\ngrid on\r\nax.YMinorGrid = 'on';\r\n\r\n%% \r\n% We can control the spacing of the minor tick using the new\r\n% |MinorTickValues| property. For example, let's set the \r\n% minor step to be one degree.\r\nstep = 1;\r\nax.YAxis.MinorTickValues = setdiff(0:step:30,ax.YTick);\r\n\r\n\r\n%% Plotting currency data\r\n% Suppose we plot currency data instead of temperature data.\r\n% Here is the same dataset as before except this time we'll\r\n% customize the y-axis for currency values. The next several\r\n% sections will step through the process slowly. First, let's\r\n% make the plot without any y-axis customization.\r\nincome = temp;\r\nplot(t1,income,'-o');\r\ntitle('Income by Month')\r\n\r\n%% Tick label precision\r\n% We can show the tick labels with exactly two decimal places\r\n% by customizing the tick label format. The format |%.2f| \r\n% specifies exactly 2 digits of precision after the decimal\r\n% point. The |f| stands for \"fixed\". \r\nax.YAxis.TickLabelFormat = '%.2f';\r\n\r\n%%\r\n% By the way, does anyone know\r\n% the true history behind the abbreviations in printf formats?\r\n% The format |g| presumably stands for \"general\" but I've never\r\n% found real proof. \r\n\r\n%% Tick labels for currency\r\n% We can do even better by prepending a currency symbol. The\r\n% unicode codepoint for the euro, for example, is 02AC in\r\n% hexidecimal. The printf format |\\xNNNN| produces a single\r\n% character with unicode hexidecimal value |NNNN|.\r\n% Prepend the euro (and a single space) to\r\n% our existing format. \r\nax.YAxis.TickLabelFormat = '\\x20AC %.2f';\r\n\r\n%% Formatting for large integer ticks\r\n% Suppose now our income ranges from 0 to 30 million instead of just\r\n% 0 to 30. We can use the format |%,d| to get integers with commas\r\n% inserted every three digits. The comma flag \",\" is an extension\r\n% which is only supported in graphics and is not available generally\r\n% in MATLAB. The comma flag can be used with any of the format types.\r\n% A common mistake is to use |%d| for doubles. \r\n% Instead |%d| produces \"decimal\" integers.\r\nplot(t1,income*1e6,'-o');\r\ntitle('Income by Month')\r\nax.YAxis.Exponent = 0;\r\nax.YAxis.TickLabelFormat = '\\x20AC %,d';\r\n\r\n\r\n%% Exponent control\r\n% The previous section set the y-axis |Exponent| to 0 to suppress\r\n% the usual graphics behavior of factoring out large powers\r\n% of 10. We can set the |Exponent| to other values to specify a\r\n% particular power of 10. For example, we can set the exponent to\r\n% a negative value like -6.\r\ndata = temp;\r\nplot(t1,data*1e-5,'-o')\r\nax.YAxis.Exponent = -6;\r\n\r\n%% Font and line control\r\n% We can also customize the fonts, lines and other properties\r\n% for individual dimensions. For example,\r\nax.YAxis.FontWeight = 'bold';\r\nax.YAxis.LineWidth = 2;\r\n\r\n%% Axes crossing through the origin\r\n% A final customization is the ability to draw the x-axis and y-axis\r\n% through the origin. This is done using the new |'origin'| value\r\n% for the |XAxisLocation| and |YAxisLocation| properties.\r\nx = -2:.1:2;\r\nplot(x, x.^2-1)\r\ntitle('x^2 - 1')\r\nax.XAxisLocation = 'origin';\r\nax.YAxisLocation = 'origin';\r\n\r\n%% Conclusion\r\n% That ends the tour of new R2015b features for individual axis\r\n% customization. Which new feature will be most useful for you?  Let us\r\n% know <https:\/\/blogs.mathworks.com\/loren\/?p=1238#respond here>.\r\n##### SOURCE END ##### 391bc11d622f42b4bb3aca57ee136757\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/ruler_blog_11.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>The R2015b release contains some new features for individual axis customization. Ben Hinkle, who works with the graphics team sometimes, goes through a number of plotting scenarios where these features can help simplify your code. This post shows some nicer techniques for making these pretty plots using R2015b vs. those in <a href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/06\/11\/advice-for-making-prettier-plots\">Loren's recent post<\/a> using early versions of MATLAB.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/09\/16\/individual-axis-customization\/\">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,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1238"}],"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=1238"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1238\/revisions"}],"predecessor-version":[{"id":1244,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1238\/revisions\/1244"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}