Skip to content

Commit

Permalink
Extend plot functionality (#3)
Browse files Browse the repository at this point in the history
* Add fill_between

* Add semilogy, semilogx, loglog plot functions
  • Loading branch information
mknbv authored May 22, 2020
1 parent 64bef1f commit 5617129
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/pyplot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ let () =
Pyplot.grid true;
Pyplot.plot ~color:Red ~xs ys1;
Pyplot.plot ~color:Green ~linestyle:Dotted ~linewidth:2. ~xs ys2;
Pyplot.fill_between ~alpha:0.3
xs ys1 (Array.create ~len:(Array.length ys1) 0.);
Pyplot.legend ~labels:[|"$y=\\sin(x/20)$"; "$y=\\cos(x/12)$"|] ();
Mpl.savefig "test.png";
let data = Mpl.plot_data `png in
Expand Down
31 changes: 29 additions & 2 deletions src/matplotlib/mpl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ end

let float_array_to_python xs = Py.List.of_array_map Py.Float.of_float xs

let plot p ?label ?color ?linewidth ?linestyle ?xs ys =
let call_plot_func p func ?label ?color ?linewidth ?linestyle ?xs ys =
let keywords =
List.filter_opt
[ Option.map color ~f:(fun color -> "color", Color.to_pyobject color)
Expand All @@ -178,7 +178,34 @@ let plot p ?label ?color ?linewidth ?linestyle ?xs ys =
| Some xs -> [| float_array_to_python xs; float_array_to_python ys |]
| None -> [| float_array_to_python ys |]
in
ignore (Py.Module.get_function_with_keywords p "plot" args keywords)
let func_name = match func with
| `plot -> "plot"
| `semilogy -> "semilogy"
| `semilogx -> "semilogx"
| `loglog -> "loglog"
in
ignore (Py.Module.get_function_with_keywords p func_name args keywords)

let plot p ?label ?color ?linewidth ?linestyle ?xs ys =
call_plot_func p `plot ?label ?color ?linewidth ?linestyle ?xs ys

let semilogy p ?label ?color ?linewidth ?linestyle ?xs ys =
call_plot_func p `semilogy ?label ?color ?linewidth ?linestyle ?xs ys

let semilogx p ?label ?color ?linewidth ?linestyle ?xs ys =
call_plot_func p `semilogx ?label ?color ?linewidth ?linestyle ?xs ys

let loglog p ?label ?color ?linewidth ?linestyle ?xs ys =
call_plot_func p `loglog ?label ?color ?linewidth ?linestyle ?xs ys

let fill_between p ?color ?alpha xs ys1 ys2 =
let keywords = List.filter_opt
[ Option.map color ~f:(fun color -> "color", Color.to_pyobject color)
; Option.map alpha ~f:(fun alpha -> "alpha", Py.Float.of_float alpha)
]
in
let args = Array.map [|xs; ys1; ys2|] float_array_to_python in
ignore (Py.Module.get_function_with_keywords p "fill_between" args keywords)

let hist p ?label ?color ?bins ?orientation ?histtype ?xs ys =
let keywords =
Expand Down
39 changes: 39 additions & 0 deletions src/matplotlib/mpl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,45 @@ val plot
-> float array
-> unit

val semilogy
: Py.Object.t
-> ?label:string
-> ?color:Color.t
-> ?linewidth:float
-> ?linestyle:Linestyle.t
-> ?xs:float array
-> float array
-> unit

val semilogx
: Py.Object.t
-> ?label:string
-> ?color:Color.t
-> ?linewidth:float
-> ?linestyle:Linestyle.t
-> ?xs:float array
-> float array
-> unit

val loglog
: Py.Object.t
-> ?label:string
-> ?color:Color.t
-> ?linewidth:float
-> ?linestyle:Linestyle.t
-> ?xs:float array
-> float array
-> unit

val fill_between
: Py.Object.t
-> ?color:Color.t
-> ?alpha:float
-> float array
-> float array
-> float array
-> unit

val hist
: Py.Object.t
-> ?label:string
Expand Down
16 changes: 16 additions & 0 deletions src/matplotlib/pyplot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ let plot ?label ?color ?linewidth ?linestyle ?xs ys =
let p = Mpl.pyplot_module () in
Mpl.plot p ?label ?color ?linewidth ?linestyle ?xs ys

let semilogy ?label ?color ?linewidth ?linestyle ?xs ys =
let p = Mpl.pyplot_module () in
Mpl.semilogy p ?label ?color ?linewidth ?linestyle ?xs ys

let semilogx ?label ?color ?linewidth ?linestyle ?xs ys =
let p = Mpl.pyplot_module () in
Mpl.semilogx p ?label ?color ?linewidth ?linestyle ?xs ys

let loglog ?label ?color ?linewidth ?linestyle ?xs ys =
let p = Mpl.pyplot_module () in
Mpl.loglog p ?label ?color ?linewidth ?linestyle ?xs ys

let fill_between ?color ?alpha xs ys1 ys2 =
let p = Mpl.pyplot_module () in
Mpl.fill_between p ?color ?alpha xs ys1 ys2

let hist ?label ?color ?bins ?orientation ?histtype ?xs ys =
let p = Mpl.pyplot_module () in
Mpl.hist p ?label ?color ?bins ?orientation ?histtype ?xs ys
Expand Down
35 changes: 35 additions & 0 deletions src/matplotlib/pyplot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,41 @@ val plot
-> float array
-> unit

val semilogy
: ?label:string
-> ?color:Mpl.Color.t
-> ?linewidth:float
-> ?linestyle:Mpl.Linestyle.t
-> ?xs:float array
-> float array
-> unit

val semilogx
: ?label:string
-> ?color:Mpl.Color.t
-> ?linewidth:float
-> ?linestyle:Mpl.Linestyle.t
-> ?xs:float array
-> float array
-> unit

val loglog
: ?label:string
-> ?color:Mpl.Color.t
-> ?linewidth:float
-> ?linestyle:Mpl.Linestyle.t
-> ?xs:float array
-> float array
-> unit

val fill_between
: ?color:Mpl.Color.t
-> ?alpha:float
-> float array
-> float array
-> float array
-> unit

val hist
: ?label:string
-> ?color:Mpl.Color.t
Expand Down

0 comments on commit 5617129

Please sign in to comment.