首页 > 编程知识 正文

goxb(折线)

时间:2023-05-03 06:38:17 阅读:88357 作者:3494

Gox语言中嵌入了有名的gonum/plot软件包,其作用与Python的Matplotlib库相似,可以辅助科学计算中的图表绘制,非常方便。 有关此包的更详细的文档,请参见此处。

基本折线图绘制

最基本的折线图示例如下所示。

导入绘图软件包

plot=import('plot ' )

//创建绘图上下文环境

p,_=plot.New (

//设定图表的标题

p.title.text='编号出图示例'

//设定图标x、y坐标的文字

p.X.Label.Text='X '

p.Y.Label.Text='Y '

//创建要绘制的坐标点集

points=make(plot.xys ) )。

//创建并分配第一个点的x、y坐标

points [0]=make (出图. xy )

点[0].x=0. 0

点[0].y=0. 0

//用简单的方法制作第2、3、4个要点

points [1]=plot.newxy (1. 0,1.0 ) ) ) ) ) ) ) ) ) ) ) ) ) )。

points [2]=plot.newxy (2. 0,4.0 ) ) ) ) ) ) ) ) ) ) ) ) ) )。

points [3]=plot.newxy (3. 0,9.0 ) ) ) ) ) ) ) ) ) ) ) ) ) ) )。

//将这些点添加到图表中,并为相应的曲线设置图例名称

plot.addlinepoints(p,' y=x * x ',点) )。

//将折线图保存为4英寸见方大小的图像points.png,

必须验证//c :测试目录是否已经存在

4 *图片信息,4 *图片信息,c :

运行程序后,可以在c驱动器的test目录下找到points.png图像文件。 打开后,您会看到如下所示的图表。

可以看到简单的折线图画得很好。

将绘制好的图形展示在界面上

下面是用简单的界面绘制的图。

plot=import('plot ' )

p,_=plot.New (

p.title.text='编号出图示例'

p.X.Label.Text='X '

p.Y.Label.Text='Y '

points=make(plot.xys ) )。

points [0]=make (出图. xy )

点[0].x=0. 0

点[0].y=0. 0

points [1]=plot.newxy (1. 0,1.0 ) ) ) ) ) ) ) ) ) ) ) ) ) )。

points [2]=plot.newxy (2. 0,4.0 ) ) ) ) ) ) ) ) ) ) ) ) ) )。

points [3]=plot.newxy (3. 0,9.0 ) ) ) ) ) ) ) ) ) ) ) ) ) ) )。

plot.addlinepoints(p,' y=x * x ',点) )。

4 *图片信息,4 *图片信息,c :

var GUI=导入(GUI ) ) )。

函数on按钮() {

exit () )

}

功能循环()

layoutt=GUI .构件

GUI.imagewithfile ` c : 测试点. png `,300,300 ),

GUI.button (“关闭”,点击按钮)、

}

GUI .单窗口(“打印诊断程序”,布局)。

}

主窗口=GUI.newmasterwindow ('绘图诊断程序',400,400,GUI.masterwindowflagsnotresizable,nil ) )。

GUI .循环窗口(主窗口,循环)。

通过将保存的折线图加载到界面的ImageWithFile控件中,可以获得以下效果:

这样,我们就完全控制了从图像生成到展示的整个过程。

虽然是

从内存中加载图片并展示

,但上面的例子是以保存的png图像文件为中继加载图像。 如果要直接加载内存中的图像,可以。

plot=import('plot ' )

