{"id":1967,"date":"2024-10-16T09:39:07","date_gmt":"2024-10-16T13:39:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/?p=1967"},"modified":"2026-01-30T11:59:25","modified_gmt":"2026-01-30T16:59:25","slug":"narrow-down-your-legends-with-iconcolumnwidth","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2024\/10\/16\/narrow-down-your-legends-with-iconcolumnwidth\/","title":{"rendered":"Narrow down your legends with IconColumnWidth"},"content":{"rendered":"<table style=\"background-color: #e2f0ff\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 120px;padding: 3px;vertical-align: middle\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft wp-image-634 size-medium\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2023\/12\/AfiqPortraitZoom-300x248.png\" alt=\"\" width=\"300\" height=\"248\" \/><\/td>\r\n<td style=\"vertical-align: middle;padding: 3px\"><strong>Guest Writer: <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8450560\">Afiq Azaibi<\/a>\r\n<\/strong>\r\n<div class=\"content\">\r\n<div class=\"SectionBlock first active\">\r\n<div class=\"SD57BF6401\"><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8450560\">Afiq Azaibi<\/a> is back to tell us about a new feature in R2024b that controls the width of a legend. In this article, you'll learn about the new <span style=\"font-weight: bold\">IconColumnWidth<\/span> legend property. Hearing directly from the developers of MATLAB graphics and app features is what this blog is all about! See Afiq's bio on our <a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/contributors\/\">contributors page<\/a>.<\/div>\r\n<\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div class=\"rtcContent\">\r\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">\r\n\r\n<hr \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<a href=\"https:\/\/matlab.mathworks.com\/open\/github\/v1?repo=MATLAB-Graphics-and-App-Building\/matlab-gaab-blog-2024&amp;file=IconColumnWidth\/R2024b_IconColumnWidth.mlx\" target=\"_blank\" rel=\"nofollow noopener\">\r\n<img decoding=\"async\" class=\"\" src=\"https:\/\/www.mathworks.com\/images\/responsive\/global\/open-in-matlab-online.svg\" alt=\"Open in MATLAB Online\" \/><\/a>\r\n\r\n\r\n<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Legend and I were on a diet for 24b. While in the office, I was busy developing a way for users to make the legend narrower. While outside the office, I was busy trying to make myself narrower for my wedding. Let's take a look at how those two projects intersected.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">datasource = <span style=\"color: #a709f5\">\"https:\/\/raw.githubusercontent.com\/MATLAB-Graphics-and-App-Building\/matlab-gaab-blog-2024\/refs\/heads\/main\/IconColumnWidth\/weight.csv\"<\/span>;<\/div>\r\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">tbl = readtable(datasource);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">tbl = renamevars(tbl, [<span style=\"color: #a709f5\">\"Var1\" \"Var2\"<\/span>], [<span style=\"color: #a709f5\">\"Date\"<\/span>, <span style=\"color: #a709f5\">\"Weight\"<\/span>]);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">ax = axes;<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">scatter(ax, tbl, <span style=\"color: #a709f5\">'Date'<\/span>, <span style=\"color: #a709f5\">'Weight'<\/span>, <span style=\"color: #a709f5\">'DisplayName'<\/span>, <span style=\"color: #a709f5\">'My Weight'<\/span>);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">ax.YLabel.String = <span style=\"color: #a709f5\">\"Weight in Pounds\"<\/span>;<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">l = legend;<\/div>\r\n<\/div>\r\n<\/div>\r\n<div><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"421\" class=\"size-full wp-image-1976 alignnone\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/10\/IconColumnWidth_1.png\" alt=\"\" \/><\/div>\r\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Each point in the scatter denotes a day that I weighed myself. The legend automatically clamps to a smaller size when there are only marker icons. You can see that there's a gap of time I didn't have access to a scale for 2 weeks. Let's highlight that gap of time with a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/xregion.html\"><span style=\"font-weight: bold;font-family: monospace\">ConstantRegion<\/span><\/a> and while we're at it, also mark the wedding day with a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/xline.html\"><span style=\"font-weight: bold;font-family: monospace\">ConstantLine<\/span><\/a>.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">bachelorStart = datetime(<span style=\"color: #a709f5\">'21-June-2024'<\/span>);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">bachelorEnd = datetime(<span style=\"color: #a709f5\">'6-July-2024'<\/span>);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px;padding: 0px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">bachelorParty = xregion(bachelorStart, bachelorEnd, <span style=\"color: #a709f5\">'DisplayName'<\/span>, <span style=\"color: #a709f5\">'Bachelor Party'<\/span>);<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px 0px 4px 4px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">weddingDay = xline(datetime(<span style=\"color: #a709f5\">'10-August-2024'<\/span>), <span style=\"color: #a709f5\">'r'<\/span>, <span style=\"color: #a709f5\">'DisplayName'<\/span>, <span style=\"color: #a709f5\">'Wedding Day'<\/span>);<\/div>\r\n<\/div>\r\n<\/div>\r\n<div><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"421\" class=\"size-full wp-image-1979 alignnone\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/10\/IconColumnWidth_2.png\" alt=\"\" \/><\/div>\r\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">If you look at the legend, you'll notice that after we added the <span style=\"font-family: monospace\">ConstantRegion<\/span> and <span style=\"font-family: monospace\">ConstantLine<\/span>, there's a lot of padding surrounding the scatter icon. This happened because the icons in the legend are not exclusively markers. However the padding can be adjusted with the new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.illustration.legend-properties.html#mw_a18598dd-6da8-49a6-93a3-3304acb675ec\"><span style=\"font-weight: bold;font-family: monospace\">IconColumnWidth<\/span><\/a> property on legend. This property sets the width of the icon in points. If the legend does not automatically set this property the way you'd like, you can always override it.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 4px;padding: 6px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">l.IconColumnWidth = 15;<\/div>\r\n<\/div>\r\n<\/div>\r\n<div><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"421\" class=\"size-full wp-image-1982 alignnone\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/10\/IconColumnWidth_3.png\" alt=\"\" \/><\/div>\r\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">To make the ConstantRegion icon square, you can set the IconColumnWidth to the legend's FontSize property<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 4px;padding: 6px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">l.IconColumnWidth = l.FontSize;<\/div>\r\n<\/div>\r\n<\/div>\r\n<div><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"421\" class=\"size-full wp-image-1985 alignnone\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/10\/IconColumnWidth_4.png\" alt=\"\" \/><\/div>\r\n<div style=\"margin: 10px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">To recap, we've introduced <span style=\"font-family: monospace\">IconColumnWidth<\/span> and a new behavior for legend in 24b. The new behavior will automatically update the default value of <span style=\"font-family: monospace\">IconColumnWidth<\/span> when all the icons in the legend are markers. We made this decision to keep the heuristic relatively simple. Additionally, we did not want to change the default value in all cases since icons with non-solid lines get clipped such that it can become difficult to tell the difference between a non-solid line and solid line if <span style=\"font-family: monospace\">IconColumnWidth<\/span> is too small.<\/div>\r\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Do you have a use case you're excited to apply this new feature to? Do you have ideas on how to improve its functionality? Please share them in the comments so we can continuously improve its design.<\/div>\r\n&nbsp;\r\n\r\n<\/div>\r\n<script type=\"text\/javascript\">\r\n{ let css = '';\r\nlet head = document.head || document.getElementsByTagName('head')[0];\r\nlet style = document.createElement('style');\r\nhead.appendChild(style);\r\nstyle.type = 'text\/css';\r\nif (style.styleSheet) {\r\n    style.styleSheet.cssText = css;\r\n} else {\r\n    style.appendChild(document.createTextNode(css));\r\n}\r\n\r\n\r\n}<\/script>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/10\/IconColumnWidth_cover.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\n\r\n\r\n\r\nGuest Writer: Afiq Azaibi\r\n\r\n\r\n\r\nAfiq Azaibi is back to tell us about a new feature in R2024b that controls the width of a legend. In this article, you'll learn about the new IconColumnWidth... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2024\/10\/16\/narrow-down-your-legends-with-iconcolumnwidth\/\">read more >><\/a><\/p>","protected":false},"author":198,"featured_media":1988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,11,2,20],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/1967"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/users\/198"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/comments?post=1967"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/1967\/revisions"}],"predecessor-version":[{"id":3645,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/1967\/revisions\/3645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media\/1988"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media?parent=1967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/categories?post=1967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/tags?post=1967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}