有没有人有过使用plotinum绘制具有对数刻度y轴的图形的经验?使用plotinum绘制具有对数刻度y轴的图形
我不觉得plotinum维基这样的例子:https://code.google.com/p/plotinum/wiki/Examples
有没有人有过使用plotinum绘制具有对数刻度y轴的图形的经验?使用plotinum绘制具有对数刻度y轴的图形
我不觉得plotinum维基这样的例子:https://code.google.com/p/plotinum/wiki/Examples
看来,对于以适当的方式如下:使用y轴的特性Scale
:
p, err := plot.New()
p.Y.Min = 0.001
p.Y.Max = 100
p.Y.Scale = LogScale
参见:
基于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)
}
输出图像看起来是这样的:
真棒!亚历克斯你是怎么弄明白的? :-) – cxwangyi 2014-09-15 02:24:06
我正在开发软件已经16年了:)这很有趣。 – 2014-09-15 02:54:57