Golang繪制數(shù)列趨勢圖的操作步驟
要在Go語言中繪制數(shù)列的趨勢圖,遵循下面的步驟:
步驟1:安裝繪圖庫
首先,需要安裝Gonum繪圖庫。在終端中運行以下命令來獲取和安裝庫:
go get -u gonum.org/v1/plot/...
確保工作環(huán)境已配置GOPATH
,并且$GOPATH/bin
已添加到PATH中。
步驟2:準備數(shù)列數(shù)據(jù)
確保數(shù)列數(shù)據(jù)以某種方式可獲得,例如存儲在文本文件中(每行一個數(shù)值)。例如,數(shù)據(jù)文件(data.txt)可能看起來是這樣的:
12 23 12 34 23 24
步驟3:編寫Go程序
創(chuàng)建一個新的Go文件,比如 main.go,編寫以下程序來讀取數(shù)據(jù)和生成趨勢圖。
package main import ( "bufio" "log" "os" "strconv" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" "gonum.org/v1/plot/vg" ) func main() { // 打開并讀取數(shù)據(jù)文件 f, err := os.Open("data.txt") if err != nil { log.Fatal(err) } defer f.Close() scanner := bufio.NewScanner(f) points := make(plotter.XYs, 0) i := 0.0 for scanner.Scan() { y, err := strconv.ParseFloat(scanner.Text(), 64) if err != nil { log.Fatalf("error parsing value: %v", err) } points = append(points, plotter.XY{X: i, Y: y}) i++ } if err := scanner.Err(); err != nil { log.Fatal(err) } // 創(chuàng)建圖表 p := plot.New() p.Title.Text = "數(shù)列趨勢圖" p.X.Label.Text = "序號" p.Y.Label.Text = "數(shù)值" // 添加線條 line, err := plotter.NewLine(points) if err != nil { log.Fatal(err) } p.Add(line) // 保存圖表 if err := p.Save(10*vg.Inch, 4*vg.Inch, "trend.png"); err != nil { log.Fatal(err) } }
步驟4:編譯并運行程序
在終端中,編譯Go文件:
go build main.go
運行編譯后的程序:
./main
程序將會生成一個名為 trend.png 的趨勢圖圖片文件:
補充說明:
- 如果數(shù)列很大(如13萬行),可能需要調(diào)整程序以適應這件量級,可能包括內(nèi)存管理和圖形的分辨率等方面。
- 在繪圖時,可以自定義圖形的樣式,比如線條寬度、顏色和圖表的尺寸等。
- 上述代碼示例考慮到了數(shù)據(jù)可能解析出錯的情況,并相應地處理了錯誤。
按照上述步驟,能夠使用Go語言和Gonum繪圖庫來繪制數(shù)列趨勢圖了。
拓展
Golang 在圖像中繪制矩形框
從獲取的坐標信息,在圖像中繪制矩形框,并添加標注信息。
1. 依賴
- 字體 simsun.ttc
- 第三方庫 github.com/fogleman/gg
2. 源碼
package main import ( "encoding/json" "fmt" "image" "image/color" "image/draw" "log" "math" "reflect" "strings" "github.com/fogleman/gg" ) // test_draw_rect_text 畫圖像矩形, 標注, 顏色; 返回保存圖像路徑 // ref: https://github.com/fogleman/gg/blob/master/examples/rotated-image.go func test_draw_rect_text(im_path, font_path, detect_label, save_path string, x, y, w, h float64) { // Load image im, err := gg.LoadImage(im_path) if err != nil { log.Fatal(err) } // 1 method // iw, ih := im.Bounds().Dx(), im.Bounds().Dy() // Set Context // dc := gg.NewContext(iw, ih) // Draw image // dc.DrawImage(im, 0, 0) // 2 method dc := gg.NewContextForImage(im) // Set color and line width dc.SetHexColor("#FF0000") dc.SetLineWidth(1) // Draw rectangle dc.DrawRoundedRectangle(x, y, w, h, 0) // Store set dc.Stroke() // Set font and draw label var font_height float64 = 7 if err := dc.LoadFontFace(font_path, font_height); err != nil { panic(err) } rect_center_x := x + w/2 rect_center_y := y + h/2 dc.DrawStringAnchored(detect_label, rect_center_x, rect_center_y, 0.5, 0.5) dc.Clip() // Save png image dc.SavePNG(save_path) } func main() { im_path := "/home/tianzx/Pictures/lena.jpeg" font_path := "/home/tianzx/ai_model/simsun.ttc" detect_label := "缺角/碎裂" save_path := "/home/tianzx/Pictures/lena_test.png" var x, y, w, h float64 = 50, 100, 50, 50 test_draw_rect_text(im_path, font_path, detect_label, save_path, x, y, w, h) }
3. 結果
到此這篇關于Golang繪制數(shù)列趨勢圖的操作步驟的文章就介紹到這了,更多相關Golang繪制趨勢圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
golang操作連接數(shù)據(jù)庫實現(xiàn)mysql事務示例
這篇文章主要為大家介紹了golang操作連接數(shù)據(jù)庫實現(xiàn)mysql事務示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04Golang?throttled基于GCRA速率限制庫使用探索
這篇文章主要為大家介紹了Golang?throttled基于GCRA速率限制庫使用實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01