plo.fun 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #import std
  2. #import nat
  3. #import flo
  4. #import tbl
  5. #comment -[graph plotting functions, Copyright (C) 2007-2010 Dennis Furey]-
  6. #optimize+
  7. ------------------------------------------------------- data structures ----------------------------------------------------
  8. linear = -+all \/fleq times/100. eps,abs*,nth_diff2+-
  9. logarithmic = (not any zeroid)&& -+all \/fleq times/1e3 eps,abs*,nth_diff1,div*,~&typ+-
  10. #library+
  11. curve ::
  12. points %ssXLesXLseXLeeXLUUU # a list of pairs of strings or floats describing a function of one variable
  13. peg %seU # value that's constant along this curve if it's a function of two variables
  14. attributes %sm # passed through as \psset{attribute=value,..} for each curve
  15. decorations %seUWsLXL # LaTeX code fragments pasted onto the graph at the given point coordinates
  16. scattered %b # points are plotted without being connected
  17. discrete %b # points are plotted on top of vertical lines
  18. ordinate %a &?=(&h!,~&)+~ordinate # index with respect to the ordinates indicating the placer to use for plotting
  19. axis ::
  20. variable %s # written near the axis on the plot
  21. alias %eW # an absolute displacement in points to move the label away from its default position if necessary
  22. hatches %eLsLU # a list of numbers or strings to be printed along the axis
  23. -|~hatches,~hats; ~&i&& strtod*; &&~& not all zeroid|-
  24. rotation %e # a number of degrees whereby the number or strings along the axis are turned for display purposes
  25. intercept %esUL # the coordinates of origin of the axis with respect to the other axes
  26. hats %sL # strings to be printed along the axis
  27. ~hats|| ~hatches; %sLI?/~& -&linear,abs*; all zeroid!| -&fleq/0.01,fleq\1.e5&-&-?(
  28. printf/*'%0.2f',
  29. scientific_notation*+ printf/*'%0.2e')
  30. placer %fOZ # takes a string or number to a number between 0 and 1 indicating its position along the axis
  31. ~placer|| ~hatches; ~&itB&& ~&hzEZ&& %eLI&& -!ordered fleq,ordered not fleq!-&& -|
  32. linear&& ~&hzX; ("l","h"). div\minus("h","l")+ minus\"l",
  33. logarithmic&& ~&hzX; ln~~; ("l","h"). div\minus("h","l")+ minus\"l"+ ln,
  34. ~&itB&& (^/~&rr div^/float+~&rl ~&l)^*D(float+ length+ ~&t,num); -+
  35. (~&av^?\~&Nadr2XNV ~&avhdlrl6NXfavPMV); "t". ^H\~& ~&/"t"; ~&aldl^?\~&aldr fleq?arldl2X/~&falvh2rXPR ~&falvth3rXPR,
  36. ~&ldll3rdlr3XNXlrNCCV:-0+ ^piNVS/(~&at^& :^/~&ahthPX ~&fatPR) ~&aitB^& :^\~&fatPR -+
  37. (("x1","y1"),("x2","y2")). plus/"y1"+ times/minus("y2","y1")+ div\minus("x2","x1")+ minus\"x1",
  38. ~&ahthPX+-+-|-
  39. visualization ::
  40. abscissa _axis # the axis for the first independent variable
  41. ~&r+ -+
  42. -!~&r.intercept,not all~hatches+ ~&l!-?/~& &r.intercept:= ~hatches.&h*+ ~&l,
  43. ^/(^T/~pegaxis.&iiNCB ~ordinates) ~abscissa;-&~hatches,~placer&-?/~abscissa -+
  44. -&~&%eLLI,all -!ordered fleq,ordered not fleq!-&-?r(
  45. axis$l[hatches: ~&l.hatches|| ~&rL; ~&i&& ari6^/fleq$- fleq$^],
  46. axis$l[hatches: ~&l.hatches|| ~&r; ^H\~& satch+ ~&itBitBitB+ length,placer: place+ ~&r]^/~&l merge+ ~&r),
  47. ^(~abscissa,~&lSS+ ~points*+ ~curves)+-+-
  48. pegaxis _axis%Z # the axis for the second independent variable if any
  49. ~&r+ -+
  50. -!~&rZ,~&r.intercept,not all~hatches+ ~&l!-?/~& &r.intercept:= ~hatches.&h*+ ~&l,
  51. ^/(:^/~abscissa ~ordinates) ||~pegaxis -&any~peg+ ~curves,~pegaxis; not -&~&,~hatches,~placer&-&-&& -+
  52. -&%eLI,-!ordered fleq,ordered not fleq!-&-?r(
  53. axis$l[hatches: ~&l.hatches|| ~&r; ari6^/fleq$- fleq$^],
  54. axis$l[hatches: ~&l.hatches|| ~&r; ^H\~& satch+ ~&itBitBitB+ length,placer: ~&l.placer|| place+ ~&r]),
  55. ^(~pegaxis||axis&!,~peg*+ ~curves)+-+-
  56. ordinates _axis%L # the axes of the dependent variables
  57. (~&l?\~&r ~&ar^& :^\~&falttiQPrtPXPR ~&abh; &r.intercept:=r ~&r.intercept||~&l)^(
  58. -+
  59. ~&g&& ~&rlrPllPQS^*DlrpS/~& (~&rS+ zipp0)^*D(0!*+ ~&z,~&)+ iota+ --<&>+ 0!*,
  60. ~hatches.&ihzXB*+ :^/~abscissa ~pegaxis.&iiNCB+-,
  61. -+
  62. ~&rS+ nleq-<&l+ ^T\~&r gdif~&EblPO,
  63. ^/~&l ~&r; (_axis%nwX,_curve%L)%XLMk; * ^/~&ll (fused axis)^/~&lr -+
  64. ~&B?/<'conflicting axis types'>!% -|
  65. ~&l&& axis$[hatches: ~&lL; ari6^/fleq$- fleq$^],
  66. ~&r&& axis$[hatches: ^H\~& satch+ ~&itBitBitB+ length,placer: place]+ merge+ ~&r|-,
  67. %eLI!=+ ~&rSS+ ~points*+ ~&r+-,
  68. ^/~&l |=hrPlSXSlF&r+ -*; * ^/~&r ~&l&& ~(&r.ordinate,&l); ~&l?\~&rh indexable&& ~&NlXrH,
  69. ^\~curves num+ ~ordinates||<axis&>!+-)
  70. curves _curve%L ~curves; * ~ordinate==&?\~& ordinate:= %eI+~points.&ihBr?/&h! &th!
  71. picture_frame %eWW ~picture_frame; ^|(^({0,0.}?<l/300.! ~&l,{0,0.}?<r/200.! ~&r),~~ {0,0.}?</-32.5! ~&)
  72. headroom %eZ ~headroom|| 25.!
  73. margin %eZ ~margin|| ~ordinates.&itB?/30.! 10.!
  74. viewport %eWZ -&~margin,~headroom,~viewport.&Z&-?\~viewport minus^~bbI(plus~~+ ~picture_frame.&bbI,~/margin headroom)
  75. boxed %b
  76. ----------------------------------------------------------- plotting functions ---------------------------------------------
  77. #library-
  78. satch"n" = ~&i&& ~&hSztzNCBPT+ ~&t?\~&iNC ^H\~& block+~&lihBPrizBPYirB+ (nleq/"n")!=&r+ (*~ not remainder)^D/length ~&itB+iol
  79. place = ?^(\/-=+ ~&lS,~&\<'bad coordinate'>!%+ -:)+ (^/~&rr div+~&rlPlX)^*D/float+length+~&t num; * ^\~&r float+ ~&l
  80. merge = ~&hS+ rlc~&E+ :-0 ~&al^?\~&ar ~&ar?\~&al ~&alhPrhPE?/~&alh2fabt2RC ~&alhPrw?/~&arh2falrtPXPRC ~&alh2faltPrXPRC
  81. left_axis =
  82. -+
  83. :/'% left axis'+ :^(
  84. -+
  85. '\put'--+ ^|T(:/`(+ --')'+ ^|T(~&,:/`,)+ printf/$'%0.2f','{\makebox(0,0)[l]{'--+ --'}}'),
  86. ~&lrlPX; ^\~&l.ordinates.&h.variable plus^~bbI/~&l.ordinates.&h.alias ^(
  87. minus\10.+ ~&r,
  88. plus/20.+ ~&l.viewport.&r)+-,
  89. ~&rlrD; :^y(
  90. '\psline{->}'--+ ~&hzXblrlPX; --+ ~~ :/`(+ --')'+ ^|T(printf/'%0.2f',:/`,),
  91. *= <.
  92. '\put'--+ ^T(~&lrlPX; :/`(+ --')'+ ^|T(printf/'%0.2f'+ minus\10.,:/`,),~&rr; :/`{+ --'}'),
  93. '\put'--+ ^T(:/`(+ --')'+ ^T(printf/'%0.2f'+ ~&l,:/`,+ ~&rl),! '{\psline{-}(0,0)(-5,0)}')>)),
  94. ^/~& ^(
  95. ^H\~ordinates.&h.intercept.&h +^\~abscissa.placer //times+ minus^\~margin plus+ ~picture_frame.&bl,
  96. ^p(printf/*'%0.2f'+ ^H/ari+length+~&r ~&/0.+ ~&l,~&r)^/~viewport.&r ~ordinates.&h; -+
  97. * '\begin{rotate}'--+ --'\end{rotate}'+ ^|T(:/`{+ --'}','\makebox(0,0)[r]{'--+ --'}'),
  98. ^D(
  99. printf/'%0.2f'+ ~rotation,
  100. ~hats|| ~hatches; %sLI?/~& -&linear,any fleq/1.&-?(printf/*'%0.2f',printf/*'%0.2e'))+-)+-
  101. right_axis = # the intercept is ignored
  102. ~ordinates.&itB&& -+
  103. ~&r&& :/'% right axis'+ :^(
  104. -+
  105. '\put'--+ ^|T(:/`(+ --')'+ ^|T(~&,:/`,)+ printf/$'%0.2f','{\makebox(0,0)[r]{'--+ --'}}'),
  106. ~&l; ^\~ordinates.&th.variable plus^~bbI/~ordinates.&h.alias ^|(plus/10.,plus/20.)+ ~viewport+-,
  107. :^y(
  108. '\psline{->}'--+ ^G(printf/'%0.2f'+ ~&l.viewport.&l,~&rhzXbl); --+ ~~ :/`(+ --')'+ ^|T/~& :/`,,
  109. -*+~(&l.viewport.&l,&r); ~&lrlPXrrPXS; *= <.
  110. '\put'--+ ^|T(^|T(:/`(+ printf/'%0.2f'+ plus/10.,:/`,+ --')'),:/`{+ --'}'),
  111. ^|T('\put('--+ printf/'%0.2f',:/`,+ --'){\psline{-}(0,0)(5,0)}')+ ~&l>)),
  112. ^/~& ^p(printf/*'%0.2f'+ ^H/ari+length+~&r ~&/0.+ ~&l,~&r)^/~viewport.&r ~ordinates.&th; -+
  113. * '\begin{rotate}'--+ --'\end{rotate}'+ ^|T(:/`{+ --'}','\makebox(0,0)[l]{'--+ --'}'),
  114. ^D(printf/'%0.2f'+ ~rotation,~hats|| ~hatches; %sLI?/~& -&linear,any fleq/1.&-?(printf/*'%0.2f',printf/*'%0.2e'))+-+-
  115. bottom_axis =
  116. -+
  117. :/'% bottom axis'+ :^(
  118. -+
  119. '\put'--+ ^|T(:/`(+ --')'+ ^|T(~&,:/`,)+ printf/$'%0.2f','{\makebox(0,0)[t]{'--+ --'}}'),
  120. ~&l; ^\~abscissa.variable plus^~bbI/~abscissa.alias ~&\-25.+ div\2.+ ~viewport.&l+-,
  121. -+
  122. -!~ordinates.&itB,~curves; any ~discrete!-?ll(
  123. :^\~&rr ~&rlPlrPX; %ssWLXMk; '\psline{-}'--+ ~&lrhzXbl3G; --+ ~~ '('--+ --')'+ ^T/~&r :/`,+ ~&l,
  124. :^y\~&rr ~&rlPlrPX; %ssWLXMk; '\psline{->}'--+ ~&lrhzXbl3G; --+ ~~ '('--+ --')'+ ^T/~&r :/`,+ ~&l),
  125. ^/~& -+
  126. ('0.00'?=l/~& ~&lritBitB2X)^/(printf/'%0.2f'+ ~&l) -+
  127. %soULMk+ *= <.
  128. '\put'--+ ^T('('--+ --')'+ ^T/~&rl :/`,+ ~&l,:/`{+ --'}'+ ~&rr),
  129. '\put('--+ --'){\psline{-}(0,5)(0,10)}'+ ^T/~&rl :/`,+ ~&l>,
  130. ^D\~&r printf/'%0.2f'+ %eoUMk+ minus\10.+ ~&l+-,
  131. ^\~&r ~&l; times^/~viewport.&r ^H/~ordinates.&h.placer ~abscissa.intercept.&h+-+-),
  132. ^/~& %sWLMk^p(printf/*'%0.2f'+ ^H/ari+length+~&r ~&/0.+ ~&l,~&r)^/~viewport.&l ~abscissa; -+
  133. * '\begin{rotate}'--+ --'\end{rotate}'+ ^T/(~&l; :/`{+ --'}'+ printf/'%0.2f') ^|T(
  134. '\makebox(0,0)['--+ --']'+ fleq/30.?/'tr'! fleq\-30.?/'tl'! 't'!,
  135. :/`{+ --'}'),
  136. ^D(~rotation,~hats|| ~hatches; %sLI?/~& -&linear,any fleq/1.&-?(printf/*'%0.2f',printf/*'%0.2e'))+-+-
  137. surface =
  138. :/'% curves'+ -+
  139. *== <.
  140. ~&r.attributes; ~&i&& ~&iNC+ '\psset{'--+ --'}'+ mat`,+ * ^T/~&l :/`=+ ~&r,
  141. ^D(~&lr,~&rF+ ~&r.decorations); *= ^lrhPTrtPC(
  142. '\put'--+ ~&lrlPH; :/`(+ --')'+ ^|T/~& :/`,,
  143. :^(:/`{+ ~&h,~&t)+ ^lrNCT(~&y,--'}'+ ~&z)+ ~&rr),
  144. ~&r.discrete?(
  145. ^H(~&lr; -+*+ //+ '\psline{-*}'--+ --+ ~~ :/`(+ --')'+ ^|T/~& :/`,,~&rlPlXrX;+ ~~+-,-*+ ~/&ll &r.points),
  146. :^(
  147. ~&r.scattered?/'\psdots%'! '\psline{-}%'!,
  148. (* ' '--)+ ~&LS+ ~&lrPrX; block4^|H\~points *+ :/`(++ --')'++ //+ ^|T/~& :/`,))>,
  149. ~&r.points*~^p\~curves ^D/~abscissa.intercept.&h (^|; //+ printf/$'%6.2f')^*D(
  150. +^\~abscissa.placer //times+ minus^\~margin plus+ ~picture_frame.&bl,
  151. +^*D(//times+ minus^\~headroom plus+ ~picture_frame.&br,~placer^*DNrXlHS/~ordinates ~ordinate*+ ~curves))+-
  152. #library+
  153. plot = # takes a visualization to a LaTeX fragment
  154. _visualization%Mk; ~&rhPlrtPCC/'\psset{unit=1pt}'+ :^(
  155. ~picture_frame; '\begin{picture}'--+ --+ ~~ printf/$'%0.2f'; :/`(+ --')'+ ^T/~&l :/`,+ ~&r,
  156. --<'\end{picture}'>+ ^T/(~&L+ <.left_axis,right_axis,bottom_axis,surface>) ~boxed&& ~picture_frame; -+
  157. :/'% bounding box'+ ~&iNC+ '\put'--+ ^T/~&r '{\dashbox'--+ --'{}}'+ ~&l,
  158. ~~ printf/$'%0.2f'; :/`(+ --')'+ ^|T/~& :/`,+-)
  159. ------------------------------------------------- file formatting functions --------------------------------------------------
  160. latex_document = # wraps the argument in LaTeX front matter suitable for a free standing document
  161. --<'\end{document}',''>+ //~&T ~&tittt2BS -[
  162. \documentclass{article}
  163. \usepackage{pstricks}
  164. \usepackage{pspicture}
  165. \usepackage{rotating}
  166. \usepackage{booktabs}
  167. \usepackage{longtable}
  168. \addtolength{\oddsidemargin}{-50pt}
  169. \addtolength{\evensidemargin}{-50pt}
  170. \addtolength{\topmargin}{-100pt}
  171. \addtolength{\textheight}{140pt}
  172. \addtolength{\textwidth}{120pt}
  173. \begin{document}
  174. \setlength{\arrowlength}{5pt}
  175. \psset{linewidth=0.5pt,arrowinset=0,arrowscale=1.1,showpoints=false}]-