p,_=plot.New (

p.title.text='编号出图示例'

p.X.Label.Text='X '

p.Y.Label.Text='

Y" points = make(plot.XYs) points[0] = make(plot.XY) points[0].X = 0.0 points[0].Y = 0.0 points[1] = plot.NewXY(1.0, 1.0) points[2] = plot.NewXY(2.0, 4.0) points[3] = plot.NewXY(3.0, 9.0) plot.AddLinePoints(p, "y = x * x", points) image = import("image") rgbaT, errT = image.LoadPlotImage(p, 4*plot.Inch, 4*plot.Inch) checkError(errT, nil) p.Save(4*plot.Inch, 4*plot.Inch, `c:testpoints.png`) var gui = import("gui") func onButtonClick() { exit() } textureT = nil flagT = 0 f = func() { if flagT != 0 { return } flagT = 1 textureT, errT = gui.NewTextureFromRgba(rgbaT) if errT != nil { plerr(errT) return } } func loop() { go f() layoutT = []gui.Widget{ gui.ImageWithFile(`c:testpoints.png`, 300, 300), gui.Custom(func() { if textureT != nil { gui.Image(textureT, 348, 348).Build() } }), gui.Button("Close", onButtonClick), } gui.SingleWindow("Plot Diagram", layoutT) } mainWindow = gui.NewMasterWindow("Plot Diagram", 400, 750, gui.MasterWindowFlagsNotResizable, nil) gui.LoopWindow(mainWindow, loop)

运行的结果如下:

第二张图片即是直接从内存中加载的图片,注意其尺寸设置与gui.ImageWithFile的有所不同,另外,加载图片所需的NewTextureFromRGBA必须要在线程中启动,这是受imgui所限制的。

用LCL库展示图片

上面同样的功能用LCL库实现图形界面的代码如下,更适合传统的编程思维一些。

plot = import("plot") p, _ = plot.New() p.Title.Text = "Gonum Plot Example" p.X.Label.Text = "X" p.Y.Label.Text = "Y" points = make(plot.XYs) points[0] = make(plot.XY) points[0].X = 0.0 points[0].Y = 0.0 points[1] = plot.NewXY(1.0, 1.0) points[2] = plot.NewXY(2.0, 4.0) points[3] = plot.NewXY(3.0, 9.0) plot.AddLinePoints(p, "y = x * x", points) p.Save(4*plot.Inch, 4*plot.Inch, `c:testpoints.png`) lcl = import("lcl") os = import("os") errT = lcl.InitLCL() if errT != nil { plerr(errT) exit() } application = lcl.GetApplication() application.Initialize() application.SetTitle("Calculator with LCL") application.SetMainFormOnTaskBar(true) mainForm = application.CreateForm() mainForm.SetWidth(880) mainForm.SetHeight(480) mainForm.SetCaption("Calculator with LCL") mainForm.SetPosition(lcl.PoScreenCenter) mainForm.Font().SetSize(11) img1 = lcl.NewImage(mainForm) img1.SetBounds(10, 10, 400, 400) img1.SetParent(mainForm) img1.Picture().LoadFromFile(`c:testpoints.png`) img1.SetStretch(true) img1.SetProportional(true) imagetk = import("imagetk") itk = imagetk.NewImageTK() bufT, errT = itk.LoadPlotImageInMemory(p, 4*plot.Inch, 4*plot.Inch, "png") checkError(errT, nil) mem = lcl.NewMemoryStream() mem.Write(bufT.Bytes()) mem.SetPosition(0) img2 = lcl.NewImage(mainForm) img2.SetBounds(420, 10, 400, 400) img2.SetParent(mainForm) img2.Picture().LoadFromStream(mem) img2.SetStretch(true) img2.SetProportional(true) mem.Free() onButtonCloseClick = func(sender) { application.Terminate() } buttonClose = lcl.NewButton(mainForm) buttonClose.SetParent(mainForm) buttonClose.SetLeft(10) buttonClose.SetTop(420) buttonClose.SetCaption("Close") buttonClose.SetOnClick(&onButtonCloseClick) application.Run()

执行后的结果界面如下:

效果还是不错的,其中,左侧的图是通过图像文件进行中转显示的,右侧的图是直接通过内存中缓冲区中转显示的。

代码中用到了imagetk库,这是封装了一些便捷的图形处理相关的函数。

需要特别注意的是,由于LCL受VCL的影响发展而来,所以所有的对象理论上都应该释放,调用该对象的Free成员函数即可。之前很多的例子为了演示方便,并没有都进行资源释放。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。