gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:34:25 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

关于go - 使用 plotinum 绘制具有对数刻度 y 轴的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25775397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com