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

huangapple go评论76阅读模式
英文:

Use plotinum to draw figures with log-scale y-axis

问题

有人有使用plotinum绘制具有对数刻度y轴的图形的经验吗?

我在plotinum的维基页面上没有找到这样的示例:https://code.google.com/p/plotinum/wiki/Examples

英文:

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

答案1

得分: 2

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

我在这里分享我的代码片段,因为如果任何数据点的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)
}

输出的图像如下所示:

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

英文:

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(&quot;Plotting to %s ...&quot;, imageFile)

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

	h, e := plotter.NewHist(data, 50)
	if e != nil {
		log.Fatalf(&quot;plotter.NewHist failed: %v&quot;, 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(&quot;Cannot save image to %s: %v&quot;, imageFile, e)
	}

	log.Printf(&quot;Done plotting to %s.&quot;, 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 &lt;= 0 {
		x = 0.01
	}
	return math.Log(x)
}

An output image looks like this:

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

答案2

得分: 1

似乎可以通过使用y轴的属性Scale来实现这一点,具体代码如下:

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

参考链接:

英文:

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:

huangapple
  • 本文由 发表于 2014年9月11日 05:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/25775397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定