gonum / plot

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

show bar problem #657

Closed fengjiansunren closed 3 years ago

fengjiansunren commented 3 years ago

What are you trying to do?

show bar

What did you do?

when the bar is too high, the legend will cover the bar

What actually happened?

image

kuriankattukaren commented 3 years ago

You can move the legend to the top left by setting:

p.Legend.Top = true
p.Legend.Left = true

where p is a plot.

kuriankattukaren commented 3 years ago

You can also refer to the barchart_example_test.go file. Here's a snippet from there:

    // Now, make a different type of BarChart.
    groupA := plotter.Values{20, 35, 30, 35, 27}
    groupB := plotter.Values{25, 32, 34, 20, 25}
    groupC := plotter.Values{12, 28, 15, 21, 8}
    groupD := plotter.Values{30, 42, 6, 9, 12}

    p, err := plot.New()
    if err != nil {
        log.Panic(err)
    }
    p.Title.Text = "Bar chart"
    p.Y.Label.Text = "Heights"

    w := vg.Points(8)

    barsA, err := plotter.NewBarChart(groupA, w)
    if err != nil {
        log.Panic(err)
    }
    barsA.Color = color.RGBA{R: 255, A: 255}
    barsA.Offset = -w / 2

    barsB, err := plotter.NewBarChart(groupB, w)
    if err != nil {
        log.Panic(err)
    }
    barsB.Color = color.RGBA{R: 196, G: 196, A: 255}
    barsB.Offset = w / 2

    barsC, err := plotter.NewBarChart(groupC, w)
    if err != nil {
        log.Panic(err)
    }
    barsC.XMin = 6
    barsC.Color = color.RGBA{B: 255, A: 255}
    barsC.Offset = -w / 2

    barsD, err := plotter.NewBarChart(groupD, w)
    if err != nil {
        log.Panic(err)
    }
    barsD.Color = color.RGBA{B: 255, R: 255, A: 255}
    barsD.XMin = 6
    barsD.Offset = w / 2

    p.Add(barsA, barsB, barsC, barsD)
    p.Legend.Add("A", barsA)
    p.Legend.Add("B", barsB)
    p.Legend.Add("C", barsC)
    p.Legend.Add("D", barsD)
    **p.Legend.Top = true**
    p.NominalX("Zero", "One", "Two", "Three", "Four", "",
        "Six", "Seven", "Eight", "Nine", "Ten")

    p.Add(plotter.NewGlyphBoxes())
    err = p.Save(300, 250, "testdata/barChart2.png")
    if err != nil {
        log.Panic(err)
    }

This code won't compile as its a snippet. However it shows how the legend is set.

fengjiansunren commented 3 years ago

You can move the legend to the top left by setting:

p.Legend.Top = true
p.Legend.Left = true

where p is a plot.

thanks

fengjiansunren commented 3 years ago

You can also refer to the barchart_example_test.go file. Here's a snippet from there:

`// Now, make a different type of BarChart. groupA := plotter.Values{20, 35, 30, 35, 27} groupB := plotter.Values{25, 32, 34, 20, 25} groupC := plotter.Values{12, 28, 15, 21, 8} groupD := plotter.Values{30, 42, 6, 9, 12}

p, err := plot.New()
if err != nil {
  log.Panic(err)
}
p.Title.Text = "Bar chart"
p.Y.Label.Text = "Heights"

w := vg.Points(8)

barsA, err := plotter.NewBarChart(groupA, w)
if err != nil {
  log.Panic(err)
}
barsA.Color = color.RGBA{R: 255, A: 255}
barsA.Offset = -w / 2

barsB, err := plotter.NewBarChart(groupB, w)
if err != nil {
  log.Panic(err)
}
barsB.Color = color.RGBA{R: 196, G: 196, A: 255}
barsB.Offset = w / 2

barsC, err := plotter.NewBarChart(groupC, w)
if err != nil {
  log.Panic(err)
}
barsC.XMin = 6
barsC.Color = color.RGBA{B: 255, A: 255}
barsC.Offset = -w / 2

barsD, err := plotter.NewBarChart(groupD, w)
if err != nil {
  log.Panic(err)
}
barsD.Color = color.RGBA{B: 255, R: 255, A: 255}
barsD.XMin = 6
barsD.Offset = w / 2

p.Add(barsA, barsB, barsC, barsD)
p.Legend.Add("A", barsA)
p.Legend.Add("B", barsB)
p.Legend.Add("C", barsC)
p.Legend.Add("D", barsD)
**p.Legend.Top = true**
p.NominalX("Zero", "One", "Two", "Three", "Four", "",
  "Six", "Seven", "Eight", "Nine", "Ten")

p.Add(plotter.NewGlyphBoxes())
err = p.Save(300, 250, "testdata/barChart2.png")
if err != nil {
  log.Panic(err)
}

This code won't compile as its a snippet. However it shows how the legend is set.`

thanks

sbinet commented 3 years ago

closing as fixed by https://github.com/gonum/plot/issues/657#issuecomment-751710856