Skip to content

[Bug] PlotBars baseline=0 breaks on log-scaled axes #669

@Gidid56

Description

@Gidid56

Bug: PlotBars baseline=0 breaks on log-scaled axes

When using ImPlot::PlotBars(...) with a log-scaled value axis (ImPlotScale_Log10), the bars are drawn with a baseline of 0. Since 0 is invalid on logarithmic axes, this can cause bar geometry to explode and appear to draw outside the plot boundaries / clip rect.

Image ---

Minimal Repro

static double values[] = { 1, 10, 100, 1000 };

if (ImPlot::BeginPlot("Bars + Log Axis")) {
    ImPlot::SetupAxisScale(ImAxis_Y1, ImPlotScale_Log10);
    ImPlot::SetupAxisLimits(ImAxis_Y1, 1e-1, 1e9);
    ImPlot::PlotBars("bars", values, 4, 0.8);
    ImPlot::EndPlot();
}

Observed

Bars may extend far beyond the plot area (or appear to draw outside plot boundaries) when the value axis uses ImPlotScale_Log10.


Expected

Bars should start at a log-safe baseline (e.g. the current axis minimum), not at 0, when the value axis is logarithmic.

Image

Proposed Fix (small, localized)

Clamp the baseline used by PlotBars from 0 to a log-safe value when the value axis is log-scaled:

  • If the relevant axis scale is ImPlotScale_Log10, use axis.Range.Min if > 0, otherwise fall back to a small epsilon (e.g. 1e-12).
  • Keep the baseline behavior unchanged for linear axes.

This preserves existing rendering/legend/toggle behavior and only replaces an invalid baseline on log scales.


Patch Summary (implot_items.cpp)

The issue comes from getter2 always using IndexerConst(0) as the baseline. The fix is to replace that 0 with a log-safe baseline when the value axis is log-scaled.

Example (conceptually):

// before (vertical bars)
GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(1.0,shift), IndexerConst(0), count);

// after (vertical bars)
const double base = GetLogSafeBaseline(ImAxis_Y1, 0.0);
GetterXY<IndexerLin,IndexerConst> getter2(IndexerLin(1.0,shift), IndexerConst(base), count);

Same idea applies to:

  • horizontal bars (value axis is X1)
  • PlotBars(xs, ys, ...)
  • PlotBarsG(...)

I have a working patch applied locally and can provide the full diff if desired.

Metadata

Metadata

Assignees

Labels

prio:highHigh prioritystatus:todoTask identified but not startedtype:fixSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions