这是想要实现的效果
这是实际实现的效果
以下是代码
close all;clear all;clc;
% Data
latency = [1.01, 0.81, 0.89, 0.86, 0.96, 0.77, 1.02, 1.00, 0.87];
latency_modified = (latency-min(latency))/(max(latency)-min(latency))+min(latency)/max(latency);
accuracy = [71.5, 68.3, 73.5, 73.9, 75.4, 74.8, 76.9, 76.9, 77.4];
accuracy_modified = (accuracy-min(accuracy))/(max(accuracy)-min(accuracy))+min(accuracy)/max(accuracy);
labels = {'MobileNetV3', 'sep. mixer', 'exp. ratio', 'early conv', 'deeper d.s.', 'classifier', 'stage ratio', 'kernel sz.', 'SE place. RepViT'};
% Create the figure
figure;
ax = gca; % 获取当前轴对象
% Latency bars pointing left
barh(-latency_modified, 'FaceColor', 'r');
hold on;
% Accuracy bars pointing right
barh(accuracy_modified, 'FaceColor', 'g');
hold off;
% Add labels and titles
xlabel('Latency (ms) / Top-1 Accuracy (%)');
title('Latency and Top-1 Accuracy Comparison');
% Add text labels
% For latency
for i = 1:length(latency)
text(-latency_modified(i) + 0.3, i, num2str(latency(i)), 'HorizontalAlignment', 'right', 'VerticalAlignment', 'middle', 'Color', 'k');
end
% For accuracy
for i = 1:length(accuracy)
text(accuracy_modified(i) - 0.3, i, num2str(accuracy(i)), 'HorizontalAlignment', 'left', 'VerticalAlignment', 'middle', 'Color', 'k');
end
% Set y-axis labels
yticks(1:length(labels));
yticklabels(labels);
% Adjust the axis limits
xlim([-min(latency)/max(latency)-1 min(accuracy)/max(accuracy)+1]); % Adjust the limits based on the maximum value of latency and accuracy
% Add grid
grid on;
% 隐藏X轴刻度
set(ax, 'XTick', []);