bar — Create bar charts for categorical comparisons, grouped series, stacked values, and MATLAB bar workflows.
bar creates vertical bar charts from vectors or matrix-style inputs. In RunMat it returns a bar handle, supports the common MATLAB bar(y) workflows as well as grouped and stacked behaviors, and integrates with the shared handle/property system.
How bar works
- Vector inputs create a single bar series with implicit category positions.
- Matrix inputs support grouped and stacked bar-style rendering workflows.
- The returned value is a bar handle that can be queried and updated through
getandset. - Display names, bar width, and face color participate in the shared plotting object/property model.
- RunMat prefers GPU-backed rendering when supported input/layout combinations are present, while preserving the same bar semantics on fallback paths.
How RunMat runs bar on the GPU
Bar rendering shares the same plotting architecture as the rest of the plotting stack.
GPU memory and residency
bar preserves GPU residency where the bar geometry pipeline can consume exported buffers directly. Fallback rendering gathers once and preserves the same grouping/stacking semantics.
Examples
Create a basic bar chart from a vector
values = [3 5 2 9];
bar(values);Create grouped bars from a matrix
Y = [3 5 2; 4 6 1; 5 4 3];
bar(Y);Style a bar object and label it for the legend
h = bar([2 4 1 5]);
set(h, 'FaceColor', 'g', 'DisplayName', 'counts');
legend;Grouped category comparison
categories = {'Q1', 'Q2', 'Q3', 'Q4'};
revenue = [42 51 63 58; 38 45 52 61; 29 34 48 55];
bar(revenue');
title('Quarterly Revenue by Region');
xlabel('Quarter');
ylabel('Revenue ($M)');
legend('North', 'South', 'West');
grid on;
FAQ
How do I create grouped vs stacked bar charts?⌄
Pass a matrix to bar. By default, each row becomes a group with side-by-side bars (one per column). To stack them instead, pass 'stacked' as the second argument.
Y = [3 5 2; 4 6 1; 5 4 3];
bar(Y); % grouped (default)
bar(Y, 'stacked'); % stackedCan I make horizontal bar charts?⌄
Use barh instead of bar. It takes the same inputs but draws bars horizontally. The y-axis becomes the category axis and the x-axis shows values.
barh([3 5 2 9]);How do I set custom category labels on the x-axis?⌄
After calling bar, use set(gca, 'XTickLabel', ...) to replace the default numeric labels with strings.
bar([4 7 2]);
set(gca, 'XTickLabel', {'Q1', 'Q2', 'Q3'});The label count should match the number of bar groups.
Related Plotting functions
More plotting resources
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how bar works, line by line, in Rust.
- View bar.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.