labelbar-chartscilab

Show data label in bar chart


Using bar charts in Scilab is there any straight way or workaround to show the data labels next to the bars?

Scilab chart:

enter image description here

example chart with data labels I would like to achieve in Scilab charts. I did not find any option but there might be a workaround?

enter image description here


Solution

  • Here is a commented example similar to your case, with grouped bars:

    clf
    [x, y] = ([1 2 5], [1  -5 6;3 -2 7;4  -3 8]);
    h = bar(x, y);
    xgrid(color("grey50"))
    
    txy = h.children.data;  // get bars raw coordinates
    // tune x coordinates inside groups
    txy(:,1) = txy(:,1) + h.children.x_shift'(:);
    
    // get Text height to tune y coordinates according to labels above vs below bars
    th = xstringl(1,1,"8")(4);
        
    // Make formated labels
    ylab = msprintf("%4.1f\n",txy(:,2))' ;
    
    // Display labels at tuned y
    t = xstring(txy(:,1), txy(:,2) + th*((txy(:,2)>=0)-0.5), ylab);
    // Center them on their required position.
    //   This makes rendered positions robust against figure resizing
    t.children.text_box_mode = "centered";
    
    // reframe the plot to show the value for the highest bar
    replot
    

    Result:

    enter image description here