gonum / plot

A repository for plotting and visualizing data
BSD 3-Clause "New" or "Revised" License
2.72k stars 202 forks source link

Extra X-axis padding when there is a label #779

Closed JAicewizard closed 2 months ago

JAicewizard commented 3 months ago

What are you trying to do?

Create a graph with 0-padded axis and axis names

What did you do?

package main

import (
    "gonum.org/v1/plot"
    "gonum.org/v1/plot/plotter"
)

type (
    benchmarkSequence struct {
    }
)

func main() {
    p := plot.New()
    p.X.Padding = 0
    p.Y.Padding = 0
    p.X.Label.Text = "P (Passingrate)" // comment this line to get expected result

    line, err := plotter.NewLine(benchmarkSequence{})
    if err != nil {
        panic(err)
    }
    p.Add(line)
    err = p.Save(500, 500, "plotLogo.png")
    if err != nil {
        panic(err)
    }
}

// Implement plot.XYer for benchmarkSequence
func (s benchmarkSequence) Len() int {
    return 99
}

func (s benchmarkSequence) XY(i int) (float64, float64) {
    return float64(i) / 100, float64(i) / 100
}

What did you expect to happen?

plotLogo

What actually happened?

plotLogo

What version of Go and Gonum/plot are you using?

I just fetched gonum 0.13, and im using go 1.22.3 (linux/amd64)

Does this issue reproduce with the current master?

Did not try, but there does not seem to be any real changes in master

JAicewizard commented 3 months ago

Actually you can see that with the expected result the Y-axis is also misaligned

kortschak commented 3 months ago

It looks to me like the descents should not be being used in calculating the height of the axis. If I do this

diff --git a/axis.go b/axis.go
index 3b275ad..451acce 100644
--- a/axis.go
+++ b/axis.go
@@ -235,7 +235,6 @@ type horizontalAxis struct {
 // size returns the height of the axis.
 func (a horizontalAxis) size() (h vg.Length) {
        if a.Label.Text != "" { // We assume that the label isn't rotated.
-               h += a.Label.TextStyle.FontExtents().Descent
                h += a.Label.TextStyle.Height(a.Label.Text)
                h += a.Label.Padding
        }

I get this plotLogo

@sbinet WDYT?

sbinet commented 2 months ago

(apologies for the belated answer. with the events unfolding in France, I have a limited bandwith for software...)

SGTM.