Skip to content

Commit

Permalink
Add scatter plots.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare committed May 13, 2019
1 parent be7bffe commit 988ffbd
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
25 changes: 25 additions & 0 deletions examples/fig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,28 @@ let () =
right_graph ax2;
Fig.suptitle fig "the figure suptitle";
Mpl.show ()

let () =
let fig, ax = Fig.create_with_ax () in
let xys1 =
Array.init 1000 ~f:(fun i ->
let f = Float.of_int i /. 20. in
Float.sin f, Float.cos f)
in
let xys2 =
Array.init 1000 ~f:(fun i ->
let f = Float.of_int i /. 20. in
let rho = 1. +. Float.cos f in
rho *. Float.sin f, rho *. Float.cos f)
in
let xys3 =
Array.init 1000 ~f:(fun i ->
let f = Float.of_int i /. 20. in
let rho = 1.5 +. 0.2 *. Float.cos (5. *. f) in
rho *. Float.sin f, rho *. Float.cos f)
in
Ax.scatter ax ~s:4. ~c:Green ~marker:'o' ~alpha:0.5 xys1;
Ax.scatter ax ~s:4. ~c:Red ~marker:'X' xys2;
Ax.scatter ax ~c:Blue ~marker:'*' xys3;
Fig.suptitle fig "...scatter...";
Mpl.show ()
10 changes: 10 additions & 0 deletions src/matplotlib/fig_ax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module Ax = struct
let set_ylabel t label =
ignore (t.&("set_ylabel")[| Py.String.of_string label |])

let set_aspect t ~aspect =
let aspect =
match aspect with
| `auto -> Py.String.of_string "auto"
| `equal -> Py.String.of_string "equal"
| `f f -> Py.Float.of_float f
in
ignore (t.&("set_aspect")[| aspect |])

let grid t ?which ?axis b =
let keywords =
let b = Some ("b", Py.Bool.of_bool b) in
Expand Down Expand Up @@ -72,6 +81,7 @@ module Ax = struct

let plot = Mpl.plot
let hist = Mpl.hist
let scatter = Mpl.scatter
end

module Fig = struct
Expand Down
14 changes: 14 additions & 0 deletions src/matplotlib/fig_ax.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Ax : sig
val set_ylim : t -> bottom:float -> top:float -> unit
val set_xlabel : t -> string -> unit
val set_ylabel : t -> string -> unit
val set_aspect : t -> aspect:[`auto|`equal|`f of float] -> unit
val grid : t -> ?which:[`major|`minor|`both] -> ?axis:[`both|`x|`y] -> bool -> unit
val legend
: ?loc:
Expand Down Expand Up @@ -43,6 +44,19 @@ module Ax : sig
-> ?xs:float array list
-> float array
-> unit

val scatter
: t
-> ?s:float
-> ?c:Mpl.Color.t
(* Possible markers:
'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X'
*)
-> ?marker:char
-> ?alpha:float
-> ?linewidths:float
-> (float * float) array
-> unit
end

module Fig : sig
Expand Down
14 changes: 14 additions & 0 deletions src/matplotlib/mpl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,17 @@ let hist p ?label ?color ?bins ?orientation ?histtype ?xs ys =
| None -> [| float_array_to_python ys |]
in
ignore (Py.Module.get_function_with_keywords p "hist" args keywords)

let scatter p ?s ?c ?marker ?alpha ?linewidths xys =
let keywords =
List.filter_opt
[ Option.map c ~f:(fun c -> "c", Color.to_pyobject c)
; Option.map s ~f:(fun s -> "s", Py.Float.of_float s)
; Option.map marker ~f:(fun m -> "marker", String.of_char m |> Py.String.of_string)
; Option.map alpha ~f:(fun a -> "alpha", Py.Float.of_float a)
; Option.map linewidths ~f:(fun l -> "linewidths", Py.Float.of_float l)
]
in
let xs = Py.List.of_array_map (fun (x, _) -> Py.Float.of_float x) xys in
let ys = Py.List.of_array_map (fun (_, y) -> Py.Float.of_float y) xys in
ignore (Py.Module.get_function_with_keywords p "scatter" [| xs; ys |] keywords)
15 changes: 14 additions & 1 deletion src/matplotlib/mpl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ val plot
-> unit

val hist
: Py.Object.t
: Py.Object.t
-> ?label:string
-> ?color:Color.t
-> ?bins:int
Expand All @@ -95,3 +95,16 @@ val hist
-> ?xs:float array list
-> float array
-> unit

val scatter
: Py.Object.t
-> ?s:float
-> ?c:Color.t
(* Possible markers:
'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X'
*)
-> ?marker:char
-> ?alpha:float
-> ?linewidths:float
-> (float * float) array
-> unit
4 changes: 4 additions & 0 deletions src/matplotlib/pyplot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ let plot ?label ?color ?linewidth ?linestyle ?xs ys =
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

let scatter ?s ?c ?marker ?alpha ?linewidths xys =
let p = Mpl.pyplot_module () in
Mpl.scatter p ?s ?c ?marker ?alpha ?linewidths xys
12 changes: 12 additions & 0 deletions src/matplotlib/pyplot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ val hist
-> ?xs:float array list
-> float array
-> unit

val scatter
: ?s:float
-> ?c:Mpl.Color.t
(* Possible markers:
'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X'
*)
-> ?marker:char
-> ?alpha:float
-> ?linewidths:float
-> (float * float) array
-> unit

0 comments on commit 988ffbd

Please sign in to comment.