dsfb20227
2014-09-10 21:29 阅读 22
已采纳

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

Does anyone have experience using plotinum to draw figures with log-scaled y-axis?

I do not find such examples in plotinum wiki: https://code.google.com/p/plotinum/wiki/Examples

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    drasv0904 drasv0904 2014-09-10 21:45

    It seems that an appropriate way for that is to use the property Scale of the y-axis as follows:

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

    See also:

    点赞 评论 复制链接分享
  • drrqwokuz71031449 drrqwokuz71031449 2014-09-15 02:30

    Based on the answer of AlexAtNet, I can draw histograms (as well as other plots) with y-axis in log-scale.

    I share my code snippet here, because plot.LogScale panics if any data point has y==0, which, however, might be hard to avoid with real data. My solution is simply implementing my own 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)
    }
    

    An output image looks like this:

    enter image description here

    点赞 评论 复制链接分享

相关推荐