Floating bar graphs with characters and numerical data
13 views (last 30 days)
Show older comments
Hello experts,
I am looking to get a plot of floating bar graphs in horizontal orientation. The Y-axis has got different chemicall elements and the X-axis has got the range of these chemical elements. I am attaching a sample figure, I am looking for. Kindly someone help me in how can I plot such a figure with one axis (Y-axis) being character names of the chemical elements and on X-axis their range with the horizontally oriented bars showing their range.
I had also written the ranges of all the chemical elements for which I need the plot.
Thank you
Data:
Al: min = 0.01, max = 0.58
Ti: min = 0.76, max = 1.95
Nb: min = 5.0, max = 5.61
Co: min = 0, max = 1.0
Mo: min = 2.63, max = 3.11
1 Comment
Rik
on 5 Sep 2021
This should be relatively easy to achieve with calls to patch. I don't think there is a native function to do exactly this. What have you tried? Have you searched the file exchange?
Accepted Answer
Ive J
on 5 Sep 2021
Edited: Ive J
on 5 Sep 2021
What about this?
tags = ["Al", "Ti", "Nb", "Co", "Mo"];
ranges = [0.01 0.58; 0.76 1.95; 5 5.61; 0 1; 2.63 3.11];
h = gca;
h.YLim = [0, numel(tags) + 1];
ranges(:, 3) = ranges(:, 2) - ranges(:, 1); % width of bars
hght = ones(size(ranges, 1), 1).*0.5; % height of each bar: 0.5
y = (1:size(ranges, 1)).' - 0.25; % Y position of each bar
for i = 1:numel(y) % loop over each element and plot
rectangle(h, 'Position', [ranges(i, 1), y(i), ranges(i, 3), hght(i)], 'FaceColor', '#153944');
end
h.YTick = 1:numel(tags);
h.YTickLabel = tags;
h.Box = 'on';
% add some aesthetics
h.LineWidth = 1.2;
h.FontSize = 12;
h.XLabel.String = "Range";
axis square
5 Comments
Voss
on 17 Jun 2023
Maybe something like this, creating lines behind the rectangles:
err = [0.4 0.5 0.3 0.4 0.2];
tags = ["Al", "Ti", "Nb", "Co", "Mo"];
ranges = [0.01 0.58; 0.76 1.95; 5 5.61; 0 1; 2.63 3.11];
h = gca;
h.XLim = [0, numel(tags) + 1];
ranges(:, 3) = ranges(:, 2) - ranges(:, 1); % height of bars
wdth = ones(size(ranges, 1), 1).*0.5; % width of each bar: 0.5
x = (1:size(ranges, 1)).' - 0.25; % X position of each bar
for i = 1:numel(x) % loop over each element and plot
line(h, ...
x(i)+0.25+wdth(i)*[-1 1 0 0 -1 1]/4, ...
[ranges(i,2)+err(i)*[1 1 1] ranges(i,1)+err(i)*[-1 -1 -1]], ...
'Color','r');
rectangle(h, 'Position', [x(i), ranges(i, 1), wdth(i), ranges(i, 3)], 'FaceColor', '#153944');
end
h.XTick = 1:numel(tags);
h.XTickLabel = tags;
h.Box = 'on';
% add some aesthetics
h.LineWidth = 1.2;
h.FontSize = 12;
h.YLabel.String = "Range";
axis square
You may also consider using boxplot or boxchart, either of which may be easier to use for what you ultimately want to do:
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!