【问题标题】:Different colors and data value on bars: Bar Chart Matlab条形图上的不同颜色和数据值:Bar Chart Matlab
【发布时间】:2017-05-12 08:14:14
【问题描述】:

我想在条形图中绘制标量 TEV_IdzTEV_TF

问题 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

【问题讨论】:

    标签: matlab bar-chart


    【解决方案1】:

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

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

    详情见code cmets,注意bar必须是'stacked'

    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
    

    输出:

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2017-11-13
      • 2017-05-13
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多