-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start sketching the object-oriented wrappers.
- Loading branch information
1 parent
ccef606
commit f98dcb6
Showing
8 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
(executables | ||
(names pyplot) | ||
(names fig pyplot) | ||
(libraries base stdio matplotlib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(* Example using the object oriented api. *) | ||
open Base | ||
open Matplotlib | ||
|
||
let () = | ||
let xs = List.init 120 ~f:Float.of_int in | ||
let ys1 = List.map xs ~f:(fun i -> Float.sin (i /. 20.)) in | ||
let ys2 = List.map xs ~f:(fun i -> Float.cos (i /. 12.)) in | ||
let xs = Array.of_list xs in | ||
let ys1 = Array.of_list ys1 in | ||
let ys2 = Array.of_list ys2 in | ||
let _fig, ax = Fig.create_with_ax () in | ||
Ax.set_xlabel ax "x label"; | ||
Ax.set_ylabel ax "sin(x)"; | ||
Ax.grid ax true; | ||
Ax.plot ax ~color:Red ~xs ys1; | ||
Ax.plot ax ~color:Green ~linestyle:Dotted ~linewidth:2. ~xs ys2; | ||
Mpl.show () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
open Base | ||
open Pyops | ||
|
||
module Ax = struct | ||
type t = Py.Object.t | ||
|
||
let set_title t title = | ||
ignore (t.&("set_title")[| Py.String.of_string title |]) | ||
|
||
let set_xlabel t label = | ||
ignore (t.&("set_xlabel")[| Py.String.of_string label |]) | ||
|
||
let set_ylabel t label = | ||
ignore (t.&("set_ylabel")[| Py.String.of_string label |]) | ||
|
||
let grid t ?which ?axis b = | ||
let keywords = | ||
let b = Some ("b", Py.Bool.of_bool b) in | ||
let which = | ||
Option.map which ~f:(fun which -> | ||
let which = | ||
match which with | ||
| `major -> "major" | ||
| `minor -> "minor" | ||
| `both -> "both" | ||
in | ||
"which", Py.String.of_string which) | ||
in | ||
let axis = | ||
Option.map axis ~f:(fun axis -> | ||
let axis = | ||
match axis with | ||
| `both -> "both" | ||
| `x -> "x" | ||
| `y -> "y" | ||
in | ||
"axis", Py.String.of_string axis) | ||
in | ||
List.filter_opt [ b; which; axis ] | ||
in | ||
ignore (Py.Module.get_function_with_keywords t "grid" [||] keywords) | ||
|
||
let plot t ?color ?linewidth ?linestyle ?xs ys = | ||
Mpl.plot t ?color ?linewidth ?linestyle ?xs ys | ||
end | ||
|
||
module Fig = struct | ||
type t = Py.Object.t | ||
|
||
let create ?figsize () = | ||
let p = Mpl.pyplot_module () in | ||
let keywords = | ||
let figsize = | ||
Option.map figsize ~f:(fun (w, h) -> | ||
"figsize", Py.Tuple.of_pair Py.Float.(of_float w, of_float h)) | ||
in | ||
List.filter_opt [ figsize ] | ||
in | ||
Py.Module.get_function_with_keywords p "figure" [||] keywords | ||
|
||
let add_subplot t ~nrows ~ncols ~index = | ||
let keywords = [] in | ||
let args = [| nrows; ncols; index |] |> Array.map ~f:Py.Int.of_int in | ||
Py.Module.get_function_with_keywords t "add_subplot" args keywords | ||
|
||
let create_with_ax ?figsize () = | ||
let t = create ?figsize () in | ||
let ax = add_subplot t ~nrows:1 ~ncols:1 ~index:1 in | ||
t, ax | ||
|
||
let suptitle t title = | ||
ignore (t.&("suptitle")[| Py.String.of_string title |]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Ax : sig | ||
type t | ||
|
||
val set_title : t -> string -> unit | ||
val set_xlabel : t -> string -> unit | ||
val set_ylabel : t -> string -> unit | ||
val grid : t -> ?which:[`major|`minor|`both] -> ?axis:[`both|`x|`y] -> bool -> unit | ||
|
||
val plot | ||
: t | ||
-> ?color:Mpl.Color.t | ||
-> ?linewidth:float | ||
-> ?linestyle:Mpl.Linestyle.t | ||
-> ?xs:float array | ||
-> float array | ||
-> unit | ||
end | ||
|
||
module Fig : sig | ||
type t | ||
|
||
val create : ?figsize:(float * float) -> unit -> t | ||
|
||
(* Use the same api as the python library even if it seems a | ||
bit odd. | ||
Note that the index starts at 1 in the upper left corner and | ||
increases to the right. | ||
*) | ||
val add_subplot : t -> nrows:int -> ncols:int -> index:int -> Ax.t | ||
|
||
val create_with_ax : ?figsize:(float * float) -> unit -> t * Ax.t | ||
|
||
val suptitle : t -> string -> unit | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
module Ax = Fig_ax.Ax | ||
module Fig = Fig_ax.Fig | ||
module Mpl = Mpl.Public | ||
module Pyplot = Pyplot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters