Skip to content

Commit 7d65748

Browse files
Merge pull request #26 from BigThinkcode/hexdocs
Hexdocs
2 parents e16c83d + ebbb43a commit 7d65748

File tree

8 files changed

+551
-85
lines changed

8 files changed

+551
-85
lines changed

lib/matplotex.ex

Lines changed: 454 additions & 40 deletions
Large diffs are not rendered by default.

lib/matplotex/element/polygon.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ defmodule Matplotex.Element.Polygon do
1515
)
1616
end
1717

18-
1918
defp assemble_point(%{points: point}) do
2019
for {x, y} <- point do
2120
"#{to_pixel(x)},#{to_pixel(y)} "

lib/matplotex/figure/areal/histogram.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ defmodule Matplotex.Figure.Areal.Histogram do
2424

2525
@impl Areal
2626
def create(
27-
%Figure{axes: %__MODULE__{} = axes, rc_params: rc_params} = figure,
27+
%Figure{axes: %__MODULE__{dataset: datasets} = axes, rc_params: rc_params} = figure,
2828
{data, bins},
2929
opts
3030
) do
3131
{x, y} = bins_and_hists(data, bins)
3232

3333
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
34+
datasets = datasets ++ [dataset]
35+
36+
xydata = flatten_for_data(datasets)
3437

3538
%Figure{
3639
figure
37-
| axes: %__MODULE__{axes | data: {x, y}, dataset: [dataset]},
40+
| axes: %__MODULE__{axes | data: xydata, dataset: datasets},
3841
rc_params: %RcParams{rc_params | y_padding: @make_it_zero}
3942
}
4043
|> PlotOptions.set_options_in_figure(opts)

lib/matplotex/figure/areal/spline.ex

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule Matplotex.Figure.Areal.Spline do
88
alias Matplotex.Figure.TwoD
99
alias Matplotex.Figure
1010

11-
1211
use Areal
1312

