A Versatile Golang Plotting Library (plotlib-go) Developed
December 27, 2022 ◦ 1 min ◦
Motivative
The Go Programming Language is much faster than Python, and its syntax is modern. I became familiar with Go by implementing the acoustic array beamforming modules. Initially, the results were exported to plot in Python, but it was inconvenient. Therefore, finding a way to directly plot the high-quality figures in Go could improve the development efficiency later.
Plot in Go
The plotlib is developed on the top of gonum/plot. The marker defined in other plotting libraries, e.g., matplotlib, is named points in gonum. To implement a function similar to imshow, we need to use heatmap.
The interface between the plotlib and the main function
- Line plotting struct
// Line structure
type Line struct {
P *plot.Plot
XYs []plotter.XYs // XY data
Width float64
Height float64
Color []string
Xlabel string
Ylabel string
Title string
Figname string
GridOn bool
PointsOn []bool // add points/markers
LineWidth []float64
Ticks []float64
Legend []string
LegendPos struct{ Left, Top bool } // legned position
}
- Plotting
var plt plotlib.Plt = &line
plt.AddDataXY(&x_data, &y_data)
plt.Plot()
- Save figure
plt.SaveFig()
Result
- Line and point/marker plotting
Narrowband beamforming of a 16-element array was simulated, and the resulting beampatterns for two different windowing functions are compared as follows:
- Scatter plotting Iris flower data set was used to test the Golang machine learning performance. The data feature distribution was visualized with scatter plotting: