make color of error bars the same as the bar fill

I must be missing something very simple here .  I would like to make a bar graph similar to the program below, but I would like the error bar colors to match the bar fills. When I try plotsetlinecolor(...) right before the plotAddErrorBar(), it only reads the first color. Thanks

// Create 2x1 numeric vectors

seattle = { 47, 70 };
phoenix = { 68, 105 };
denver = { 32, 85 };

// Create 2x1 string labels
labels = "January" $| "June";

// Declare 'myPlot' to be a plotControl struct
// and fill with defaults settings for bar plots
struct plotControl myPlot;
myPlot = plotGetDefaults("bar");

// Create legend

leg_text = "Phoenix" $| "Seattle" $| "Denver" $| "";
plotSetLegend(&myPlot, leg_text, "top left inside");

// Set legend border to zero pixels wide
plotSetLegendBorder(&myPlot, "gray", 0);

// Set Fill to two different fill patterns
// and 100% opacity
plotSetFill(&myPlot, 12|13, 1);

// Use HTML to create the degrees symbol
plotSetYLabel(&myPlot, "Temp °F");

// Concatenate temperature vectors
// into a 2x2 matrix
heights = phoenix ~ seattle ~ denver;

// Draw the graph
plotBar(myPlot, labels, heights);

se = {  5  8,
        8 10,
       30 15 };

plotAddBoxErrorBars(labels, heights, se);

proc (0) = plotAddBoxErrorBars(labels, heights, se);
    local box_xs;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    plotAddErrorBar(box_xs, vec(heights), vecr(se));
endp;

proc (1) = calcBoxCenters(nlabels, nboxes);
    local box_width, idx, x;

    box_width = 0.5;

    x = seqa(1, 1, nlabels);

    idx = seqa(x[1]-box_width/2+box_width/(nboxes*2), 2*box_width/(nboxes*2), nboxes);
    retp(vecr(idx+(x-1)'));

endp;

1 Answer



0



To change the colors of the error bars, we need to pass a plotControl structure with the desired line color settings to plotSetErrorBar .
Since each set of error bars added with shares the same line color, we need to loop over each set of lines that we want to have a separate color and add the lines.

Here is the code from the example you posted that needs to be modified.

plotAddBoxErrorBars(labels, heights, se, myPlot.fillColor);

proc (0) = plotAddBoxErrorBars(labels, heights, se, clrs);
    local box_xs, j;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    heights = vec(heights);
    se = vecr(se);

    // Create new plotControl structure for loines
    struct plotControl plt;
    plt = plotGetDefaults("xy");

    /*
    ** Each series of lines added shares the same color.
    ** So we loop and add the lines by color
    */
    j = 1;
    for i(1, 6, 2);
        // Set the corresponding color
        plotSetLinePen(&plt, 1, clrs[j]);
        plotAddErrorBar(plt, box_xs[i:i+1], heights[i:i+1], se[i:i+1]);
        j = j + 1;
    endfor;
endp;

aptech

1,773

Your Answer

1 Answer

0

To change the colors of the error bars, we need to pass a plotControl structure with the desired line color settings to plotSetErrorBar . Since each set of error bars added with shares the same line color, we need to loop over each set of lines that we want to have a separate color and add the lines.

Here is the code from the example you posted that needs to be modified.

plotAddBoxErrorBars(labels, heights, se, myPlot.fillColor);

proc (0) = plotAddBoxErrorBars(labels, heights, se, clrs);
    local box_xs, j;

    box_xs = calcBoxCenters(rows(labels), cols(heights));

    heights = vec(heights);
    se = vecr(se);

    // Create new plotControl structure for loines
    struct plotControl plt;
    plt = plotGetDefaults("xy");

    /*
    ** Each series of lines added shares the same color.
    ** So we loop and add the lines by color
    */
    j = 1;
    for i(1, 6, 2);
        // Set the corresponding color
        plotSetLinePen(&plt, 1, clrs[j]);
        plotAddErrorBar(plt, box_xs[i:i+1], heights[i:i+1], se[i:i+1]);
        j = j + 1;
    endfor;
endp;


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.