英文:
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)
}
输出的图像如下所示:
英文:
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:
答案2
得分: 1
似乎可以通过使用y轴的属性Scale
来实现这一点,具体代码如下:
p, err := plot.New()
p.Y.Min = 0.001
p.Y.Max = 100
p.Y.Scale = LogScale
参考链接:
- http://godoc.org/code.google.com/p/plotinum/plot#LogScale
- http://godoc.org/code.google.com/p/plotinum/plot#Axis
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论