条形图上的不同颜色和数据值:条形图Matlab

条形图上的不同颜色和数据值:条形图Matlab,matlab,bar-chart,Matlab,Bar Chart,我想在条形图中绘制标量TEV_Idz和TEV_TF 问题1:我想看到两种不同颜色的两个标量的图例,但我只得到一个两个条形图具有相同蓝色的图例 问题2:我试图获取每个标量在各自条上的值,但无法使用下面的函数生成输出 这是我的输出: 这是我的代码: 条形图每个系列只能采用一种颜色,所以我们只需要将您的数据分为两个系列,而不是一个系列!每个系列都是一个矩阵行,矩阵的每一列都是不同的颜色,所以如果我们添加一些0,这样填充就不会显示为任何高度的实际条,您就可以实现您想要的效果 这实际上解决了问题1,使图例

我想在条形图中绘制标量TEV_Idz和TEV_TF

问题1:我想看到两种不同颜色的两个标量的图例,但我只得到一个两个条形图具有相同蓝色的图例

问题2:我试图获取每个标量在各自条上的值,但无法使用下面的函数生成输出

这是我的输出:

这是我的代码:


条形图每个系列只能采用一种颜色,所以我们只需要将您的数据分为两个系列,而不是一个系列!每个系列都是一个矩阵行,矩阵的每一列都是不同的颜色,所以如果我们添加一些0,这样填充就不会显示为任何高度的实际条,您就可以实现您想要的效果

这实际上解决了问题1,使图例正常工作,并使问题2更容易

有关详细信息,请参见代码注释,注意该条必须“堆叠”

输出:

TEV_plot = bar([TEV_Idz;TEV_TF], 0.6);
grid on;
set(gca, 'yTickLabel',num2str(100.*get(gca,'yTick')','%g%%'));

% PROBLEM 1: The code for having a legend
ii = cell(1,2);
ii{1}='L'; ii{2}='B';     %first bar called L and second bar called B
legend(TEV_plot,ii);      %mylegend

%PROBLEM 2: This is my code for plotting the value of each scalar on the top of the every bar graph.
for k = 1:numel(TEV_plot)
    text(k, TEV_plot(k), {num2str(TEV_plot(k)), ''}, ...
        'HorizontalAlignment', 'center', ...
        'verticalalignment', 'bottom')
end
TEV_Idz = 0.0018; TEV_TF = 0.012;
% Each row in a bar plot is its own series, so put the data on two rows.
% This can be created using TEV_data = diag([TEV_Idz, TEV_TF]); 
TEV_data = [TEV_Idz, 0; 
            0,       TEV_TF]; 
% Each column will be a different colour, plot them stacked to be central
TEV_plot = bar(TEV_data, 0.6, 'stacked');
grid on; ylim([0, max(TEV_data(:) + 0.002)]);
set(gca, 'yTickLabel',num2str(100.*get(gca,'yTick')','%g%%'));

% Insert legend, one element for each series
legend({'L','B'}, 'location', 'northoutside', 'orientation', 'horizontal')
% A better option might be to just label on the x axes and not have a legend:
% set(gca, 'xticklabels', {'L','B'})

% Plotting the value of each scalar on the top of the every bar graph.
% The diagonal entries of TEV_data is where the actual data is stored. 
% Use num of diagonal entries, and index (k,k) for diagonal.
for k = 1:numel(diag(TEV_data))
    text(k, TEV_data(k,k), num2str(100.*TEV_data(k,k),'%g%%'), ...
        'HorizontalAlignment', 'center', ...
        'verticalalignment', 'bottom')
end