{"id":1182,"date":"2015-06-11T08:27:29","date_gmt":"2015-06-11T13:27:29","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1182"},"modified":"2016-08-04T09:18:37","modified_gmt":"2016-08-04T14:18:37","slug":"advice-for-making-prettier-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2015\/06\/11\/advice-for-making-prettier-plots\/","title":{"rendered":"Advice for Making Prettier Plots"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>A few years ago, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871-jiro-doke\">Jiro<\/a> wrote a <a href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/11\/making-pretty-graphs\/\">popular post<\/a> for making pretty plots on this blog.  We also host a <a href=\"https:\/\/blogs.mathworks.com\/graphics\">blog specifically about graphics<\/a> by Mike.  And with the R2014b release of MATLAB came an updated graphics system that Dave described last year in a 3 part series: <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/03\/matlab-r2014b-graphics-part-1-features-of-the-new-graphics-system\/\">Part 1<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/14\/matlab-r2014b-graphics-part-2-using-graphics-objects\/\">Part 2<\/a>, and <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/11\/05\/matlab-r2014b-graphics-part-3-compatibility-considerations-in-the-new-graphics-system\/\">Part 3<\/a>.<\/p><p>Even with that, I continue to hear questions about how to accomplish certain tasks, such as using a symbol to indicate degrees. This post contains a collection of a few tips that may help you update your plots to match more closely what you are trying to convey.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#b4fcce1b-f026-4261-9d92-b40d52b9fb2e\">Plotting Temperature Data<\/a><\/li><li><a href=\"#f5eb051b-cb52-4e0f-8fe1-e51dc62a0ded\">Add Y Label with Degree Symbol<\/a><\/li><li><a href=\"#c9a96723-802b-458a-9b98-7859d6d209b2\">Find the Y Labels<\/a><\/li><li><a href=\"#ce310c68-dc33-4a38-96c0-350f66b20fc6\">Append the Degree Symbol<\/a><\/li><li><a href=\"#e2b0a5a3-fd36-4bd3-830a-ea6bcb5f2a22\">Reset the Labels<\/a><\/li><li><a href=\"#b8d4d558-a808-46a7-8759-c0cfbebd5829\">Suppose the Y Axis is Money, Not Degrees<\/a><\/li><li><a href=\"#88e4aee8-950c-4168-b765-2b7a109593da\">Update the Y Tick Labels with Euros<\/a><\/li><li><a href=\"#d87fc6c4-95f8-4247-a518-4d8f8863c934\">Finally Annotate a Plot with Some Math<\/a><\/li><li><a href=\"#a45a6934-91a4-4c92-9d4a-6ab87eb68e85\">Call to Action<\/a><\/li><\/ul><\/div><h4>Plotting Temperature Data<a name=\"b4fcce1b-f026-4261-9d92-b40d52b9fb2e\"><\/a><\/h4><p>Suppose we have some temperature data to plot and want to add tick marks that include the degree symbol.  Here's some ways to achieve this.<\/p><p>Mock up data for the first of each month this year.<\/p><pre class=\"codeinput\">t1 = datetime(2014,1:12,1);\r\ntemp = [0 2 12 11 15 25 23 27 25 24 12 8];\r\n<\/pre><h4>Add Y Label with Degree Symbol<a name=\"f5eb051b-cb52-4e0f-8fe1-e51dc62a0ded\"><\/a><\/h4><pre class=\"codeinput\">h = plot(t1,temp,<span class=\"string\">':*'<\/span>);\r\nax = h.Parent;\r\ntitle(<span class=\"string\">'A Year of Temperatures on the 1st of the Month'<\/span>)\r\nylabel(<span class=\"string\">'Degrees Celcius ^{\\circ}'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/prettierPlots_01.png\" alt=\"\"> <h4>Find the Y Labels<a name=\"c9a96723-802b-458a-9b98-7859d6d209b2\"><\/a><\/h4><pre class=\"codeinput\">ytl = ax.YTickLabel\r\n<\/pre><pre class=\"codeoutput\">ytl = \r\n    '0'\r\n    '5'\r\n    '10'\r\n    '15'\r\n    '20'\r\n    '25'\r\n    '30'\r\n<\/pre><h4>Append the Degree Symbol<a name=\"ce310c68-dc33-4a38-96c0-350f66b20fc6\"><\/a><\/h4><pre class=\"codeinput\">ytld = strcat(ytl,<span class=\"string\">'^{\\circ}'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">ytld = \r\n    '0^{\\circ}'\r\n    '5^{\\circ}'\r\n    '10^{\\circ}'\r\n    '15^{\\circ}'\r\n    '20^{\\circ}'\r\n    '25^{\\circ}'\r\n    '30^{\\circ}'\r\n<\/pre><h4>Reset the Labels<a name=\"e2b0a5a3-fd36-4bd3-830a-ea6bcb5f2a22\"><\/a><\/h4><pre class=\"codeinput\">ax.YTickLabel = ytld;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/prettierPlots_02.png\" alt=\"\"> <h4>Suppose the Y Axis is Money, Not Degrees<a name=\"b8d4d558-a808-46a7-8759-c0cfbebd5829\"><\/a><\/h4><p>First replace the labels with the original values.  Then update the Y axis label. If you want to use the &#8364; symbol and it is not on your keyboard, you can use <tt>char(8364)<\/tt>; $ is char(36).<\/p><pre class=\"codeinput\">ax.YTickLabel = ytl;\r\nylabel(<span class=\"string\">'Cost in &#8364;'<\/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\/prettierPlots_03.png\" alt=\"\"> <h4>Update the Y Tick Labels with Euros<a name=\"88e4aee8-950c-4168-b765-2b7a109593da\"><\/a><\/h4><p>Since we want to set all the labels at once, we want to return the results back into a cell array, hence the <tt>false<\/tt> setting for <tt>'UniformOutput'<\/tt>.<\/p><pre class=\"codeinput\">ax.YTickLabel = cellfun(@(x)sprintf(<span class=\"string\">'&#8364; %s'<\/span>,x),ax.YTickLabel,<span class=\"string\">'UniformOutput'<\/span>,false)\r\n<\/pre><pre class=\"codeoutput\">ax = \r\n  Axes (Income by Month) with properties:\r\n\r\n             XLim: [7.3559e+05 7.3594e+05]\r\n             YLim: [0 30]\r\n           XScale: 'linear'\r\n           YScale: 'linear'\r\n    GridLineStyle: '-'\r\n         Position: [0.13 0.11 0.775 0.815]\r\n            Units: 'normalized'\r\n\r\n  Use GET to show all properties\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/prettierPlots_04.png\" alt=\"\"> <h4>Finally Annotate a Plot with Some Math<a name=\"d87fc6c4-95f8-4247-a518-4d8f8863c934\"><\/a><\/h4><p>Just create the latex expression for the math, and call <tt>text<\/tt>, with the <tt>'interpreter'<\/tt> parameter set to <tt>'latex'<\/tt>.<\/p><pre class=\"codeinput\">plot(magic(3))\r\nt1 = text(1.25,5,<span class=\"string\">'$$\\frac{a}{b}$$'<\/span>);\r\nt1.FontSize = 18;\r\nt1.Interpreter = <span class=\"string\">'latex'<\/span>;\r\nt2 = text(2.2,8,<span class=\"string\">'$$\\sqrt{3}$$'<\/span>,<span class=\"string\">'FontSize'<\/span>,32,<span class=\"string\">'Interpreter'<\/span>,<span class=\"string\">'latex'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/prettierPlots_05.png\" alt=\"\"> <h4>Call to Action<a name=\"a45a6934-91a4-4c92-9d4a-6ab87eb68e85\"><\/a><\/h4><p>Do you annotate your plots frequently?  What do you need to show?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_5b4acc76de68433e8a9a62fceb40fb9c() {\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='5b4acc76de68433e8a9a62fceb40fb9c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5b4acc76de68433e8a9a62fceb40fb9c';\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_5b4acc76de68433e8a9a62fceb40fb9c()\"><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; R2015a<br><\/p><\/div><!--\r\n5b4acc76de68433e8a9a62fceb40fb9c ##### SOURCE BEGIN #####\r\n%% Advice for Making Prettier Plots\r\n% A few years ago,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871-jiro-doke\r\n% Jiro> wrote a\r\n% <https:\/\/blogs.mathworks.com\/loren\/2007\/12\/11\/making-pretty-graphs\/\r\n% popular post> for making pretty plots on this blog.  We also host a\r\n% <https:\/\/blogs.mathworks.com\/graphics blog specifically about graphics> by\r\n% Mike.  And with the R2014b release of MATLAB came an updated graphics\r\n% system that Dave described last year in a 3 part series:\r\n% <https:\/\/blogs.mathworks.com\/loren\/2014\/10\/03\/matlab-r2014b-graphics-part-1-features-of-the-new-graphics-system\/\r\n% Part 1>,\r\n% <https:\/\/blogs.mathworks.com\/loren\/2014\/10\/14\/matlab-r2014b-graphics-part-2-using-graphics-objects\/\r\n% Part 2>, and <https:\/\/blogs.mathworks.com\/loren\/2014\/11\/05\/matlab-r2014b-graphics-part-3-compatibility-considerations-in-the-new-graphics-system\/\r\n% Part 3>.\r\n%\r\n% Even with that, I continue to hear questions about how to accomplish\r\n% certain tasks, such as using a symbol to indicate degrees. This post\r\n% contains a collection of a few tips that may help you update your plots\r\n% to match more closely what you are trying to convey.\r\n%\r\n%\r\n%% Plotting Temperature Data\r\n% Suppose we have some temperature data to plot and want to add tick marks\r\n% that include the degree symbol.  Here's some ways to achieve this.\r\n%\r\n% Mock up data for the first of each month this year.\r\nt1 = datetime(2014,1:12,1);  \r\ntemp = [0 2 12 11 15 25 23 27 25 24 12 8];\r\n%% Add Y Label with Degree Symbol\r\nh = plot(t1,temp,':*');\r\nax = h.Parent;\r\ntitle('A Year of Temperatures on the 1st of the Month')\r\nylabel('Degrees Celcius ^{\\circ}')\r\n%% Find the Y Labels\r\nytl = ax.YTickLabel\r\n%% Append the Degree Symbol\r\nytld = strcat(ytl,'^{\\circ}')  \r\n%% Reset the Labels\r\nax.YTickLabel = ytld;\r\n%% Suppose the Y Axis is Money, Not Degrees\r\n% First replace the labels with the original values.  Then update the Y\r\n% axis label. If you want to use the \u00e2\u201a\u00ac symbol and it is not on your\r\n% keyboard, you can use |char(8364)|; $ is char(36).\r\nax.YTickLabel = ytl;\r\nylabel('Cost in \u00e2\u201a\u00ac')\r\ntitle('Income by Month')\r\n%% Update the Y Tick Labels with Euros\r\n% Since we want to set all the labels at once, we want to return the\r\n% results back into a cell array, hence the |false| setting for\r\n% |'UniformOutput'|.\r\nax.YTickLabel = cellfun(@(x)sprintf('\u00e2\u201a\u00ac %s',x),ax.YTickLabel,'UniformOutput',false)\r\n%% Finally Annotate a Plot with Some Math\r\n% Just create the latex expression for the math, and call |text|, with the\r\n% |'interpreter'| parameter set to |'latex'|.\r\nplot(magic(3))\r\nt1 = text(1.25,5,'$$\\frac{a}{b}$$');\r\nt1.FontSize = 18;\r\nt1.Interpreter = 'latex';\r\nt2 = text(2.2,8,'$$\\sqrt{3}$$','FontSize',32,'Interpreter','latex');\r\n%% Call to Action\r\n% Do you annotate your plots frequently?  What do you need to show?\r\n% Let us know <https:\/\/blogs.mathworks.com\/loren\/?p=XXXX#respond here>.\r\n##### SOURCE END ##### 5b4acc76de68433e8a9a62fceb40fb9c\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/prettierPlots_05.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>A few years ago, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871-jiro-doke\">Jiro<\/a> wrote a <a href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/11\/making-pretty-graphs\/\">popular post<\/a> for making pretty plots on this blog.  We also host a <a href=\"https:\/\/blogs.mathworks.com\/graphics\">blog specifically about graphics<\/a> by Mike.  And with the R2014b release of MATLAB came an updated graphics system that Dave described last year in a 3 part series: <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/03\/matlab-r2014b-graphics-part-1-features-of-the-new-graphics-system\/\">Part 1<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/10\/14\/matlab-r2014b-graphics-part-2-using-graphics-objects\/\">Part 2<\/a>, and <a href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/11\/05\/matlab-r2014b-graphics-part-3-compatibility-considerations-in-the-new-graphics-system\/\">Part 3<\/a>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/06\/11\/advice-for-making-prettier-plots\/\">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,1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1182"}],"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=1182"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1182\/revisions"}],"predecessor-version":[{"id":1971,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1182\/revisions\/1971"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}