多应用+插件架构,代码干净,支持一键云编译,码云点赞13K star,4.8-4.12 预售价格198元 广告
我们在做数据绘图或函数图像时经常需要使用对数坐标系。尤其是数据的范围跨越很多个数量级时,通常的线性坐标系下无法表现出数据特征。  Scilab 中Plot函数无法画出对数坐标。需要使用 plot2d 函数。  plot2d 函数的基本用法如下:   plot2d([logflag,][x,],y[,style[,strf[,leg[,rect[,nax]]]]]) plot2d([logflag,][x,],y,<opt_args>)   下面是一个简单的例子: ~~~ iter = linspace(0,10,11); err = 10.^(-iter); plot2d("nl", iter, err, style=2); set(gca(),"grid",[1 1]); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a4234437d63.jpg) 这个例子如果在普通的坐标系下看,是这个样子的: ~~~ plot(iter,err); set(gca(),"grid",[1 1]); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42344461ed.jpg) 由于数据很快就很接近0了,在图中很难看出后面的趋势。 下面来详细的讲解一下plot2d函数。 plot2d("nl", iter, err, style=2);  “nl” 表示,横坐标为正常的模式(normal),纵坐标为对数(log). Style = 2 表示的是曲线的颜色。2 表示的是colormap 中的第二项,也就是蓝色。 如果是负数,则表示用不同的线型。如果既要设置曲线的颜色,又要设置线型,那么。。。暂时还没搞定。 下面再给一个例子,通过rect参数控制xy的范围: ~~~ x=[0:0.1:2*%pi]'; plot2d(x,[sin(x) sin(2*x) sin(3*x)],rect=[0,0,6,0.5]); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a4234454376.jpg) 有点跑题了,接着介绍对数坐标系绘图。下面再给一个例子: ~~~ ind = linspace(0,6,7); iter = 10.^ind; err1 = 10.^(-ind); err2 = (10.^(-ind)).^2; xset("font size", 4); plot2d("ll", iter, err1, style=2); plot2d("ll", iter, err2, style=3); title("Loglog","fontsize", 4); xlabel("Iterations","fontsize", 4); ylabel("Error","fontsize", 4); set(gca(),"grid",[5 5]); legend(['error1';'error2'],"in_lower_left"); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42344647a8.jpg) 这个图是双对数坐标,同时还调整了图上的文字。需要注意的是: xset("font size", 4);   语句一定要在 legend(['error1';'error2'],"in_lower_left"); 语句之前调用,否则得到的图形的legend 的字号会有问题,下面是个实验,先执行如下语句: ~~~ ind = linspace(0,6,7); iter = 10.^ind; err1 = 10.^(-ind); err2 = (10.^(-ind)).^2; plot2d("ll", iter, err1, style=2); plot2d("ll", iter, err2, style=3); title("Loglog","fontsize", 4); xlabel("Iterations","fontsize", 4); ylabel("Error","fontsize", 4); set(gca(),"grid",[5 5]); legend(['error1';'error2'],"in_lower_left"); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a4234476f85.jpg) 这个图形是和我们的预期一样的。标题和Label的字号变大了,刻度和Legend的字号还是原来的大小。 接着执行: ~~~ xset("font size", 4); ~~~ ![](https://box.kancloud.cn/2016-01-24_56a42344844ca.jpg) 结果是刻度上的字号更新为正确的大小了,可legend 的字号没变。看来这个是 scilab 的一个bug。 所以我们需要先设置字号,然后调用legend 函数。