{"id":24,"date":"2007-10-31T14:58:35","date_gmt":"2007-10-31T13:58:35","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=24"},"modified":"2010-11-12T15:47:43","modified_gmt":"2010-11-12T14:47:43","slug":"mathematica-version-of-colorbar","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=24","title":{"rendered":"Mathematica version of colorbar"},"content":{"rendered":"<p>Imagine that you have a function of the form z=f(x,y) and you want to plot it.  The result you have in mind is something like this:<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"\/images\/random\/colorbar.gif\" alt=\"\" \/><\/p>\n<p>The above plot was produced by <a href=\"http:\/\/www.mathworks.com\/\">Matlab<\/a> and the scale on the right hand side is produced via the colorbar function.  I was recently asked if it is possible to easily produce such a scale using <a href=\"http:\/\/www.wolfram.com\/\">Mathematica<\/a> and after a long search through the documentation the answer was no!  Just to make sure that I hadn&#8217;t missed something I made a post to <a href=\"http:\/\/groups.google.co.uk\/group\/comp.soft-sys.math.mathematica\/topics?lnk=srg\">comp.soft-sys.mathematica<\/a> to see what the Mathematica gurus there would make of the problem.<\/p>\n<p>Sure enough there is no simple alternative to colorbar in Mathematica but some helpful soul posted a piece of code that reproduced a similar effect.  With a bit of work this turned out to be sufficient for the needs of the person who originally approached me and everyone was happy.<\/p>\n<p>A few days later I discovered that someone called Will Robertson had been following the same thread on the newsgroup and he had used the information posted there to produce a package that allowed a Mathematica user to easily produce &#8216;colorbar plots&#8217; for both DensityPlots and ContourPlots.  I hacked his code so that it supported Plot3D as well and emailed it to him.  Between us we tidied up the code a little and submitted it to the <a href=\"http:\/\/library.wolfram.com\/infocenter\/MathSource\/7030\/\">wolfram library<\/a> for everyone to download and use for free.<\/p>\n<p>There is an examples file included in the package distribution that shows how to use it in detail but to give you an idea of what you are going to get here is how to plot the function<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/walkingrandomly.com\/wp-content\/ql-cache\/quicklatex.com-a3e652b4ca2d423ca15efbb22e634b96_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#108;&#105;&#103;&#104;&#116;&#32;&#122;&#61;&#101;&#94;&#123;&#45;&#120;&#94;&#50;&#45;&#121;&#94;&#50;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"90\" style=\"vertical-align: 0px;\"\/><\/p>\n<p>once you have downloaded and run the package.  The command<\/p>\n<p>ColorbarPlot[Exp[-(#1^2 + #2^2)] &amp;, {-2, 2}, {-2, 2}]<\/p>\n<p>produces the following plot<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"\/images\/mathematica\/gauss_densitybar.jpg\" alt=\"\" \/><\/p>\n<p>The above plot is a DensityPlot which is the default type but you can change it using the PlotType option:<\/p>\n<p>ColorbarPlot[Exp[-(#1^2 + #2^2)] &amp;, {-2, 2}, {-2, 2},PlotType-&gt;ContourPlot]<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"\/images\/mathematica\/gausscontour.jpg\" alt=\"\" \/><\/p>\n<p>Valid PlotTypes are DensityPlot, ContourPlot and Plot3D but this list will be expanded in future versions.  Finally, here is a plot that makes use of lots of options.<\/p>\n<p>f = Exp[-(#1^2 + #2^2)] &amp;<br \/>\nplot = ColorbarPlot[f, {-2, 2}, {-2, 2},<br \/>\nPlotType -&gt; Plot3D,<br \/>\nXLabel -&gt; &#8220;x&#8221;,<br \/>\nYLabel -&gt; &#8220;y&#8221;,<br \/>\nZLabel -&gt; &#8220;z&#8221;,<br \/>\nTitle -&gt; &#8220;Gaussian&#8221;,<br \/>\nCLabel -&gt; &#8220;Range&#8221;,<br \/>\nHeight -&gt; 300,<br \/>\nColors -&gt; &#8220;TemperatureMap&#8221;,<br \/>\nBoxed -&gt; False<br \/>\n]<\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"\/images\/mathematica\/gauss_3d_bar.jpg\" alt=\"\" \/><\/p>\n<p>The package is available for download from <a href=\"http:\/\/library.wolfram.com\/infocenter\/MathSource\/7030\/\">here<\/a>.  Comments are welcomed and we hope you like it.<\/p>\n<ul>\n<li><strong>UPDATE 12th Novermber 2010.<\/strong> Update to <a href=\"https:\/\/www.walkingrandomly.com\/?p=2960\">version 0.6<\/a><\/li>\n<li><strong>UPDATE 22nd October 2009.<\/strong> Update to <a href=\"..\/?p=1782\">version 0.5<\/a><\/li>\n<li><strong>UPDATE 5th June 2008. <\/strong>This package has now been updated to <a href=\"https:\/\/www.walkingrandomly.com\/?p=116\">version 0.4<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Imagine that you have a function of the form z=f(x,y) and you want to plot it. The result you have in mind is something like this: The above plot was produced by Matlab and the scale on the right hand side is produced via the colorbar function. I was recently asked if it is possible [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-math-software","category-mathematica"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-o","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":3,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":2248,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/2248"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}