Skip to content

BarPlotBase: Fixes axis calculation.#1857

Merged
swharden merged 5 commits intoScottPlot:mainfrom
bclehmann:fix/barchart-axis-limits
Jun 2, 2022
Merged

BarPlotBase: Fixes axis calculation.#1857
swharden merged 5 commits intoScottPlot:mainfrom
bclehmann:fix/barchart-axis-limits

Conversation

@bclehmann
Copy link
Member

Purpose:
#1855

There were two issues:

  • ValueOffsets was not taken into account when calculating ValueMin
  • ValueOffsets was not taken into account when ensuring the base of the bar was between ValueMin and ValueMax

The second one is a little hard to explain without referencing the code, but there used to be these two lines:

valueMin = Math.Min(valueMin, ValueBase);
valueMax = Math.Max(valueMax, ValueBase);

This calculation is incorrect because the base of the bar is really ValueBase + ValueOffsets[i], so for non-zero ValueOffsets[i] you get incorrect results, such as with the following code:

double[] values = { 23, 17, 19, 24, 22 };
double[] yOffsets = { -100, -100, -100, -100, -100 }; // This is in the cookbook, but I think the better way to do this is just set ValueBase = -100. Regardless, it illustrates the issue

var bar = plt.AddBar(values);
bar.ValueOffsets = yOffsets;

Before:
image

Note that the axis goes from -100 to 0, as it's trying to keep ValueBase = 0 in frame, but it should be keeping ValueBase + ValueOffsets[i] = -100 in frame

After:
image

There were two issues:
  - ValueOffsets was not taken into account when calculating ValueMin
  - ValueOffsets was not taken into account when ensuring the base of
    the bar was within [ValueMin, ValueMax]
@swharden swharden linked an issue Jun 2, 2022 that may be closed by this pull request
@swharden swharden enabled auto-merge June 2, 2022 01:47
@swharden swharden merged commit f423f2c into ScottPlot:main Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problems with plt.AxisAuto() and negative numbers

2 participants