{"id":2405,"date":"2025-05-16T08:38:11","date_gmt":"2025-05-16T12:38:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/?p=2405"},"modified":"2026-01-30T20:34:32","modified_gmt":"2026-01-31T01:34:32","slug":"labeling-bars-made-easy","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2025\/05\/16\/labeling-bars-made-easy\/","title":{"rendered":"Labeling Bars Made Easy"},"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\" class=\"alignleft size-medium wp-image-487\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/benjaminKraus.jpg\" alt=\"Benjamin Kraus\" \/><\/td>\r\n<td style=\"vertical-align: middle;padding: 3px\"><strong>Guest Writer: <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4517334\" target=\"_blank\" rel=\"noopener\">Benjamin Kraus<\/a>\r\n<\/strong>Ben has been a Software Engineer on the MATLAB Graphics and Charting Team since 2015. Although his first exposure to MATLAB was in 2000 as an undergraduate studying Biological Engineering and Computer Science at Cornell University, he really grew to love MATLAB while completing his PhD in Biomedical Engineering at Boston University. Throughout graduate school Ben used MATLAB nearly every day for nearly every aspect of his research: from running his experimental setup, collecting data, and analyzing data, to producing figures for his thesis and research papers. Ben joined MathWorks in 2013 and spent a little over a year in the Engineering Development Group at MathWorks before joining the Graphics and Charting Team. When he isn't at a computer, Ben enjoys biking, rowing, and sailing, and is a member of The Fast and the Fouriers running club at MathWorks.<\/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><a href=\"https:\/\/matlab.mathworks.com\/open\/github\/v1?repo=MATLAB-Graphics-and-App-Building\/matlab-gaab-blog-2025&amp;file=BarLabels\/labelingBars_R2024b.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><\/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\">Although the Charting Team loves adding completely new chart types to MATLAB (such as <a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2025\/01\/16\/violin-plot-a-tool-for-data-visualization\/\" target=\"_blank\" rel=\"noopener\">violin plot<\/a>), we try not to forget our old favorites, including the trusty bar plot. We've added two new features to bar plots in R2024a and R2024b that makes it just a bit easier to get that perfect bar plot: the ability to adjust the amount of whitespace between groups of bars and a way to easily add labels to your bar plots.<\/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\">To show off these new features, I'm going to build a bar plot of my biking, rowing, and running mileage since 2020. I spend a good chunk of my day at a computer, so I try to compensate for that by getting outside for a daily workout. Let's take an initial look at the data:<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\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\">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\">years = [<span style=\"color: #a709f5\">\"2020\"<\/span>, <span style=\"color: #a709f5\">\"2021\"<\/span>, <span style=\"color: #a709f5\">\"2022\"<\/span>, <span style=\"color: #a709f5\">\"2023\"<\/span>, <span style=\"color: #a709f5\">\"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\">run = [1359, 1200, 1027, 1019, 439];<\/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\">bike = [1128, 1679, 1268, 1043, 651];<\/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\">row = [68, 164, 253, 271, 548];<\/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\">b = bar(years, [run; bike; row]);<\/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\">legend([<span style=\"color: #a709f5\">\"Run\"<\/span>, <span style=\"color: #a709f5\">\"Bike\"<\/span>, <span style=\"color: #a709f5\">\"Row\"<\/span>])<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">title(<span style=\"color: #a709f5\">\"Annual Miles\"<\/span>)<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_0\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_1.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/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\">As you can see, like many people, I logged a lot of running and biking miles in 2020. Unfortunately, a recent injury has curtailed a lot of my running, but I've replaced it with more rowing (a relatively new sport for me).<\/div>\r\n<h2 style=\"margin: 3px 10px 5px 4px;padding: 0px;line-height: 25px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Add labels to a bar plot<\/h2>\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\"><span style=\"font-weight: bold\">One issue with the bar plot above is that it can be hard to read precise values.<\/span> You've got to visually trace from the top of each bar back to the y-axis and then interpolate to get an approximate value. A quick way to fix that is to add grid lines:<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">ax.YGrid=<span style=\"color: #a709f5\">'on'<\/span>;<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_1\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_2.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\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\">This helps a little, but you still need to interpolate to get an approximate value. Sometimes you want to show the actual value as a label above each bar. A new feature in R2024b makes this much easier. <span style=\"font-weight: bold\">Starting in R2024b, you can set the <\/span><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_ae2a0ff6-2ad0-4aa9-a9f5-1ff7fbb97907\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: bold;font-family: monospace\">Labels<\/span><\/a><span style=\"font-weight: bold\"> property to add labels automatically aligned with the top of each bar.<\/span> Each set of data (run, bike, and row) corresponds to a different <span style=\"font-family: monospace\">Bar<\/span> object, so we need to set the <span style=\"font-family: monospace\">Labels<\/span> property individually for each set of data. Let's see what that looks like:<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\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\">b(1).Labels = run;<\/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\">b(2).Labels = bike;<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">b(3).Labels = row;<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_2\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_3.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/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\">The astute observer will note that I set those properties using the original data in the form of numeric vectors. When you do that, MATLAB will automatically convert those values into strings. However, if you prefer, you can set the labels to include whatever strings you want.<\/div>\r\n<h2 style=\"margin: 20px 10px 5px 4px;padding: 0px;line-height: 25px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Control bar and group width in a bar plot<\/h2>\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\">With the new labels you can now tell exactly how many miles I logged each year, but the chart is getting a bit cluttered. Let's see what we can do to clean that up.<\/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\">To start with, I'm going to eliminate some white space by making each individual bar and each group of bars a bit wider using two properties: <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#buduai1-1_sep_shared-BarWidth\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">BarWidth<\/span><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_b17c63f5-f1a7-49e0-bb5c-abe34a9409bc\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">GroupWidth<\/span><\/a>. The <span style=\"font-family: monospace\">BarWidth<\/span> property has been available in MATLAB for many years. This property is a ratio that controls the width of each individual bar, with a value of 0 meaning the bars should have no width and a value of 1 meaning the bars should touch each other (occupy 100% of the space available). The <span style=\"font-family: monospace\">GroupWidth<\/span> property was added in MATLAB R2024a and controls the width of each group of bars. Similar to <span style=\"font-family: monospace\">BarWidth<\/span>, <span style=\"font-family: monospace\">GroupWidth<\/span> is a ratio: a value of 0 for means the group should have no width and a value of 1 means each group should be touching the neighboring group.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(b,BarWidth=0.9,GroupWidth=0.9)<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_3\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_4.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\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\">That helped a lot, but a few labels are still overlapping. Let's decrease the font size to make more room.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">fontsize(b,8,<span style=\"color: #a709f5\">\"points\"<\/span>)<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_4\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_5.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\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\">Now the labels no longer overlap, but the chart is still a little cluttered. The y-axis and grid lines are not as useful once you have labels, so I'm going to turn those off.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">axis(<span style=\"color: #a709f5\">'off'<\/span>)<\/div>\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">ax.XAxis.Visible = true;<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_5\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_6.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper\">\r\n<div style=\"border-radius: 0px 0px 4px 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\"><\/div>\r\n<\/div>\r\n<\/div>\r\n<h2 style=\"margin: 3px 10px 5px 4px;padding: 0px;line-height: 25px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Label location and color<\/h2>\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\">That looks pretty good, but there are two more features of the bar labels I want to show off, which is automatic color selection and label location. To demonstrate these features, I'm going to make the bar labels a little smaller and then move them into the bars by setting the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_90a43205-46d7-47cf-b612-b672d9a030ad\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">LabelLocation<\/span><\/a> property.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\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\">set(b,LabelLocation=<span style=\"color: #a709f5\">'end-inside'<\/span>)<\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">set(b,FontSize=7)<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_6\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_7.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/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\">Notice how the labels on the yellow bars stay black (because they are against a light background), but the other labels change to white (to contrast against the blue and red bars). If you would prefer to pick the label color yourself, you can set the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_7dca3c66-bbc3-4ba3-92a4-bff2af2fc00c\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">LabelColor<\/span><\/a> property (using a single color for all the bars or one color per bar).<\/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\">Other properties you can use to customize your labels include: <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#buduai1-1_sep_shared-FontName\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">FontName<\/span><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_163486b8-8761-4a68-94b6-a4ad9f6b0fa4\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">FontSize<\/span><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#buduai1-1_sep_shared-FontWeight\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">FontWeight<\/span><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#buduai1-1_sep_shared-FontAngle\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">FontAngle<\/span><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#buduai1-1_sep_shared-Interpreter\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">Interpreter<\/span><\/a><span style=\"font-family: monospace\">.<\/span><\/div>\r\n<h2 style=\"margin: 3px 10px 5px 4px;padding: 0px;line-height: 25px;min-height: 0px;font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif;font-style: normal;font-size: 20px;font-weight: bold;text-align: left\">Want more flexibility with your labels?<\/h2>\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\">What if you need just a bit more flexibility with your label placement, or you are stuck in an older release and can't leverage this new feature?<\/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\">Although it requires a bit more fiddling, you can do something similar using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_3d0c5b61-b082-4c79-b9c6-a21152ae26ff\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">XEndPoints<\/span><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.graphics.chart.primitive.bar-properties.html#mw_31111e15-f43e-4ad5-bbab-e1e919ee5765\" target=\"_blank\" rel=\"noopener\"><span style=\"font-family: monospace\">YEndPoints<\/span><\/a> properties, which were added in MATLAB R2019b. These properties give you the exact x- and y-coordinates of the tips of the bars, which you can pass into the <span style=\"font-family: monospace\">text<\/span> command to create labels at each bar. This approach requires a little more patience to get correct (such as setting the <span style=\"font-family: monospace\">HorizontalAlignment<\/span> and <span style=\"font-family: monospace\">VerticalAlignment<\/span>) but it produces text objects that you can position anywhere you like, giving you complete flexibility over the label properties.<\/div>\r\n<div style=\"background-color: #f5f5f5;margin: 10px 15px 10px 0;min-width: 100%\">\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\">b = bar(years, [run; bike; row],BarWidth=0.9,GroupWidth=0.9);<\/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\">legend([<span style=\"color: #a709f5\">\"Run\"<\/span>, <span style=\"color: #a709f5\">\"Bike\"<\/span>, <span style=\"color: #a709f5\">\"Row\"<\/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\">title(<span style=\"color: #a709f5\">\"Annual Miles\"<\/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\">x = [b.XEndPoints]; <span style=\"color: #008013\">% Combine all the x-coordinates into one matrix.<\/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\">y = [b.YEndPoints]; <span style=\"color: #008013\">% Combine all the y-coordinates into one matrix.<\/span><\/div>\r\n<\/div>\r\n<div class=\"inlineWrapper outputs\">\r\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, Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px\">text(x,y,string(y),HorizontalAlignment=<span style=\"color: #a709f5\">'center'<\/span>,VerticalAlignment=<span style=\"color: #a709f5\">'bottom'<\/span>,FontSize=8)<\/div>\r\n<div style=\"padding: 10px 0px 6px 17px;font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;font-size: 14px;line-height: 17.234px\">\r\n<div class=\"inlineElement eoOutputWrapper disableDefaultGestureHandling embeddedOutputsFigure\" data-testid=\"output_7\">\r\n<div class=\"outputLayer selectedOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer activeOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer scrollableOutputDecorationLayer doNotExport\" aria-hidden=\"true\"><\/div>\r\n<div class=\"outputLayer navigationFocusLayer doNotExport\" role=\"application\" aria-hidden=\"false\"><\/div>\r\n<div class=\"figureElement eoOutputContent\" role=\"article\"><img decoding=\"async\" class=\"figureImage figureContainingNode\" style=\"width: 560px\" src=\"http:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_8.png\" \/><\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/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\">Let me know in the comments what other bar labeling features you'd like to see.<\/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\"><\/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\"><\/div>\r\n&nbsp;\r\n\r\n<\/div>\r\n<script type=\"text\/javascript\">\r\n{ let css = '.eoOutputWrapper { width: calc(90vw - 10px) !important; }';\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><a href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx\"><button class=\"btn btn-sm btn_color_blue pull-right add_margin_10\">Download Live Script<\/button><\/a>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/files\/2025\/05\/labelingBars_R2024b.mlx-05-15-25_7.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: Benjamin Kraus\r\nBen has been a Software Engineer on the MATLAB Graphics and Charting Team since 2015. Although his first exposure to MATLAB was in 2000 as an undergraduate... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/graphics-and-apps\/2025\/05\/16\/labeling-bars-made-easy\/\">read more >><\/a><\/p>","protected":false},"author":198,"featured_media":2396,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,11,20,1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/2405"}],"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=2405"}],"version-history":[{"count":10,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/2405\/revisions"}],"predecessor-version":[{"id":3649,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/posts\/2405\/revisions\/3649"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media\/2396"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/media?parent=2405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/categories?post=2405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/graphics-and-apps\/wp-json\/wp\/v2\/tags?post=2405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}