How to change bars colour in MATLAB

后端 未结 3 395
生来不讨喜
生来不讨喜 2021-01-25 20:27

I am new to programming so I am learning introductory to MATLAB I was wondering how you could change colours of bar in MATLAB.

this is my script. Can someone please help

3条回答
  •  后悔当初
    2021-01-25 20:48

    Whilst this is overkill for your specific question, in general, to change the colour of bars depending on their height, you can apply a colormap to the bars. This is mainly from the bar documentation.

    x = 1:8;
    y = 10*x;
    h=bar(y);  %// create a sample bar graph
    

    For the colormap MAP, you do this:

    colormap(MAP)
    ch = get(h,'Children');
    fvd = get(ch,'Faces');
    fvcd = get(ch,'FaceVertexCData');
    [zs, izs] = sort(y);
    for i = 1:length(x)
        row = izs(i);
        fvcd(fvd(row,:)) = i;
    end
    set(ch,'FaceVertexCData',fvcd)
    hold off
    

    And, for example, using the builtin colormap hsv, you get enter image description here

    But in this case we want a very specific colormap,

    b=40 %// the cut-off for changing the colour
    MAP=zeros(length(x),3); %// intialise colormap matrix
    MAP(y=b,:)=repmat([0 1 0],sum(y>=b),1); %// [0 1 0] is green, for y>=40
    colormap(MAP)
    

    which gives

    enter image description here

提交回复
热议问题