Floating bar graphs with characters and numerical data

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 件のコメント

Rik
Rik 2021 年 9 月 5 日
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?

サインインしてコメントする。

 採用された回答

Ive J
Ive J 2021 年 9 月 5 日
編集済み: Ive J 2021 年 9 月 5 日

0 投票

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 件のコメント

Onkar Khadke
Onkar Khadke 2021 年 9 月 6 日
Dear Ive J sir,
Your code works perfectly and has helped me, exactly for what I was looking for.
A big big Thank you to you sir.
Your help is highly appreciated sir
Thank you once again
Regards
Onkar Khadke
Madeleine Liblong
Madeleine Liblong 2023 年 6 月 16 日
How do the code such that the range is plotted on the vertical y-axis?
Voss
Voss 2023 年 6 月 16 日
編集済み: Voss 2023 年 6 月 16 日
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
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
Madeleine Liblong
Madeleine Liblong 2023 年 6 月 16 日
thank you!! any idea on how to add error bars to the data?
Voss
Voss 2023 年 6 月 17 日
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:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

質問済み:

2021 年 9 月 5 日

コメント済み:

2023 年 6 月 17 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by