Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
494 changes: 454 additions & 40 deletions lib/matplotex.ex

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion lib/matplotex/element/polygon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule Matplotex.Element.Polygon do
)
end


defp assemble_point(%{points: point}) do
for {x, y} <- point do
"#{to_pixel(x)},#{to_pixel(y)} "
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotex/figure/areal/histogram.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ defmodule Matplotex.Figure.Areal.Histogram do

@impl Areal
def create(
%Figure{axes: %__MODULE__{} = axes, rc_params: rc_params} = figure,
%Figure{axes: %__MODULE__{dataset: datasets} = axes, rc_params: rc_params} = figure,
{data, bins},
opts
) do
{x, y} = bins_and_hists(data, bins)

dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)
datasets = datasets ++ [dataset]

xydata = flatten_for_data(datasets)

%Figure{
figure
| axes: %__MODULE__{axes | data: {x, y}, dataset: [dataset]},
| axes: %__MODULE__{axes | data: xydata, dataset: datasets},
rc_params: %RcParams{rc_params | y_padding: @make_it_zero}
}
|> PlotOptions.set_options_in_figure(opts)
Expand Down
71 changes: 45 additions & 26 deletions lib/matplotex/figure/areal/spline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ defmodule Matplotex.Figure.Areal.Spline do
alias Matplotex.Figure.TwoD
alias Matplotex.Figure


use Areal

frame(
Expand All @@ -21,6 +20,7 @@ defmodule Matplotex.Figure.Areal.Spline do
region_legend: %Region{},
region_content: %Region{}
)

@impl Areal
def create(
%Figure{axes: %__MODULE__{dataset: data} = axes} = figure,
Expand All @@ -40,29 +40,34 @@ defmodule Matplotex.Figure.Areal.Spline do

@impl Areal
def materialize(figure) do
figure
|>__MODULE__.materialized_by_region()
|> materialize_spline()

figure
|> __MODULE__.materialized_by_region()
|> materialize_spline()
end
defp materialize_spline(%Figure{axes:
%{
dataset: data,
limit: %{x: xlim, y: ylim},
region_content: %Region{
x: x_region_content,
y: y_region_content,
width: width_region_content,
height: height_region_content
},
element: elements
} = axes,
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}} = figure) do

defp materialize_spline(
%Figure{
axes:
%{
dataset: data,
limit: %{x: xlim, y: ylim},
region_content: %Region{
x: x_region_content,
y: y_region_content,
width: width_region_content,
height: height_region_content
},
element: elements
} = axes,
rc_params: %RcParams{x_padding: x_padding, y_padding: y_padding}
} = figure
) do
x_padding_value = width_region_content * x_padding
y_padding_value = height_region_content * y_padding
shrinked_width_region_content = width_region_content - x_padding_value * 2
shrinked_height_region_content = height_region_content - y_padding_value * 2
transition = {x_region_content + x_padding_value, y_region_content + y_padding_value}

