【问题标题】:matlab bar colormap returns the same color for all barsmatlab bar colormap为所有条返回相同的颜色
【发布时间】:2013-07-23 16:02:41
【问题描述】:

我在使用 bar 和颜色图时遇到问题。

我有一个这样的 csv 文件,其中包含六个任务的完成时间:

34,22,103,22,171,26
24,20,41,28,78,28
37,19,60,23,141,24
...

然后我用 的方法创建了一个条形图,并添加了标准变化误差条。

res = csvread('sorting_results.csv');
figure();
y = mean(res)';
e = std(res);
hold on;
bar(y);
errorbar(y,e,'.r');
title('Sorting completion time');
ylabel('Completion time (seconds)');
xlabel('Task No.');
hold off;
colormap(summer(size(y,2)));

为什么是这样的输出?为什么这些条具有相同的颜色?以及如何将图例添加到六个小节中?

【问题讨论】:

    标签: matlab matlab-figure


    【解决方案1】:

    一段神奇的代码。它不使用@am304 提到的规范技术,因为您将很难用它来设置图例。在这里,对于 6 个输入值中的每一个,我们绘制完整的 6 个条形图:一个带有值的条形图,其余五个设置为零。

    x = rand(1,6);     %create data
    x_diag = diag(x);  %zero matrix with diagonal filled with x
    cmap = summer(6);  %define colors to use (summer colomap)
    
    figure('color','w','Render','Zbuffer'); %create figure
    
    %bar plot for each x value
    for ind_data = 1:length(x)
        h_bar = bar( x_diag(ind_data, :)); %bar plot
        set( get(h_bar,'children'), 'FaceVertexCData', cmap(ind_data,:) ) ; %color
        hold on;
    end
    colormap('summer');
    
    %legend-type info
    hleg = legend( ('a':'f')' ); 
    set(hleg, 'box', 'off');
    %xticks info
    set(gca, 'XTickLabel', ('a':'f')' );
    
    %plot errors
    e = ones(1,6) * 0.05;
    errorbar(x, e,'.r');
    set(gca, 'FontSize', 14, 'YLim', [ 0 (max(x) + max(e) + 0.1) ]);
    

    【讨论】:

      【解决方案2】:

      请参阅 MATLAB 文档中的 Coloring 2-D Bars According to Height。只有颜色图的第一种颜色用于为面部着色,您需要一些技巧(根据该文档页面上的代码)来做您想做的事情。

      【讨论】:

        猜你喜欢
        • 2016-10-19
        • 2020-10-13
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        • 1970-01-01
        相关资源
        最近更新 更多