Add Percentage to the Bars in Tornado Plot
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nassar AlMarry
am 19 Okt. 2023
Kommentiert: Nassar AlMarry
am 19 Okt. 2023
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 Kommentare
Akzeptierte Antwort
atharva
am 19 Okt. 2023
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!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Exploration finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