line_elements =
data
|> Enum.map(fn dataset ->
Expand All @@ -83,19 +88,33 @@ defmodule Matplotex.Figure.Areal.Spline do
%Figure{figure | axes: %{axes | element: elements}}
end



defp capture(%Dataset{transformed: transformed, color: color,edge_color: edge_color, line_width: stroke_width}, move_to_def) do
defp capture(
%Dataset{
transformed: transformed,
color: color,
edge_color: edge_color,
line_width: stroke_width
},
move_to_def
) do
{moveto, transformed} = List.pop_at(transformed, 0, move_to_def)
cubic = Enum.slice(transformed, 0..2)
smooths = blend(transformed, 3)
%Spline{type: "figure.spline", moveto: moveto, cubic: cubic, smooths: smooths, fill: color, stroke: edge_color, stroke_width: stroke_width}

%Spline{
type: "figure.spline",
moveto: moveto,
cubic: cubic,
smooths: smooths,
fill: color,
stroke: edge_color,
stroke_width: stroke_width
}
end

defp blend(smooths, start_from) do
smooths
|>Enum.slice(start_from..-1//1)
|>Enum.chunk_every(2)

smooths
|> Enum.slice(start_from..-1//1)
|> Enum.chunk_every(2)
end
end
2 changes: 1 addition & 1 deletion lib/matplotex/figure/font.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Matplotex.Figure.Font do
@default_font_color "black"
@default_font_family "Arial, Verdana, sans-serif"
@default_font_style "normal"
@default_font_size 16
@default_font_size 12
@default_font_weight "normal"
@font_unit "pt"
@pt_to_inch_ratio 1 / 72
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotex/figure/two_d.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Matplotex.Figure.TwoD do
defstruct [:x, :y]

def update(twod, opts, context) do
def update(%__MODULE__{x: x, y: y} = twod, opts, context) do
%__MODULE__{
twod
| x: fetch_from_opts(opts, :x, context),
y: fetch_from_opts(opts, :y, context)
| x: fetch_from_opts(opts, :x, context) || x,
y: fetch_from_opts(opts, :y, context) || y
}
end

Expand Down
33 changes: 32 additions & 1 deletion lib/matplotex/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,46 @@ defmodule Matplotex.Helpers do
|> copy()
end

def multi_hist() do
values1 =
Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list()

bins = 30
values2 = Nx.Random.key(13) |> Nx.Random.normal(0, 1, shape: {500}) |> elem(0) |> Nx.to_list()

Matplotex.hist(values1, bins,
x_label: "Value",
y_label: "Frequency",
title: "Histogram",
color: "blue",
edge_color: "black",
alpha: 0.7,
x_ticks_count: 9
)
|> Matplotex.hist(values2, bins, color: "red")
|> Matplotex.show()
|> copy()
end

def spline() do
x_nx = Nx.linspace(0, 10, n: 100)
x = Nx.to_list(x_nx)
y = x_nx |> Nx.sin() |> Nx.to_list()


Matplotex.spline(x, y, x_label: "X", y_label: "Y", edge_color: "green")
|> Matplotex.show()
|> copy()
end

def multi_spline() do
x_nx = Nx.linspace(0, 10, n: 100)
x = Nx.to_list(x_nx)
y1 = x_nx |> Nx.sin() |> Nx.to_list()
y2 = x_nx |> Nx.cos() |> Nx.to_list()

Matplotex.spline(x, y1, x_label: "X", y_label: "Y", edge_color: "green")
|> Matplotex.spline(x, y2, x_label: "X", y_label: "Y", edge_color: "red")
|> Matplotex.show()
|> copy()
end
end
22 changes: 11 additions & 11 deletions test/matplotex/figure/areal/spline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ defmodule Matplotex.Figure.Areal.SplineTest do
use Matplotex.PlotCase
alias Matplotex.Figure
alias Matplotex.Figure.Areal.Spline
setup do
x_nx = Nx.linspace(0, 10, n: 100)
x = Nx.to_list(x_nx)
y = x_nx |> Nx.sin() |> Nx.to_list()

figure = Matplotex.spline(x, y, x_label: "X", y_label: "Y")
{:ok, %{figure: figure}}
end
setup do
x_nx = Nx.linspace(0, 10, n: 100)
x = Nx.to_list(x_nx)
y = x_nx |> Nx.sin() |> Nx.to_list()

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

test "adds a spline element in a figure",%{figure: figure} do
assert %Figure{axes: %Spline{element: elements}} =Spline.materialize(figure)
assert Enum.any?(elements, &(&1.type=="figure.spline"))
end
test "adds a spline element in a figure", %{figure: figure} do
assert %Figure{axes: %Spline{element: elements}} = Spline.materialize(figure)
assert Enum.any?(elements, &(&1.type == "figure.spline"))
end
end
Loading