2014-09-10 92 views

回答

2

基于AlexAtNet的回答,我可以绘制直方图(以及其他图)与y轴对数刻度。

我在这里分享我的代码片段,因为plot.LogScale恐慌如果任何数据点有y == 0,但是,这可能很难避免真正的数据。我的解决方案是简单地实施我自己的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

相关问题