Skip to content

BarPlot: incorrect automatic axis limits when negative offsets are used #1750

@swharden

Description

@swharden

@painstgithub noted in #1748 that automatic axis limits do not properly account for negative offsets

double[] heights = ScottPlot.DataGen.Ones(21);
double[] bases = ScottPlot.DataGen.Sin(21);

var bar = formsPlot1.Plot.AddBar(heights);
bar.ValueOffsets = bases;

image

That logic is here:

public virtual AxisLimits GetAxisLimits()
{
double valueMin = double.PositiveInfinity;
double valueMax = double.NegativeInfinity;
double positionMin = double.PositiveInfinity;
double positionMax = double.NegativeInfinity;
for (int i = 0; i < Positions.Length; i++)
{
valueMin = Math.Min(valueMin, Values[i] - ValueErrors[i] + ValueOffsets[i]);
valueMax = Math.Max(valueMax, Values[i] + ValueErrors[i] + ValueOffsets[i]);
positionMin = Math.Min(positionMin, Positions[i]);
positionMax = Math.Max(positionMax, Positions[i]);
}
valueMin = Math.Min(valueMin, ValueBase);
valueMax = Math.Max(valueMax, ValueBase);
if (ShowValuesAboveBars)
valueMax += (valueMax - valueMin) * .1; // increase by 10% to accommodate label
positionMin -= BarWidth / 2;
positionMax += BarWidth / 2;
positionMin += PositionOffset;
positionMax += PositionOffset;
return Orientation == Orientation.Vertical ?
new AxisLimits(positionMin, positionMax, valueMin, valueMax) :
new AxisLimits(valueMin, valueMax, positionMin, positionMax);
}

I think the error is here:

valueMin = Math.Min(valueMin, Values[i] - ValueErrors[i] + ValueOffsets[i]);
valueMax = Math.Max(valueMax, Values[i] + ValueErrors[i] + ValueOffsets[i]);

valueMin should represent the base of each bar and not include the Values[i] unless it is negative

Metadata

Metadata

Assignees

No one assigned

    Labels

    BUGunexpected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions