How to plot numbers on Tornado Chart?
29 views (last 30 days)
Show older comments
Hi Guys, I created a tornado plot but I want numbers beside each bar. The same way you could have numbers above bars in a bar chart. How do I code that? See my code below.
clear all
close all
clc
% Names of the Y axis ticks
names={'Feedstock cost';
'Fixed Capital Cost';
'IRR';
'Fixed Operating Cost'; 'Capacity Factor'};
% Initialize low and high matricies for speed
LCOE_low=[ 14.8, 15.627379, 15.64117003, 15.66, 18.133451];
LCOE_high=[17.77587095,16.94949315,16.96679901,16.92362191,15.06];
% The base value is where the y axis is centered
LCOE_base_value=16.29;
% Sort the values based on the lower change
% Sort the higher values and the names arrays
% using the same indices
[LCOE_low ind]=sort(LCOE_low,'descend');
LCOE_high=LCOE_high(ind);
names_LCOE=names(ind);
% Create a figure and plot the low and high horizontally
figure
h = barh(LCOE_low);
hold on
barh(LCOE_high,'r')
bh = get(h,'BaseLine');
set(bh,'BaseValue',LCOE_base_value);
title('Sensitivity Analysis')
set(gca,'yticklabel',names)
set(gca,'Ytick',[1:length(names)],'YTickLabel',[1:length(names)])
set(gca,'yticklabel',names_LCOE,'FontSize',12)
xlabel('LCOE ($/kWh)','FontWeight','bold')
1 Comment
Star Strider
on 5 Jul 2023
According to the barh documentation section on Add Labels to the Ends of Bars this is supposed to work (and does work everywhere else I’ve used it), however it doesn’t work with your plot —
% Names of the Y axis ticks
names={'Feedstock cost';
'Fixed Capital Cost';
'IRR';
'Fixed Operating Cost'; 'Capacity Factor'};
% Initialize low and high matricies for speed
LCOE_low=[ 14.8, 15.627379, 15.64117003, 15.66, 18.133451];
LCOE_high=[17.77587095,16.94949315,16.96679901,16.92362191,15.06];
% The base value is where the y axis is centered
LCOE_base_value=16.29;
% Sort the values based on the lower change
% Sort the higher values and the names arrays
% using the same indices
[LCOE_low ind]=sort(LCOE_low,'descend');
LCOE_high=LCOE_high(ind);
names_LCOE=names(ind);
% Create a figure and plot the low and high horizontally
figure
h1 = barh(LCOE_low);
hold on
h2 = barh(LCOE_high,'r');
bh1 = get(h1,'BaseLine');
set(bh1,'BaseValue',LCOE_base_value);
title('Sensitivity Analysis')
set(gca,'yticklabel',names)
set(gca,'Ytick',[1:length(names)],'YTickLabel',[1:length(names)])
set(gca,'yticklabel',names_LCOE,'FontSize',12)
xlabel('LCOE ($/kWh)','FontWeight','bold')
text(h1.XEndPoints, h1.YEndPoints, string(h1.YEndPoints), 'Horiz','right', 'Vert','middle')
text(h2.XEndPoints, h2.YEndPoints, string(h2.YEndPoints), 'Horiz','left', 'Vert','middle')
% for k = 1:numel(h1.XEndPoints)
% h1.XEndPoints(k)
% text(h1.XEndPoints(k), h1.YEndPoints(k), string(h1.YEndPoints(k)), 'Horiz','right', 'Vert','middle')
% string(h1.YEndPoints(k))
% text(h2.XEndPoints(k), h2.YEndPoints(k), string(h2.YEndPoints(k)), 'Horiz','left', 'Vert','middle')
% end
I’ve tried it with and without the loop, and plotting text objects randomly on the plot, and none of them work. All the 'XEndPoint' and 'YEndPoint' references return the correct values for both bar series, so that isn’t the problem. For some reason I cannot understand, your plot simply will not work with text objects. It should.
.
Accepted Answer
Voss
on 5 Jul 2023
clear all
close all
clc
% Names of the Y axis ticks
names={'Feedstock cost';
'Fixed Capital Cost';
'IRR';
'Fixed Operating Cost'; 'Capacity Factor'};
% Initialize low and high matricies for speed
LCOE_low=[ 14.8, 15.627379, 15.64117003, 15.66, 18.133451];
LCOE_high=[17.77587095,16.94949315,16.96679901,16.92362191,15.06];
% The base value is where the y axis is centered
LCOE_base_value=16.29;
% Sort the values based on the lower change
% Sort the higher values and the names arrays
% using the same indices
[LCOE_low ind]=sort(LCOE_low,'descend');
LCOE_high=LCOE_high(ind);
names_LCOE=names(ind);
% Create a figure and plot the low and high horizontally
figure
h1 = barh(LCOE_low);
hold on
h2 = barh(LCOE_high,'r');
bh = get(h1,'BaseLine');
set(bh,'BaseValue',LCOE_base_value);
title('Sensitivity Analysis')
set(gca,'yticklabel',names)
set(gca,'Ytick',[1:length(names)],'YTickLabel',[1:length(names)])
set(gca,'yticklabel',names_LCOE,'FontSize',12)
xlabel('LCOE ($/kWh)','FontWeight','bold')
text(h1.YEndPoints, h1.XEndPoints, string(h1.YEndPoints), 'Horiz','right', 'Vert','middle')
text(h2.YEndPoints, h2.XEndPoints, string(h2.YEndPoints), 'Horiz','left', 'Vert','middle')
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots 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!