{"id":987,"date":"2024-04-16T07:30:12","date_gmt":"2024-04-16T11:30:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/?p=987"},"modified":"2026-01-30T11:51:37","modified_gmt":"2026-01-30T16:51:37","slug":"introducing-transparency-to-legends-and-rectangles","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2024\/04\/16\/introducing-transparency-to-legends-and-rectangles\/","title":{"rendered":"Introducing Transparency to Legends and Rectangles"},"content":{"rendered":"<table style=\"background-color: #e2f0ff\">\n<tbody>\n<tr>\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>\n<td style=\"vertical-align: middle;padding: 3px\"><strong>Guest Writer: <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8450560\">Afiq Azaibi<\/a><br \/>\n<\/strong><\/p>\n<div class=\"content\">\n<div class=\"SectionBlock first active\">\n<div class=\"SD57BF6401\">Today's R2024a feature highlight is written by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8450560\">Afiq Azaibi<\/a>, a developer on the Charting Team and a returning contributor to the Graphics and App Building blog. See Afiq's bio on our <a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/contributors\/\">contributors page<\/a>.<\/div>\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<div class=\"rtcContent\">\n<div>\n<p><a href=\"https:\/\/matlab.mathworks.com\/open\/github\/v1?repo=MATLAB-Graphics-and-App-Building\/matlab-gaab-blog-2024&amp;file=LegendAndRectangleAlpha\/LegendTransparency.mlx\" target=\"_blank\" rel=\"nofollow noopener\"><img decoding=\"async\" class=\"\" src=\"https:\/\/www.mathworks.com\/images\/responsive\/global\/open-in-matlab-online.svg\" alt=\"Open in MATLAB Online\" \/><\/a><\/p>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Sometimes a legend or rectangle can occlude data behind it. You can make both objects fully transparent by setting the legend's <span style=\"font-family: monospace\">Box<\/span> property to <span style=\"font-family: monospace\">'off'<\/span> or setting the rectangle's <span style=\"font-family: monospace\">FaceColor<\/span> property to <span style=\"font-family: monospace\">'none'<\/span> respectively but this could be undesirable in some situations. Starting in R2024a, we introduced features to control the transparency of both legends and rectangles. Let's take a look at an example using weather data around the MathWorks HQ located in Natick, MA from <a href=\"https:\/\/ncei.noaa.gov\/\">ncei.noaa.gov<\/a> (<a href=\"https:\/\/github.com\/MATLAB-Graphics-and-App-Building\/matlab-gaab-blog-2024\/raw\/main\/LegendAndRectangleAlpha\/natickWeatherTbl.mat\">download link<\/a>).<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\n<div class=\"inlineWrapper\">\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;font-size: 14px\">load <span style=\"color: #a709f5\">natickWeatherTbl.mat<\/span><\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">head(natickWeatherTbl)<\/div>\n<\/div>\n<div><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-993 alignnone\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/weatherTable.png\" alt=\"\" width=\"616\" height=\"200\" \/><\/div>\n<\/div>\n<div><\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\n<div class=\"inlineWrapper\">\n<div style=\"border-radius: 0px;padding: 6px 45px 0px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">s = scatter(natickWeatherTbl,<span style=\"color: #a709f5\">\"Date\"<\/span>, [<span style=\"color: #a709f5\">\"Min Daily Temperature\" \"Max Daily Temperature\" <\/span>],<span style=\"color: #a709f5\">'filled'<\/span>, <span style=\"color: #a709f5\">'MarkerFaceAlpha'<\/span>,.2);<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\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;font-size: 14px\">axis <span style=\"color: #a709f5\">tight<\/span>;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\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;font-size: 14px\">leg = legend;<\/div>\n<\/div>\n<div class=\"inlineWrapper\">\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;font-size: 14px\">title(<span style=\"color: #a709f5\">\"Daily Temperature in Natick\"<\/span>);<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">ylabel(<span style=\"color: #a709f5\">\"Degrees (F)\"<\/span>);<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_1\">\n<div class=\"figureElement eoOutputContent\" role=\"article\">\n<p><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/LegendTransparency.mlx-04-15-24_1.png\" \/><\/p>\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\" aria-label=\"figure output \"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">The legend is obscuring part of the scatter charts. Starting in R2024a, we can update the <span style=\"font-family: monospace\">BackgroundAlpha<\/span> property which makes the legend semi-transparent. Now we can see data points behind the legend. If you find yourself in a situation where you can't set the legend's location to be outside and all the locations inside would obscure data, consider leveraging the legend's <span style=\"font-family: monospace\">BackgroundAlpha<\/span> property.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">leg.BackgroundAlpha = .6;<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_2\">\n<div class=\"figureElement eoOutputContent\" role=\"article\">\n<p><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/LegendTransparency.mlx-04-15-24_2.png\" \/><\/p>\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\" aria-label=\"figure output \"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Additionally, if we wanted to highlight the most number of days below 0 during the winter of 2011, we could add a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rectangle.html\">rectangle<\/a> in this region. The filled rectangle unfortunately obscures the data.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\n<div class=\"inlineWrapper\">\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;font-size: 14px\">x = days(natickWeatherTbl.Date(335)-natickWeatherTbl.Date(1));<\/div>\n<\/div>\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 0px;padding: 0px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rect = rectangle(<span style=\"color: #a709f5\">'Position'<\/span>,[x,-5.5,100,10], <span style=\"color: #a709f5\">'EdgeColor'<\/span>, <span style=\"color: #a709f5\">'none'<\/span>, <span style=\"color: #a709f5\">'FaceColor'<\/span>, <span style=\"color: #a709f5\">'c'<\/span>);<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_3\">\n<div class=\"figureElement eoOutputContent\" role=\"article\">\n<p><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/LegendTransparency.mlx-04-15-24_3.png\" \/><\/p>\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\" aria-label=\"figure output \"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Starting in R2024a, rectangle has a <span style=\"font-family: monospace\">FaceAlpha<\/span> property which can be used to change the rectangle's transparency.<\/div>\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0\">\n<div class=\"inlineWrapper outputs\">\n<div style=\"border-radius: 4px 4px 0px 0px;padding: 6px 45px 4px 13px;line-height: 18.004px;min-height: 0px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">rect.FaceAlpha = .4;<\/div>\n<\/div>\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_4\">\n<div class=\"figureElement eoOutputContent\" role=\"article\">\n<p><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/LegendTransparency.mlx-04-15-24_4.png\" \/><\/p>\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\" aria-label=\"figure output \"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div style=\"margin: 2px 10px 9px 4px;padding: 0px;line-height: 21px;min-height: 0px;font-family: Helvetica, Arial, sans-serif;font-style: normal;font-size: 14px;font-weight: 400;text-align: left\">Previously the only workaround for this would be to update the child order of the axes such that the scatter markers render on top of the rectangle. Now that rectangle has <span style=\"font-family: monospace\">FaceAlpha<\/span>, you can choose between the two options depending on whether you want the rectangle to overlay the data or not.<\/div>\n<p><a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2024\/04\/03\/r2024a-release-whats-new-in-graphics-and-app-building\/\">Read more about the R2024a graphics and app building features<\/a>.<\/p>\n<\/div>\n<p><script type=\"text\/javascript\">\n{ let css = '.eoOutputWrapper { width: calc(90vw - 10px) !important; } \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsErrorElement {    min-height: 18px;    max-height: 550px;} .embeddedOutputsErrorElement .diagnosticMessage-errorType {    overflow: auto;} .embeddedOutputsErrorElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} .embeddedOutputsErrorElement.activeOutput .eoOutputContent button {    user-select: none;    -webkit-user-select: none;} .embeddedOutputsErrorElement .eoOutputContent ::selection {} .embeddedOutputsErrorElement.inlineElement {} .embeddedOutputsErrorElement.rightPaneElement {} \/* Styling that is common to warnings and errors is in diagnosticOutput.css *\/.embeddedOutputsWarningElement {    min-height: 18px;    max-height: 550px;} .embeddedOutputsWarningElement .diagnosticMessage-warningType {    overflow: auto;} .embeddedOutputsWarningElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} .embeddedOutputsWarningElement .eoOutputContent ::selection {} .embeddedOutputsWarningElement.inlineElement {} .embeddedOutputsWarningElement.rightPaneElement {} \/* Copyright 2015-2023 The MathWorks, Inc. *\/\/* In this file, styles are not scoped to rtcContainer since they could be in the Dojo Tooltip *\/.diagnosticMessage-wrapper {    font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace;    font-size: 12px;} .diagnosticMessage-wrapper.diagnosticMessage-warningType {    \/*This fallback value will be used for appdesigner warnings*\/    color: var(--rtc-warning-output-color, var(--mw-color-matlabWarning));} .diagnosticMessage-wrapper.diagnosticMessage-warningType a {    \/*This fallback value will be used for appdesigner warnings*\/    color: var(--rtc-warning-output-color, var(--mw-color-matlabWarning));    text-decoration: underline;} .rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-warningType,.rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-warningType a {    color: var(--mw-color-matlabWarning) !important;} .diagnosticMessage-wrapper.diagnosticMessage-errorType {    \/*This fallback value will be used in appdesigner error tooltip text*\/    color: var(--rtc-error-output-color, var(--mw-color-matlabErrors));} .diagnosticMessage-wrapper.diagnosticMessage-errorType a {    \/*This fallback value will be used in appdesigner error tooltip text*\/    color: var(--rtc-error-output-color, var(--mw-color-matlabErrors));    text-decoration: underline;} .rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-errorType,.rtcThemeDefaultOverride .diagnosticMessage-wrapper.diagnosticMessage-errorType a {    color: var(--mw-color-matlabErrors) !important;} .diagnosticMessage-wrapper .diagnosticMessage-messagePart,.diagnosticMessage-wrapper .diagnosticMessage-causePart {    white-space: pre-wrap;} .diagnosticMessage-wrapper .diagnosticMessage-stackPart {    white-space: pre;} .embeddedOutputsTextElement,.embeddedOutputsVariableStringElement {    white-space: pre;    word-wrap:  initial;    min-height: 18px;    max-height: 550px;} .embeddedOutputsTextElement .textElement,.embeddedOutputsVariableStringElement .textElement {    overflow: auto;} .embeddedOutputsTextElement.activeOutput .eoOutputContent,.embeddedOutputsVariableStringElement.activeOutput .eoOutputContent {    user-select: text;    -webkit-user-select: text;} \/*embeddedOutputsTextElement has a different dom structure than embeddedOutputsVariableStringElement.Unlike variableString, the text output has both TEXT nodes and elements as children. Hence we needa selector for each.*\/.embeddedOutputsTextElement .eoOutputContent::selection,.embeddedOutputsTextElement .eoOutputContent ::selection,.embeddedOutputsVariableStringElement .eoOutputContent ::selection {} .textElement,.rtcDataTipElement .textElement {    padding-top: 2px;} .embeddedOutputsTextElement.inlineElement,.embeddedOutputsVariableStringElement.inlineElement {} .inlineElement .textElement {} .embeddedOutputsTextElement.rightPaneElement,.embeddedOutputsVariableStringElement.rightPaneElement {    min-height: 16px;} .rightPaneElement .textElement {    padding-top: 2px;    padding-left: 9px;}';\nlet head = document.head || document.getElementsByTagName('head')[0];\nlet style = document.createElement('style');\nhead.appendChild(style);\nstyle.type = 'text\/css';\nif (style.styleSheet) {\n    style.styleSheet.cssText = css;\n} else {\n    style.appendChild(document.createTextNode(css));\n}<\/p>\n<p>}<\/script><a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/LegendTransparency.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2024\/04\/transparentLegendAndRectangles.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Guest Writer: Afiq Azaibi<\/p>\n<p>Today's R2024a feature highlight is written by Afiq Azaibi, a developer on the Charting Team and a returning contributor to the Graphics and App Building blog. See... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2024\/04\/16\/introducing-transparency-to-legends-and-rectangles\/\">read more >><\/a><\/p>\n","protected":false},"author":198,"featured_media":1017,"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\/987"}],"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=987"}],"version-history":[{"count":14,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":3640,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/987\/revisions\/3640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media\/1017"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}