1413
frame(
@@ -21,6 +20,7 @@ defmodule Matplotex.Figure.Areal.Spline do
2120
region_legend: %Region{},
2221
region_content: %Region{}
2322
)
23+
2424
@impl Areal
2525
def create(
2626
%Figure{axes: %__MODULE__{dataset: data} = axes} = figure,
@@ -40,29 +40,34 @@ defmodule Matplotex.Figure.Areal.Spline do
4040

4141
@impl Areal
4242
def materialize(figure) do
43-
figure
44-
|>__MODULE__.materialized_by_region()
45-
|> materialize_spline()
46-
43+
figure
44+
|> __MODULE__.materialized_by_region()
45+
|> materialize_spline()
4746
end
48-
defp materialize_spline(%Figure{axes:
49-
%{
50-
dataset: data,
51-
limit: %{x: xlim, y: ylim},
52-
region_content: %Region{
53-
x: x_region_content,
54-
y: y_region_content,
55-
width: width_region_content,
56-
height: height_region_content
57-
},
58-
element: elements
59-
} = axes,
60-
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}} = figure) do
47+
48+
defp materialize_spline(
49+
%Figure{
50+
axes:
51+
%{
52+
dataset: data,
53+
limit: %{x: xlim, y: ylim},
54+
region_content: %Region{
55+
x: x_region_content,
56+
y: y_region_content,
57+
width: width_region_content,
58+
height: height_region_content
59+
},
60+
element: elements
61+
} = axes,
62+
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
63+
} = figure
64+
) do
6165
x_padding_value = width_region_content * x_padding
6266
y_padding_value = height_region_content * y_padding
6367
shrinked_width_region_content = width_region_content - x_padding_value * 2
6468
shrinked_height_region_content = height_region_content - y_padding_value * 2
6569
transition = {x_region_content + x_padding_value, y_region_content + y_padding_value}
70+
6671
line_elements =
6772
data
6873
|> Enum.map(fn dataset ->
@@ -83,19 +88,33 @@ defmodule Matplotex.Figure.Areal.Spline do
8388
%Figure{figure | axes: %{axes | element: elements}}
8489
end
8590

86-
87-
88-
defp capture(%Dataset{transformed: transformed, color: color,edge_color: edge_color, line_width: stroke_width}, move_to_def) do
91+
defp capture(
92+
%Dataset{
93+
transformed: transformed,
94+
color: color,
95+
edge_color: edge_color,
96+
line_width: stroke_width
97+
},
98+
move_to_def
99+
) do
89100
{moveto, transformed} = List.pop_at(transformed, 0, move_to_def)
90101
cubic = Enum.slice(transformed, 0..2)
91102
smooths = blend(transformed, 3)
92-
%Spline{type: "figure.spline", moveto: moveto, cubic: cubic, smooths: smooths, fill: color, stroke: edge_color, stroke_width: stroke_width}
103+
104+
%Spline{
105+
type: "figure.spline",
106+
moveto: moveto,
107+
cubic: cubic,
108+
smooths: smooths,
109+
fill: color,
110+
stroke: edge_color,
111+
stroke_width: stroke_width
112+
}
93113
end
94114

95115
defp blend(smooths, start_from) do
96-
smooths
97-
|>Enum.slice(start_from..-1//1)
98-
|>Enum.chunk_every(2)
99-
116+
smooths
117+
|> Enum.slice(start_from..-1//1)
118+
|> Enum.chunk_every(2)
100119
end
101120
end

lib/matplotex/figure/font.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Matplotex.Figure.Font do
22
@default_font_color "black"
33
@default_font_family "Arial, Verdana, sans-serif"
44
@default_font_style "normal"
5-
@default_font_size 16
5+
@default_font_size 12
66
@default_font_weight "normal"
77
@font_unit "pt"
88
@pt_to_inch_ratio 1 / 72

lib/matplotex/figure/two_d.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
defmodule Matplotex.Figure.TwoD do
22
defstruct [:x, :y]
33

4-
def update(twod, opts, context) do
4+
def update(%__MODULE__{x: x, y: y} = twod, opts, context) do
55
%__MODULE__{
66
twod
7-
| x: fetch_from_opts(opts, :x, context),
8-
y: fetch_from_opts(opts, :y, context)
7+
| x: fetch_from_opts(opts, :x, context) || x,
8+
y: fetch_from_opts(opts, :y, context) || y
99
}
1010
end
1111

lib/matplotex/helpers.ex

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,46 @@ defmodule Matplotex.Helpers do
325325
|> copy()
326326
end
327327

328+
def multi_hist() do
329+
values1 =
330+
Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list()
331+
332+
bins = 30
333+
values2 = Nx.Random.key(13) |> Nx.Random.normal(0, 1, shape: {500}) |> elem(0) |> Nx.to_list()
334+
335+
Matplotex.hist(values1, bins,
336+
x_label: "Value",
337+
y_label: "Frequency",
338+
title: "Histogram",
339+
color: "blue",
340+
edge_color: "black",
341+
alpha: 0.7,
342+
x_ticks_count: 9
343+
)
344+
|> Matplotex.hist(values2, bins, color: "red")
345+
|> Matplotex.show()
346+
|> copy()
347+
end
348+
328349
def spline() do
329350
x_nx = Nx.linspace(0, 10, n: 100)
330351
x = Nx.to_list(x_nx)
331352
y = x_nx |> Nx.sin() |> Nx.to_list()
332353

333-
334354
Matplotex.spline(x, y, x_label: "X", y_label: "Y", edge_color: "green")
335355
|> Matplotex.show()
336356
|> copy()
337357
end
338358

359+
def multi_spline() do
360+
x_nx = Nx.linspace(0, 10, n: 100)
361+
x = Nx.to_list(x_nx)
362+
y1 = x_nx |> Nx.sin() |> Nx.to_list()
363+
y2 = x_nx |> Nx.cos() |> Nx.to_list()
364+
365+
Matplotex.spline(x, y1, x_label: "X", y_label: "Y", edge_color: "green")
366+
|> Matplotex.spline(x, y2, x_label: "X", y_label: "Y", edge_color: "red")
367+
|> Matplotex.show()
368+
|> copy()
369+
end
339370
end

test/matplotex/figure/areal/spline_test.exs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ defmodule Matplotex.Figure.Areal.SplineTest do
22
use Matplotex.PlotCase
33
alias Matplotex.Figure
44
alias Matplotex.Figure.Areal.Spline
5-
setup do
6-
x_nx = Nx.linspace(0, 10, n: 100)
7-
x = Nx.to_list(x_nx)
8-
y = x_nx |> Nx.sin() |> Nx.to_list()
95

10-
figure = Matplotex.spline(x, y, x_label: "X", y_label: "Y")
11-
{:ok, %{figure: figure}}
12-
end
6+
setup do
7+
x_nx = Nx.linspace(0, 10, n: 100)
8+
x = Nx.to_list(x_nx)
9+
y = x_nx |> Nx.sin() |> Nx.to_list()
1310

11+
figure = Matplotex.spline(x, y, x_label: "X", y_label: "Y")
12+
{:ok, %{figure: figure}}
13+
end
1414

15-
test "adds a spline element in a figure",%{figure: figure} do
16-
assert %Figure{axes: %Spline{element: elements}} =Spline.materialize(figure)
17-
assert Enum.any?(elements, &(&1.type=="figure.spline"))
18-
end
15+
test "adds a spline element in a figure", %{figure: figure} do
16+
assert %Figure{axes: %Spline{element: elements}} = Spline.materialize(figure)
17+
assert Enum.any?(elements, &(&1.type == "figure.spline"))
18+
end
1919
end

0 commit comments

Comments
 (0)