Matlab 如何在分组条形图顶部绘制线条?

Matlab 如何在分组条形图顶部绘制线条?,matlab,plot,bar-chart,Matlab,Plot,Bar Chart,我有一个分组条形图,我想比较这些值。也就是说,我的意思是,我想用线来可视化它。我尝试了下面的代码,输出也如下 Y=rand(5,5) str = {'A'; 'B'; 'C'; 'D'; 'E';}; bar_widh=0.2; h = bar(Y,bar_widh); hold on;plot(Y,'b'); set(gca, 'XTickLabel',str, 'XTick',1:numel(str)) grid on l = cell(1,5); l{1}='P'; l{2}='Q'; l

我有一个分组条形图,我想比较这些值。也就是说,我的意思是,我想用线来可视化它。我尝试了下面的代码,输出也如下

Y=rand(5,5)
str = {'A'; 'B'; 'C'; 'D'; 'E';};
bar_widh=0.2;
h = bar(Y,bar_widh);
hold on;plot(Y,'b');
set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
grid on
l = cell(1,5);
l{1}='P'; l{2}='Q'; l{3}='R'; l{4}='S'; l{5}='T'; 
legend(h,l);
我得到了以下输出:

我想将最小数量/较大数量的条可视化。在某些情况下,较大的值是不好的。你能帮我画出和这条线一样的线的颜色吗

我得到如下输出

您可以尝试以下方法:

Y=rand(5,5);
str = {'A'; 'B'; 'C'; 'D'; 'E';};
bar_widh=0.2;

figure; hold on;grid on
h = bar(Y,bar_widh);

% to highlight the minimum of each group, 
% copy data into a new matrix
Y_ = Y; 
% find the minimum values and make the rest zeors
Y_(Y_~=repmat(min(Y_,[],1),size(Y,1),1)) = 0;
% then plot with so sort of highlighting
h2 = bar(Y_,0.5);

pause(0.1) % pause to allow bars to be drawn

% now go through each group of bars and plot the line
for i = 1:numel(h)
    x = h(i).XData + h(i).XOffset; % find the x coordinates where the bars are plotted
    ax = plot(x,Y(:,i)); % plot the line
    % set color of the bars the same as the line
    h(i).FaceColor = ax.Color; 
    h2(i).FaceColor = ax.Color;
end

set(gca, 'XTickLabel',str, 'XTick',1:numel(str))
legend('P','Q','R','S','T');

h(i).扩展数据

是第i组钢筋的中心坐标

例如,在您的情况下:

h(1).XData = [ 1 2 3 4 5 ]; % group P
h(2).XData = [ 1 2 3 4 5 ]; % group Q
...
h(5).XData = [ 1 2 3 4 5 ]; % group T
h(1).XOffset = -0.3077; % group P
h(2).XOffset = -0.1538; % group Q
...
h(5).XOffset = 0.3077; % group T
h(i).XOffset

是组中每个杆相对于其相应中心坐标的偏移值

例如,在您的情况下:

h(1).XData = [ 1 2 3 4 5 ]; % group P
h(2).XData = [ 1 2 3 4 5 ]; % group Q
...
h(5).XData = [ 1 2 3 4 5 ]; % group T
h(1).XOffset = -0.3077; % group P
h(2).XOffset = -0.1538; % group Q
...
h(5).XOffset = 0.3077; % group T

不突出显示最小值

突出显示最小值

您当前的输出有什么问题?@m7913d线条没有添加到条形图的顶部,而是添加到A、B、C、D和E。实际上,我想可视化哪个线条较小,或者您可以帮助我将线条的颜色设置为与条形图相同的颜色吗?您可以使用h(1)设置条形图的颜色。面颜色并使用相同的颜色绘制线条(因此,您应该使用for循环)总结解决方案的关键部分并提供解决方案的屏幕截图可能很有用。@Anthony是否可以突出显示每组条形图的最小第一个值(即B C D的第一个条形图中的最小值)E@user上传了一个新的图形以显示突出显示的条。它们更厚:)。@user你是什么意思?你能上传一张照片吗?@user我没看到任何问题。在A组中,蓝色是最小值,非常接近于0,可能几乎看不见,但从线条上看仍然很明显。