Add Percentage to the Bars in Tornado Plot
4 次查看(过去 30 天)
显示 更早的评论
How to add the percentage of how much each value decrease and increase in the bars in Tornado plot like in this picture?
here is my code:
% Input data
Data = [
0.983 0.955 0.935;
1.006 0.955 0.920;
0.897 0.955 1.014;
1.026 0.955 0.906;
0.892 0.955 1.038;
1.058 0.955 0.888;
1.059 0.955 0.887
];
% Calculate the differences between low, base, and high values
diff_low = Data(:, 1) - Data(:, 2);
diff_high = Data(:, 3) - Data(:, 2);
% Plot tornado plot
figure;
barh([diff_low, diff_high], 'stacked');
set(gca, 'YTickLabel', {'', '1', '2', '3', '4', '5', '6' , '7'});
xlabel('% Change to LCOW from Base Case (0.955 $/m^{3})');
%ylabel('Data Point');
title('Sensitivity Analysis of CSP-RO');
legend('Decrease Cost', 'Increase Cost');
% Display grid
grid on;
% Customize axis labels
set(gca, 'YTick', 1:7);
yticklabels({'DNI, W/m² (-20%, 755, +20%)', 'Membrane lifetime, yr (-20%, 5, +20%)', 'IRR, % (-20%, 5, +20%)', 'Operating hours, hr/day (-20%, 6, +20%)', 'Seawater salinity, g/m³ (-20%, 45, +20%)', 'Plant lifetime, yr (-20%, 20, +20%)', 'Plant capacity, m³/dy (-20%, 3500, +20%)'});
0 个评论
采纳的回答
atharva
2023-10-19
Hey Nassar,
I understand that you want to add percentage labels to the bars in tornado plot in MATLAB.
Try the following code-
% Input data
Data = [
0.983 0.955 0.935;
1.006 0.955 0.920;
0.897 0.955 1.014;
1.026 0.955 0.906;
0.892 0.955 1.038;
1.058 0.955 0.888;
1.059 0.955 0.887
];
% Calculate the differences between low, base, and high values
diff_low = Data(:, 1) - Data(:, 2);
diff_high = Data(:, 3) - Data(:, 2);
% Calculate the percentage change for each value
percent_change_low = (diff_low ./ Data(:, 2)) * 100;
percent_change_high = (diff_high ./ Data(:, 2)) * 100;
% Plot tornado plot
figure;
h = barh([diff_low, diff_high], 'stacked');
set(gca, 'YTickLabel', {'', '1', '2', '3', '4', '5', '6', '7'});
xlabel('% Change to LCOW from Base Case (0.955 $/m^{3})');
title('Sensitivity Analysis of CSP-RO');
legend('Decrease Cost', 'Increase Cost');
grid on;
% Customize axis labels
set(gca, 'YTick', 1:7);
yticklabels({'DNI, W/m² (-20%, 755, +20%)', 'Membrane lifetime, yr (-20%, 5, +20%)', 'IRR, % (-20%, 5, +20%)', 'Operating hours, hr/day (-20%, 6, +20%)', 'Seawater salinity, g/m³ (-20%, 45, +20%)', 'Plant lifetime, yr (-20%, 20, +20%)', 'Plant capacity, m³/dy (-20%, 3500, +20%)'});
% Add percentage labels to both sides of the bars
xpos = [diff_low, diff_high];
ypos = 1:7;
labels = arrayfun(@(value) sprintf('%.2f%%', value), [percent_change_low], 'UniformOutput', false);
for i = 1:7
text(xpos(i), ypos(i), labels{i}, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', 'Color', 'k');
end
xpos = [diff_high, diff_low];
ypos = 1:7;
labels = arrayfun(@(value) sprintf('%.2f%%', value), [percent_change_high], 'UniformOutput', false);
for i = 1:7
text(xpos(i), ypos(i), labels{i}, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom', 'Color', 'k');
end
I hope this helps!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!