使用plotinum绘制具有对数刻度y轴的数字

时间:2014-09-10 21:29:20

标签: go

是否有人使用plotinum绘制具有对数刻度y轴的数字?

我在plotinum wiki中找不到这样的例子:https://code.google.com/p/plotinum/wiki/Examples

2 个答案:

答案 0 :(得分:2)

根据AlexAtNet的答案,我可以用对数刻度的y轴绘制直方图(以及其他图)。

我在这里分享了我的代码段,因为如果任何数据点的y == 0,plot.LogScale会发生恐慌,但实际数据可能难以避免。我的解决方案只是实现我自己的LogScale

func plotHist(data plotter.Values, title, xLabel, yLabel, imageFile string) {
    log.Printf("Plotting to %s ...", imageFile)

    p, e := plot.New()
    if e != nil {
        log.Fatalf("plot.New failed: %v", e)
    }

    h, e := plotter.NewHist(data, 50)
    if e != nil {
        log.Fatalf("plotter.NewHist failed: %v", e)
    }
    p.Add(h)

    p.Title.Text = title
    p.X.Label.Text = xLabel
    p.Y.Label.Text = yLabel
    p.Y.Min = 1
    _, _, _, p.Y.Max = h.DataRange()
    p.Y.Scale = LogScale
    p.Y.Tick.Marker = plot.LogTicks
    p.Add(plotter.NewGrid())

    if e := p.Save(9, 6, imageFile); e != nil {
        log.Fatalf("Cannot save image to %s: %v", imageFile, e)
    }

    log.Printf("Done plotting to %s.", imageFile)
}

func LogScale(min, max, x float64) float64 {
    logMin := ln(min)
    return (ln(x) - logMin) / (ln(max) - logMin)
}

func ln(x float64) float64 {
    if x <= 0 {
        x = 0.01
    }
    return math.Log(x)
}

输出图像如下所示:

enter image description here

答案 1 :(得分:1)

似乎适当的方法是使用y轴的属性Scale,如下所示:

p, err := plot.New()
p.Y.Min = 0.001
p.Y.Max = 100
p.Y.Scale = LogScale

另见: