12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697# 1 "src/owl/misc/owl_plot.ml"(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2016-2018 Liang Wang <liang.wang@cl.cam.ac.uk>
*)(** Plot Module *)(* types in plot module *)typedsmat=Owl_dense_matrix.D.mattypecolor=RED|GREEN|BLUEtypelegend_typ=LINE|SCATTER|BOXtypelegend_position=North|South|West|East|NorthWest|NorthEast|SouthWest|SouthEasttypelegend_item={mutableplot_type:legend_typ;mutableline_style:int;mutableline_color:int*int*int;mutablemarker:string;mutablemarker_color:int*int*int;mutablefill_pattern:int;mutablefill_color:int*int*int;}typepage={mutabletitle:string;mutablefgcolor:int*int*int;mutablefontsize:float;mutableis_3d:bool;mutableno_axes:bool;(* control axis labels *)mutablexlabel:string;mutableylabel:string;mutablezlabel:string;(* control axis ranges *)mutablexrange:float*float;mutableyrange:float*float;mutablezrange:float*float;mutableauto_xrange:bool;mutableauto_yrange:bool;mutableauto_zrange:bool;(* control tick labels *)mutablexticklabels:(float*string)list;mutableyticklabels:(float*string)list;mutablezticklabels:(float*string)list;(* control axis log scale *)mutablexlogscale:bool;mutableylogscale:bool;(* control grids *)mutablexgrid:bool;mutableygrid:bool;mutablezgrid:bool;(* viewing perspective for 3D plots
* http://plplot.sourceforge.net/docbook-manual/plplot-html-5.12.0/plw3d.html *)mutablealtitude:float;mutableazimuth:float;(* control legend *)mutablelegend:bool;mutablelegend_position:legend_position;mutablelegend_items:legend_itemarray;mutablelegend_names:stringarray;(* cache the plot operations *)mutableplots:(unit->unit)array;}typehandle={mutableholdon:bool;mutableoutput:string;mutablebgcolor:int*int*int;mutablepensize:float;mutablepage_size:int*int;(* control the sub plots *)mutableshape:int*int;mutablepages:pagearray;mutablecurrent_page:int;}typeaxis=X|Y|Z|XY|XZ|YZ|XYZ(* Specification to configure a plot *)typespec=|RGBofint*int*int|LineStyleofint|LineWidthoffloat|Markerofstring|MarkerSizeoffloat|Fill|FillPatternofint|Contour|Altitudeoffloat|Azimuthoffloat|ZLineofaxis|NoMagColor|Curtain|Faceted|Axisofaxislet_get_rgbldefault_val=letl=l|>List.filter(functionRGB_->true|_->false)|>List.map(functionRGB(r,g,b)->(r,g,b)|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_line_styleldefault_val=letl=l|>List.filter(functionLineStyle_->true|_->false)|>List.map(functionLineStylex->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_line_widthldefault_val=letl=l|>List.filter(functionLineWidth_->true|_->false)|>List.map(functionLineWidthx->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_markerldefault_val=letl=l|>List.filter(functionMarker_->true|_->false)|>List.map(functionMarkerx->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_marker_sizeldefault_val=letl=l|>List.filter(functionMarkerSize_->true|_->false)|>List.map(functionMarkerSizex->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_fillldefault_val=letl=l|>List.filter(functionFill->true|_->false)|>List.map(functionFill->true|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_fill_patternldefault_val=letl=l|>List.filter(functionFillPattern_->true|_->false)|>List.map(functionFillPatternx->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_contourldefault_val=letl=l|>List.filter(functionContour->true|_->false)|>List.map(functionContour->true|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_altitudeldefault_val=letl=l|>List.filter(functionAltitude_->true|_->false)|>List.map(functionAltitudex->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_azimuthldefault_val=letl=l|>List.filter(functionAzimuth_->true|_->false)|>List.map(functionAzimuthx->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_zlineldefault_val=letl=l|>List.filter(functionZLine_->true|_->false)|>List.map(function|ZLineX->Plplot.PL_DRAW_LINEX|ZLineY->Plplot.PL_DRAW_LINEY|ZLineXY->Plplot.PL_DRAW_LINEXY|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_mag_colorldefault_val=letl=l|>List.filter(functionNoMagColor->true|_->false)|>List.map(functionNoMagColor->false|_->true)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_curtainldefault_val=letl=l|>List.filter(functionCurtain->true|_->false)|>List.map(functionCurtain->true|_->false)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_facetedldefault_val=letl=l|>List.filter(functionFaceted->true|_->false)|>List.map(functionFaceted->true|_->false)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)let_get_axisldefault_val=letl=l|>List.filter(functionAxis_->true|_->false)|>List.map(functionAxisx->x|_->default_val)inletk=List.lengthlinifk=0thendefault_valelseList.nthl(k-1)(* module functions to simplify plotting *)let_create_page()={title="";fgcolor=(255,0,0);fontsize=-1.;is_3d=false;no_axes=false;xlabel="x";ylabel="y";zlabel="z";xrange=(infinity,neg_infinity);yrange=(infinity,neg_infinity);zrange=(infinity,neg_infinity);auto_xrange=true;auto_yrange=true;auto_zrange=true;xticklabels=[];yticklabels=[];zticklabels=[];xlogscale=false;ylogscale=false;xgrid=false;ygrid=false;zgrid=false;altitude=33.;azimuth=45.;legend=false;legend_position=NorthEast;legend_items=[||];legend_names=[||];plots=[||];}let_create_handle()={holdon=true;output="";bgcolor=(0,0,0);pensize=0.;page_size=(0,0);shape=(1,1);current_page=0;pages=[|_create_page()|];}letcreate?(m=1)?(n=1)s=letpages=Array.make(m*n)None|>Array.map(fun_->_create_page())inleth=_create_handle()inh.shape<-(m,n);h.pages<-pages;h.output<-s;hlet_default_handle=leth=_create_handle()inh.holdon<-false;hlet_supported_device=["aqt";"xwin";"pdf";"ps";"psc";"png";"svg";"xfig";"psttf";"psttc";"xcairo";"pdfcairo";"epscairo";"pscairo";"svgcairo";"pngcairo";"memcairo";"extcairo"]let_set_deviceh=tryletx=Owl_utils.get_suffixh.outputinPlplot.plsdevx;Plplot.plsfnamh.output;with_exn->()let_add_legend_itempplot_typeline_styleline_colormarkermarker_colorfill_patternfill_color=letitem={plot_type=plot_type;line_style=line_style;line_color=line_color;marker=marker;marker_color=marker_color;fill_pattern=fill_pattern;fill_color=fill_color;}inp.legend_items<-Array.appendp.legend_items[|item|]let_plplot_positionpos=letopenPlplotinmatchposwith|North->[PL_POSITION_TOP]|South->[PL_POSITION_BOTTOM]|West->[PL_POSITION_LEFT]|East->[PL_POSITION_RIGHT]|NorthWest->[PL_POSITION_TOP;PL_POSITION_LEFT]|NorthEast->[PL_POSITION_TOP;PL_POSITION_RIGHT]|SouthWest->[PL_POSITION_BOTTOM;PL_POSITION_LEFT]|SouthEast->[PL_POSITION_BOTTOM;PL_POSITION_RIGHT]let_draw_legendp=letopenPlplotinlet_=plscmap0n64inletcbase=16inletopt=[PL_LEGEND_BOUNDING_BOX]inletposition=_plplot_positionp.legend_position@[PL_POSITION_INSIDE]inletopt_array=Array.map(funitem->matchitem.plot_typewith|LINE->[PL_LEGEND_LINE;PL_LEGEND_SYMBOL]|SCATTER->[PL_LEGEND_SYMBOL]|BOX->[PL_LEGEND_COLOR_BOX])p.legend_itemsinlettext_colors=Array.map(fun_->1)p.legend_itemsinlettext=Array.mapi(funi_->p.legend_names.(i))p.legend_itemsinletline_colors=Array.mapi(funix->letr,g,b=x.line_colorinplscol0(i+cbase)rgb;(i+cbase))p.legend_itemsinletline_styles=Array.map(funx->x.line_style)p.legend_itemsinletline_widths=Array.map(fun_->1.)p.legend_itemsinletmarker_colors=line_colorsinletmarker_scales=Array.map(fun_->1.)p.legend_itemsinletmarker_nums=Array.map(fun_->3)p.legend_itemsinletmarkers=Array.map(funx->x.marker)p.legend_itemsinletbox_colors=line_colorsinletbox_patterns=Array.map(funx->x.fill_pattern)p.legend_itemsinletbox_scales=Array.map(fun_x->0.8)p.legend_itemsinletbox_linewidths=Array.map(fun_x->1.)p.legend_itemsinlet_=pllegendoptposition0.050.050.1151100opt_array1.01.02.01.0text_colorstextbox_colorsbox_patternsbox_scalesbox_linewidthsline_colorsline_stylesline_widthsmarker_colorsmarker_scalesmarker_numsmarkersin()let_calculate_paper_sizemn=letmax_w,max_h=900.,900.inletr0=4./.3.inletcur_w,cur_h=r0*.(float_of_intn),float_of_intminletr1=max_w/.max_hinletr2=cur_w/.cur_hinletw,h=match(r1/.r2)<1.with|true->max_w,max_w/.r2|false->max_h*.r2,max_hinint_of_floatw,int_of_floath(* calculate the axis config based on a page config *)let_config_2d_axisp=letbase=0inifp.no_axesthen-1elseletresidual=if(p.xlogscale,p.ylogscale)=(true,false)then10elseif(p.xlogscale,p.ylogscale)=(false,true)then20elseif(p.xlogscale,p.ylogscale)=(true,true)then30elseifp.xticklabels|>List.length>0||p.yticklabels|>List.length>0then70else0inbase+residuallet_initialiseh=letopenPlplotin(* configure before init *)_set_deviceh;letr,g,b=h.bgcolorinplscolbgrgb;(* init the plot *)letm,n=h.shapeinifnot(h.shape=(1,1))thenplssubnm;letx,y=matchh.page_size=(0,0)with|true->_calculate_paper_sizemn|false->h.page_sizeinplspage0.0.xy00;plinit();(* configure after init *)plwidthh.pensize(* callback function of drawing customised tick labels *)let_draw_ticklabelspaxisvalue=letopenPlplotinletl=matchaxiswith|PL_X_AXIS->p.xticklabels|PL_Y_AXIS->p.yticklabels|PL_Z_AXIS->p.zticklabelsintryList.assocvaluelwith_exn->Printf.sprintf"%g"valuelet_prepare_pagep=letopenPlplotin(* customise tick labels if necessary *)plslabelfunc(_draw_ticklabelsp);(* configure an individual page *)letr,g,b=p.fgcolorinplscol02rgb;plcol02;ifp.fontsize>0.thenplschrp.fontsize1.0;letxmin,xmax=p.xrangeinletymin,ymax=p.yrangeinletzmin,zmax=p.zrangeinletalt,az=p.altitude,p.azimuthinifnotp.is_3dthen((* prepare a 2D plot *)plenvxminxmaxyminymax0(_config_2d_axisp))else((* prepare a 3D plot *)pladv0;plvpor0.01.00.00.9;plwind(-1.0)1.0(-1.0)1.5;plw3d1.01.01.2xminxmaxyminymaxzminzmaxaltaz;plbox3"bntu"p.xlabel0.00"bntu"p.ylabel0.00"bcdfntu"p.zlabel0.04);(* set x-label, y-label, and title *)pllabp.xlabelp.ylabelp.title;(* reset foreground colour to index 1 *)plcol01;ifp.legendthen_draw_legendplet_finalise()=(* play safe, reset pages in default_handle *)_default_handle.pages<-[|_create_page()|];Plplot.plend()letoutputh=h.holdon<-false;_initialiseh;Array.iteri(funip->ifi>0thenPlplot.pladvi;_prepare_pagep;Array.iter(funf->f())p.plots)h.pages;_finalise()letset_outpuths=letx=Owl_utils.get_suffixsinmatch(List.memx_supported_device)with|true->h.output<-s|false->Owl_log.error"unsupported file type."letget_outputh=h.outputletset_titlehs=(h.pages.(h.current_page)).title<-sletset_xlabelhs=(h.pages.(h.current_page)).xlabel<-sletset_ylabelhs=(h.pages.(h.current_page)).ylabel<-sletset_zlabelhs=(h.pages.(h.current_page)).zlabel<-sletset_xrangehab=(h.pages.(h.current_page)).auto_xrange<-false;(h.pages.(h.current_page)).xrange<-(a,b)letset_yrangehab=(h.pages.(h.current_page)).auto_yrange<-false;(h.pages.(h.current_page)).yrange<-(a,b)letset_zrangehab=(h.pages.(h.current_page)).auto_zrange<-false;(h.pages.(h.current_page)).zrange<-(a,b)letset_xticklabelshl=(h.pages.(h.current_page)).xticklabels<-lletset_yticklabelshl=(h.pages.(h.current_page)).yticklabels<-lletset_zticklabelshl=(h.pages.(h.current_page)).zticklabels<-lletset_foreground_colorhrgb=(h.pages.(h.current_page)).fgcolor<-(r,g,b)letset_background_colorhrgb=h.bgcolor<-(r,g,b)letset_font_sizehx=(h.pages.(h.current_page)).fontsize<-xletset_pen_sizehx=h.pensize<-xletset_page_sizehxy=h.page_size<-(x,y)letlegend_onh?(position=NorthEast)s=(h.pages.(h.current_page)).legend<-true;(h.pages.(h.current_page)).legend_position<-position;(h.pages.(h.current_page)).legend_names<-sletlegend_offh=(h.pages.(h.current_page)).legend<-false(* TODO *)letrgb=None[@@warning"-32"](*FIXME: plptex3 to write text inside the viewport of a 3D plot*)lettext?(h=_default_handle)?(spec=[])xy?(dx=0.)?(dy=0.)s=letopenPlplotin(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorin(* drawing function *)letf=(fun()->(*save original color index*)letr',g',b'=plgcol01inplscol01rgb;plcol01;plptexxydxdy0.s;(* restore original settings *)plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhlet_thinningx=letl=float_of_int(Array.lengthx)inletn=ifl<16.thenlelse16.inletc=float_of_int(Array.lengthx)/.ninArray.init(int_of_floatn)(funi->x.(int_of_float(float_of_inti*.c)))let_union_rangeprx=leta,b=rinletm,n=Owl_stats.minmaxxinletc=ifa<mthenaelseminletd=ifb>nthenbelseninlete=(d-.c)*.pinc-.e,d+.elet_adjust_range?(margin=0.)hdaxis=letp=h.pages.(h.current_page)inmatchaxiswith|X->ifp.auto_xrangethenp.xrange<-_union_rangemarginp.xranged|Y->ifp.auto_yrangethenp.yrange<-_union_rangemarginp.yranged|Z->ifp.auto_zrangethenp.zrange<-_union_rangemarginp.zranged|_->failwith"owl_plot:_adjust_range"letplot?(h=_default_handle)?(spec=[])xy=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyin_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec""inletmarker_size=_get_marker_sizespec4.inletline_style=_get_line_stylespec1inletline_width=_get_line_widthspec(-1.)inletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;letc'=plgchr()|>fstinplschrmarker_size1.;ifline_style>0&&line_style<9thenpllstyline_style;pllinexy;ifmarker<>""then(letx',y'=_thinningx,_thinningyinplstringx'y'marker);(* restore original settings *)plschrc'1.;plwidthold_pensize;pllsty1;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempLINEline_stylecolormarkercolor0color;ifnoth.holdonthenoutputhletplot_fun?(h=_default_handle)?(spec=[])fab=letx=Owl_dense_matrix.D.linspaceab100inlety=Owl_dense_matrix.D.mapfxinplot~h~specxyletscatter?(h=_default_handle)?(spec=[])xy=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyin_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec"•"inletmarker_size=_get_marker_sizespec4.in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;letc'=plgchr()|>fstinplschrmarker_size1.;plstringxymarker;(* restore original settings *)plschrc'1.;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempSCATTER0colormarkercolor0color;ifnoth.holdonthenoutputhlethistogram?(h=_default_handle)?(spec=[])?(bin=10)x=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxin_adjust_rangehxX;letxmin,xmax=Owl_stats.minmaxxinletymin,ymax=0.,Owl_stats.((histogram(`Nbin)x).counts|>Array.mapfloat_of_int|>max)*.1.1in_adjust_rangeh[|xmin;xmax|]X;_adjust_rangeh[|ymin;ymax|]Y;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorin(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inlet_=plscol01rgb;plcol01inplhistxxminxmaxbin[PL_HIST_DEFAULT;PL_HIST_NOSCALING;PL_HIST_NOEXPAND];(* restore original settings *)plscol01r'g'b';plcol01;)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempBOX1color""color0color;ifnoth.holdonthenoutputhletsubplothij=let_,n=h.shapeinh.current_page<-(n*i+j)letstem?(h=_default_handle)?(spec=[])xy=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyin_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec"#[0x2299]"inletmarker_size=_get_marker_sizespec4.inletline_style=_get_line_stylespec2inletline_width=_get_line_widthspec(-1.)inletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;letc'=plgchr()|>fstinplschrmarker_size1.;ifline_style>0&&line_style<9then(pllstyline_style;Owl_utils.Array.iter2(funx'y'->pljoinx'0.x'y')xy);ifnot(marker="")thenplstringxymarker;(* restore original settings *)plschrc'1.;plwidthold_pensize;pllsty1;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempLINEline_stylecolormarkercolor0color;ifnoth.holdonthenoutputhletautocorr?(h=_default_handle)?(spec=[])x=letz=Owl_dense_matrix.D.to_arrayxinletx'=Array.init(Array.lengthz)(funi->float_of_inti)inlety'=Array.mapi(funi_->Owl_stats.autocorrelation~lag:iz)x'inletx'=Owl_dense_matrix.D.of_arrays[|x'|]inlety'=Owl_dense_matrix.D.of_arrays[|y'|]inset_xlabelh"Lag";set_ylabelh"Autocorrelation";(* prepare the closure *)letp=h.pages.(h.current_page)inletr,g,b=_get_rgbspecp.fgcolorinletcolor=RGB(r,g,b)inletmarker=Marker(_get_markerspec"•")inletmarker_size=MarkerSize(_get_marker_sizespec4.)inletline_style=LineStyle(_get_line_stylespec1)inletline_width=LineWidth(_get_line_widthspec(-1.))inletspec=[color;marker;marker_size;line_style;line_width]in(* drawing function *)stem~h~specx'y'letdraw_line?(h=_default_handle)?(spec=[])x0y0x1y1=letopenPlplotinletx=[|x0;x1|]inlety=[|y0;y1|]in_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletline_width=_get_line_widthspec(-1.)inletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;ifline_style>0&&line_style<9then(pllstyline_style;pljoinx0y0x1y1);(* restore original settings *)plwidthold_pensize;pllsty1;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputh(* TODO *)letplot_multi=None[@@warning"-32"]let_draw_error_bar?(w=0.)xye=letopenPlplotinletw=w/.2.in(* draw vertical line *)letx'=[|x;x|]inlety'=[|y-.e;y+.e|]inpllinex'y';(* draw upper bar *)letx'=[|x-.w;x+.w|]inlety'=[|y+.e;y+.e|]inpllinex'y';(* draw lower line *)letx'=[|x-.w;x+.w|]inlety'=[|y-.e;y-.e|]inpllinex'y'leterror_bar?(h=_default_handle)?(spec=[])xye=letopenPlplotinletymin=Owl_dense_matrix.D.(min'(y-e))inletymax=Owl_dense_matrix.D.(max'(y+e))inletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyinlete=Owl_dense_matrix.D.to_arrayein_adjust_rangehxX;_adjust_rangeh[|ymin;ymax|]Y;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletline_width=_get_line_widthspec(-1.)inletold_pensize=h.pensizeinletw=leta,b=Owl_stats.minmaxxin(a-.b)*.0.02in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;pllstyline_style;Owl_utils.Array.iter3(funx0y0e0->_draw_error_bar~wx0y0e0)xye;(* restore original settings *)plwidthold_pensize;pllsty1;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhlet_draw_whiskers_boxwxy=letopenPlplotinletymed,y1st,y3rd=Owl_stats.(mediany,first_quartiley,third_quartiley)inletw=w/.2.inletx'=[|x-.w;x-.w;x+.w;x+.w;x-.w|]inlety'=[|y1st;y3rd;y3rd;y1st;y1st|]inpllsty1;pllinex'y';letx'=[|x-.w;x+.w|]inlety'=[|ymed;ymed|]inpllsty1;pllinex'y';letymin,ymax=Owl_stats.minmaxyinletx'=[|x;x|]inlety'=[|ymin;y1st|]inpllsty1;pllinex'y';letx'=[|x;x|]inlety'=[|y3rd;ymax|]inpllsty1;pllinex'y'letboxplot?(h=_default_handle)?(spec=[])y=letopenPlplotinletm,_=Owl_dense_matrix.D.shapeyinletx=Array.initm(funi->float_of_inti+.1.)inletxmin,xmax=Owl_stats.minmaxxinletw=0.4inlety0=Owl_dense_matrix.D.to_arrayyinlety1=Owl_dense_matrix.D.to_arraysyin_adjust_rangeh[|xmin-.w;xmax+.w|]X;_adjust_rangeh~margin:0.1y0Y;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorin(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;Owl_utils.Array.iter2(funx'y'->_draw_whiskers_boxwx'y')xy1;(* restore original settings *)plscol01r'g'b';plcol01;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhlet_draw_barwx0y0=letopenPlplotinletx=[|x0-.w;x0-.w;x0+.w;x0+.w|]inlety=[|0.;y0;y0;0.|]inplfillxy;pllsty1;pllinexyletdraw_rect?(h=_default_handle)?(spec=[])x0y0x1y1=letopenPlplotinletx=[|x0;x0;x1;x1|]inlety=[|y1;y0;y0;y1|]in_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletfill_pattern=_get_fill_patternspec0in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;pllstyline_style;plpstyfill_pattern;plfillxy;(* restore original settings *)plscol01r'g'b';plcol01;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletbar?(h=_default_handle)?(spec=[])y=letopenPlplotinletw=0.4inlety=Owl_dense_matrix.D.to_arrayyinletx=Array.mapi(funi_->float_of_inti+.1.)yinletxmin,xmax=Owl_stats.minmaxxin_adjust_rangeh[|xmin-.w;xmax+.w|]X;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletfill_pattern=_get_fill_patternspec0in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;pllstyline_style;plpstyfill_pattern;Owl_utils.Array.iter2(funx0y0->_draw_barwx0y0)xy;(* restore original settings *)plscol01r'g'b';plcol01;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempBOXline_stylecolor""colorfill_patterncolor;ifnoth.holdonthenoutputhletarea?(h=_default_handle)?(spec=[])xy=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyinletxmin,xmax=Owl_stats.minmaxxinletx=Array.(append(append[|xmin|]x)[|xmax|])inlety=Array.(append(append[|0.|]y)[|0.|])in_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletfill_pattern=_get_fill_patternspec0in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;pllstyline_style;pllinexy;plpstyfill_pattern;plfillxy;(* restore original settings *)plscol01r'g'b';plcol01;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempBOXline_stylecolor""colorfill_patterncolor;ifnoth.holdonthenoutputhletdraw_polygon?(h=_default_handle)?(spec=[])xy=letopenPlplotinletx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyin_adjust_rangehxX;_adjust_rangehyY;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletfill_pattern=_get_fill_patternspec0in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;pllstyline_style;pllinexy;plpstyfill_pattern;plfillxy;(* restore original settings *)plscol01r'g'b';plcol01;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempBOXline_stylecolor""colorfill_patterncolor;ifnoth.holdonthenoutputhlet_ecdf_interleavexi=letm=Array.lengthxinletn=2*minlety=Array.maken0.inArray.iteri(funjz->letk=2*j+iinifk<ntheny.(k)<-z;ifk<n-1theny.(k+1)<-z)x;yletecdf?(h=_default_handle)?(spec=[])x=letx0=Owl_dense_matrix.D.to_arrayxinletx,y=Owl_stats.ecdfx0inletx=_ecdf_interleavex0inlety=_ecdf_interleavey1inletn=Array.lengthxinletx=Owl_dense_matrix.D.of_arrayxn1inlety=Owl_dense_matrix.D.of_arrayyn1inplot~h~specxyletstairs?(h=_default_handle)?(spec=[])xy=letx=Owl_dense_matrix.D.to_arrayxinlety=Owl_dense_matrix.D.to_arrayyinletx=_ecdf_interleavex0inleta=y.(0)inlety=_ecdf_interleavey1inlet_=y.(0)<-ainletn=Array.lengthxinletx=Owl_dense_matrix.D.of_arrayxn1inlety=Owl_dense_matrix.D.of_arrayyn1inplot~h~specxyletdraw_circle?(h=_default_handle)?(spec=[])xyrr=letopenPlplotinletn=1000inlettheta=(2.*.Owl_const.pi)/.(float_of_intn)inletx'=Array.init(n+1)(funi->x+.Owl_maths.(sin(float_of_inti*.theta))*.rr)inlety'=Array.init(n+1)(funi->y+.Owl_maths.(cos(float_of_inti*.theta))*.rr)in_adjust_rangeh~margin:0.05x'X;_adjust_rangeh~margin:0.05y'Y;(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletline_style=_get_line_stylespec1inletline_width=_get_line_widthspec(-1.)inletfill_pattern=_get_fill_patternspec0inletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;pllstyline_style;plpstyfill_pattern;plfillx'y';pllinex'y';(* restore original settings *)plscol01r'g'b';plcol01;plwidthold_pensize;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhlet_draw_arc_fillnx=letopenPlplotinleta=2.*.Owl_const.piinlettheta=a/.ninleti=ref0.inArray.iter(funy->letc=n*.yinletx'=Array.init(int_of_floatc+1)(funj->Owl_maths.(sin((float_of_intj+.!i)*.theta)))inlety'=Array.init(int_of_floatc+1)(funj->Owl_maths.(cos((float_of_intj+.!i)*.theta)))inletx'=Array.(append[|0.|]x')inlety'=Array.(append[|0.|]y')inpllinex'y';(* generates a color theme *)letr',g',b'=plgcol01inletrgb=mod_float((!i+.1.)*.50.)256.|>int_of_floatinplscol01rgbrgbrgb;plcol01;plpsty3;plfillx'y';(* restore original settings *)plscol01r'g'b';plcol01;i:=!i+.c;)x(* TODO: improve the filling ... *)letpie?(h=_default_handle)?(spec=[])x=letopenPlplotin_adjust_rangeh~margin:0.1[|-1.;1.|]X;_adjust_rangeh~margin:0.1[|-1.;1.|]Y;letx=Owl_dense_matrix.D.to_arrayxinletx=Owl_stats.normlise_pdfxin(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletfill=_get_fillspecfalseinletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;plwidth1.;pllsty1;plpsty0;_draw_arcfill1000.x;(* restore original settings *)plscol01r'g'b';plcol01;plwidthold_pensize;pllsty1)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletloglog?(h=_default_handle)?(spec=[])?xy=letopenPlplotinlety=Owl_dense_matrix.D.to_arrayyinletn=Array.lengthyinletx=matchxwith|Somemtx->Owl_dense_matrix.D.to_arraymtx(* The range is [1..n] instead of [0..(n-1)] *)|None->Owl_dense_matrix.D.linspace1.(float_of_intn)n|>Owl_dense_matrix.D.to_arrayinletaxis=_get_axisspecXYinletx,y=matchaxiswith|X->(Array.mapOwl_maths.log10x,y)|Y->(x,Array.mapOwl_maths.log10y)|_->(Array.mapOwl_maths.log10x,Array.mapOwl_maths.log10y)in_adjust_rangehxX;_adjust_rangehyY;letp=h.pages.(h.current_page)inlet_=matchaxiswith|X->p.xlogscale<-true|Y->p.ylogscale<-true|_->(p.xlogscale<-true;p.ylogscale<-true)in(* prepare the closure *)letcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec""inletmarker_size=_get_marker_sizespec4.inletline_style=_get_line_stylespec1inletline_width=_get_line_widthspec(-1.)inletold_pensize=h.pensizein(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;ifline_width>(-1.)thenplwidthline_width;letc'=plgchr()|>fstinplschrmarker_size1.;ifline_style>0&&line_style<9thenpllstyline_style;pllinexy;ifmarker<>""then(letx',y'=_thinningx,_thinningyinplstringx'y'marker);(* restore original settings *)plschrc'1.;plwidthold_pensize;pllsty1;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempLINEline_stylecolormarkercolor0color;ifnoth.holdonthenoutputhletsemilogx?(h=_default_handle)?(spec=[])?xy=letspec=spec@[AxisX]inmatchxwith|Somearr->loglog~h~spec~x:arry|None->loglog~h~specyletsemilogy?(h=_default_handle)?(spec=[])?xy=letspec=spec@[AxisY]inmatchxwith|Somearr->loglog~h~spec~x:arry|None->loglog~h~specyletsurf?(h=_default_handle)?(spec=[])xyz=letopenPlplotinletx=Owl_dense_matrix.D.(rowx0|>to_array)inlety=Owl_dense_matrix.D.(coly0|>to_array)inletz=Owl_dense_matrix.D.transposezinletz0=Owl_dense_matrix.D.to_arrayszinletz1=Owl_dense_matrix.D.to_arrayzin_adjust_rangehxX;_adjust_rangehyY;_adjust_rangehz1Z;(* construct contour level *)letzmin,zmax=Owl_stats.minmaxz1inletclvl=Owl_dense_matrix.D.(linspacezminzmax10|>to_array)in(* prepare the closure *)letp=h.pages.(h.current_page)inp.is_3d<-true;p.altitude<-_get_altitudespecp.altitude;p.azimuth<-_get_azimuthspecp.azimuth;(* assemble the specifications *)letmag_color=_get_mag_colorspectrueinletcontour=_get_contourspecfalseinletcurtain=_get_curtainspecfalseinletfaceted=_get_facetedspecfalseinletopt=[PL_DIFFUSE]inletopt=opt@ifmag_colorthen[PL_MAG_COLOR]else[]inletopt=opt@ifcontourthen[PL_BASE_CONT;PL_SURF_CONT]else[]inletopt=opt@ifcurtainthen[PL_DRAW_SIDES]else[]inletopt=opt@iffacetedthen[PL_FACETED]else[]in(* drawing function *)letf=(fun()->plsurf3dxyz0optclvl;(* restore original settings, if any *))in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletplot3d=surfletmesh?(h=_default_handle)?(spec=[])xyz=letopenPlplotinletx=Owl_dense_matrix.D.(rowx0|>to_array)inlety=Owl_dense_matrix.D.(coly0|>to_array)inletz=Owl_dense_matrix.D.transposezinletz0=Owl_dense_matrix.D.to_arrayszinletz1=Owl_dense_matrix.D.to_arrayzin_adjust_rangehxX;_adjust_rangehyY;_adjust_rangehz1Z;(* construct contour level *)letzmin,zmax=Owl_stats.minmaxz1inletclvl=Owl_dense_matrix.D.(linspacezminzmax10|>to_array)in(* prepare the closure *)letp=h.pages.(h.current_page)inp.is_3d<-true;p.altitude<-_get_altitudespecp.altitude;p.azimuth<-_get_azimuthspecp.azimuth;letcolor=_get_rgbspecp.fgcolorinletr,g,b=colorin(* assemble the specifications *)letmag_color=_get_mag_colorspectrueinletcontour=_get_contourspecfalseinletcurtain=_get_curtainspecfalseinletopt=[PL_MESH]inletopt=opt@[_get_zlinespecPL_DRAW_LINEXY]inletopt=opt@ifmag_colorthen[PL_MAG_COLOR]else[]inletopt=opt@ifcontourthen[PL_BASE_CONT;PL_SURF_CONT]else[]inletopt=opt@ifcurtainthen[PL_DRAW_SIDES]else[]in(* drawing function *)letf=(fun()->(* only takes effect when NoMagColor is set *)letr',g',b'=plgcol01inplscol01rgb;plcol01;matchcontourwith|true->plmeshcxyz0optclvl|false->plmeshxyz0opt;(* restore original settings, if any *)plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletheatmap?(h=_default_handle)xyz=letopenPlplotinletx=Owl_dense_matrix.D.(rowx0|>to_array)inlety=Owl_dense_matrix.D.(coly0|>to_array)inletz=Owl_dense_matrix.D.transposezinletz0=Owl_dense_matrix.D.to_arrayszinletz1=Owl_dense_matrix.D.to_arrayzin_adjust_rangehxX;_adjust_rangehyY;_adjust_rangehz1Z;(* construct contour level *)letxmin,xmax=Owl_stats.minmaxxinletymin,ymax=Owl_stats.minmaxyinletzmin,zmax=Owl_stats.minmaxz1inletclvl=Owl_dense_matrix.D.(linspacezminzmax10|>to_array)in(* prepare the closure *)letp=h.pages.(h.current_page)inletf=(fun()->plshadesz0xminxmaxyminymaxclvl1.001.0false(* restore original settings, if any *))in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletcontour?(h=_default_handle)xyz=letopenPlplotinletm,n=Owl_dense_matrix.D.shapezinletx0=Owl_dense_matrix.D.to_arraysxinletx1=Owl_dense_matrix.D.to_arrayxinlety0=Owl_dense_matrix.D.to_arraysyinlety1=Owl_dense_matrix.D.to_arrayyinletz0=Owl_dense_matrix.D.to_arrayszinletz1=Owl_dense_matrix.D.to_arrayzin_adjust_rangehx1X;_adjust_rangehy1Y;(* construct contour level *)letzmin,zmax=Owl_stats.minmaxz1inletclvl=Owl_dense_matrix.D.(linspacezminzmax10|>to_array)in(* prepare the closure *)letp=h.pages.(h.current_page)inletf=(fun()->plset_pltr(pltr2x0y0);plcontz01m1nclvl;plunset_pltr()(* restore original settings, if any *))in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhlet_draw_extended_linex0y0x1y1lrud=(* Specify two points, draw a line between them, and extend on both ends with dash line *)letopenPlplotinletx0,y0,x1,y1=ifx0>x1thenx1,y1,x0,y0elsex0,y0,x1,y1inletyl=ifx0=x1thenuelsey0-.(y1-.y0)/.(x1-.x0)*.(x0-.l)inletyr=ifx0=x1thendelsey1+.(y1-.y0)/.(x1-.x0)*.(r-.x1)inletxl=ifx0=x1thenx0elselinletxr=ifx0=x1thenx0elserinpllsty1;pljoinx0y0x1y1;pllsty3;pljoinxlylx0y0;pllsty3;pljoinx1y1xryr;(* restore line style *)pllsty1letprobplot?(h=_default_handle)?(spec=[])?(dist=(Owl_stats.gaussian_ppf~mu:0.~sigma:1.))?(noref=false)x=(* TODO: show y-axis as probability instead of invcdf; Choose suitable
yticks for different distribution; support for censor data, frequency *)(* inputs *)letopenPlplotinletx=Owl_dense_matrix.D.to_arrayx|>Owl_stats.sort~inc:trueinlety=letn=Array.lengthxinletqth=Owl_dense_matrix.D.linspace((1.-.0.5)/.float_of_intn)((float_of_intn-.0.5)/.float_of_intn)ninletq=Owl_dense_matrix.D.mapdistqthinOwl_dense_matrix.D.to_arrayqin_adjust_rangehxX;_adjust_rangehyY;(* parameters to draw the reference line *)letp1y,p1x=(Owl_stats.first_quartiley,Owl_stats.first_quartilex)inletp3y,p3x=(Owl_stats.third_quartiley,Owl_stats.third_quartilex)inletleft,right=Owl_stats.minmaxxinletup,down=Owl_stats.minmaxyin(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec"#[0x002b]"inletmarker_size=_get_marker_sizespec4.in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;letc'=plgchr()|>fstinplschrmarker_size1.;ifnotnorefthen_draw_extended_linep1xp1yp3xp3yleftrightupdown;plstringxymarker;(* restore original settings *)plschrc'1.;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempSCATTER0colormarkercolor0color;ifnoth.holdonthenoutputhletnormplot?(h=_default_handle)?(spec=[])?(sigma=1.)x=(* TODO: replace yticklabels, including unseen tick labels, with user-defined labels *)letdist=Owl_stats.gaussian_ppf~mu:0.~sigmainprobplot~h~spec~dist:distxletwblplot?(h=_default_handle)?(spec=[])?(lambda=1.)?(k=1.)x=(* TODO: logscale and logfit *)(* inputs *)letopenPlplotinletx=Owl_dense_matrix.D.to_arrayx|>Owl_stats.sort~inc:trueinletdist=Owl_stats.weibull_ppf~shape:k~scale:lambdainlety=letn=Array.lengthxinletqth=Owl_dense_matrix.D.linspace((1.-.0.5)/.float_of_intn)((float_of_intn-.0.5)/.float_of_intn)ninletq=Owl_dense_matrix.D.mapdistqthinOwl_dense_matrix.D.to_arrayqin_adjust_rangehxX;_adjust_rangehyY;(* parameters to draw the reference line *)letp1y,p1x=(Owl_stats.first_quartiley,Owl_stats.first_quartilex)inletp3y,p3x=(Owl_stats.third_quartiley,Owl_stats.third_quartilex)inletleft,right=Owl_stats.minmaxxinletup,down=Owl_stats.minmaxyin(* prepare the closure; note the change to log sacle *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec"#[0x002b]"inletmarker_size=_get_marker_sizespec4.in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;letc'=plgchr()|>fstinplschrmarker_size1.;_draw_extended_linep1xp1yp3xp3yleftrightupdown;plstringxymarker;(* restore original settings *)plschrc'1.;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempSCATTER0colormarkercolor0color;ifnoth.holdonthenoutputhlet_ecdf_distabp=(* find the ecdf value of probability value p; (a, b) is the output of
Stats.ecdf *)letrec_find_recxlsti=matchlstwith|hd::tl->if(hd>x)theni-1else_find_recxtl(i+1)|_->(Array.lengthb)-1inletticks=Array.to_listbinleti=_find_recpticks1ina.(i)letqqplot?(h=_default_handle)?(spec=[])?(pd=Owl_stats.gaussian_ppf~mu:0.~sigma:1.)?xy=(* TODO: support matrix input; add support for `pvec` argument;
plot the larger data input on x-axis *)letopenPlplotinlety=Owl_dense_matrix.D.to_arrayy|>Owl_stats.sort~inc:trueinletn=Array.lengthyinletdist=matchxwith|Somearr->(* if the second argument is a vector *)letx=Owl_dense_matrix.D.to_arrayarrin(* The empirical CDF of it is used as dist. *)leta,b=Owl_stats.ecdfxin(funp->_ecdf_distabp)|None->pdinletqth=Owl_dense_matrix.D.linspace((1.-.0.5)/.float_of_intn)((float_of_intn-.0.5)/.float_of_intn)ninletq=Owl_dense_matrix.D.mapdistqthinletx=Owl_dense_matrix.D.to_arrayqin(* draw the figure *)_adjust_rangehxX;_adjust_rangehyY;(* parameters to draw the reference line *)letp1y,p1x=(Owl_stats.first_quartiley,Owl_stats.first_quartilex)inletp3y,p3x=(Owl_stats.third_quartiley,Owl_stats.third_quartilex)inletleft,right=Owl_stats.minmaxxinletup,down=Owl_stats.minmaxyin(* prepare the closure *)letp=h.pages.(h.current_page)inletcolor=_get_rgbspecp.fgcolorinletr,g,b=colorinletmarker=_get_markerspec"#[0x002b]"inletmarker_size=_get_marker_sizespec4.in(* drawing function *)letf=(fun()->letr',g',b'=plgcol01inplscol01rgb;plcol01;letc'=plgchr()|>fstinplschrmarker_size1.;_draw_extended_linep1xp1yp3xp3yleftrightupdown;plstringxymarker;(* restore original settings *)plschrc'1.;plscol01r'g'b';plcol01)in(* add closure as a layer *)p.plots<-Array.appendp.plots[|f|];(* add legend item to page *)_add_legend_itempSCATTER0colormarkercolor0color;ifnoth.holdonthenoutputh(* TODO *)letscatterhist=None[@@warning"-32"](* other plots *)letimage?(h=_default_handle)x=letopenPlplotin(* rotate the matrix 90 degree clockwise to be shown correctly as image *)letx=Owl_dense_matrix.D.rotatex90in(* compute necessary parameters *)letwidth,height=Owl_dense_matrix.D.shapexinletnum_col=Owl_dense_matrix.D.max'xinletimg=Owl_dense_matrix.D.to_arraysxinletwidth=float_of_intwidthinletheight=float_of_intheightinletnum_col=int_of_floatnum_colin(* specify the boundary of imageplot *)letx=[|1.0;width|]inlety=[|1.0;height|]in_adjust_rangehxX;_adjust_rangehyY;(* keep the scale of original image instead of 4:3 *)h.page_size<-(int_of_floatwidth,int_of_floatheight);(* prepare the closure *)letp=h.pages.(h.current_page)inlet_=p.no_axes<-trueinletf=(fun()->(* set gray_cmap *)letr=[|0.0;1.0|]inletg=[|0.0;1.0|]inletb=[|0.0;1.0|]inletpos=[|0.0;1.0|]inplscmap1nnum_col;plscmap1ltrueposrgbNone;plimageimg1.0width1.0height0.00.01.0width1.0height;(* possibly need to restore original settings *))inp.plots<-Array.appendp.plots[|f|];ifnoth.holdonthenoutputhletspy?(h=_default_handle)?(spec=[])x=letxs=Owl_utils.Stack.make()inletys=Owl_utils.Stack.make()inletm,n=Owl_dense_matrix.D.shapexinfori=0tom-1doforj=0ton-1doleta=Owl_dense_matrix.D.getxijinifa<>0.then(Owl_utils.Stack.pushxs(float_of_inti);Owl_utils.Stack.pushys(float_of_intj);)done;done;letx=Owl_utils.Stack.to_arrayxsinlety=Owl_utils.Stack.to_arrayysinletx=Owl_dense_matrix.D.of_arrays[|x|]inlety=Owl_dense_matrix.D.of_arrays[|y|]inscatter~h~specxy(* ends here *)