From 4d8bdc1bbb9f746a79a8041a699311324bc3dd16 Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Wed, 22 Jan 2025 16:32:39 +0530 Subject: [PATCH 1/2] step plot --- lib/matplotex.ex | 3 +++ lib/matplotex/figure/areal/step.ex | 37 ++++++++++++++++++++++++++++++ test/matplotex_test.exs | 13 +++++++++++ 3 files changed, 53 insertions(+) create mode 100644 lib/matplotex/figure/areal/step.ex diff --git a/lib/matplotex.ex b/lib/matplotex.ex index 0987efe..d255a1c 100644 --- a/lib/matplotex.ex +++ b/lib/matplotex.ex @@ -598,6 +598,9 @@ defmodule Matplotex do """ + def step(x, y, opts), do: Matplotex.Figure.Areal.Step.create(x, y, opts) + def step(figure, x, y, opts), do: Matplotex.Figure.Areal.Step.create(figure, x, y, opts) + @spec set_xlabel(Figure.t(), String.t()) :: Figure.t() def set_xlabel(figure, label, opts \\ []) do Figure.add_label(figure, {:x, label}, opts) diff --git a/lib/matplotex/figure/areal/step.ex b/lib/matplotex/figure/areal/step.ex new file mode 100644 index 0000000..0783871 --- /dev/null +++ b/lib/matplotex/figure/areal/step.ex @@ -0,0 +1,37 @@ +defmodule Matplotex.Figure.Areal.Step do + + alias Matplotex.Figure.Areal.LinePlot + alias Matplotex.Figure + alias Matplotex.InputError + + def create(x, y, opts) do + {x, y} = step_segments(x, y) + LinePlot.create(%Figure{axes: %LinePlot{}}, {x, y}, opts) + end + def create(%Figure{} = figure, x, y, opts) do + {x, y} = step_segments(x, y) + LinePlot.create(figure, {x, y}, opts) + end + defp step_segments(x, y) do + x = horizontal_segments(x) + y = vertical_segments(y) + {x,y} + end + defp horizontal_segments(data) when is_list(data) do + first_value = List.first(data) + second_last_index = length(data) - 1 + repeated = data|>Enum.slice(1..second_last_index)|>List.duplicate(2)|> List.flatten() + [first_value | repeated]|>Enum.sort() + end + defp horizontal_segments(_data), do: list_error(:y) + + defp vertical_segments(data) when is_list(data) do + data|>List.duplicate(2)|>List.flatten()|>Enum.sort() + end + defp vertical_segments(_data), do: list_error(:y) + + defp list_error(arg) do + raise InputError, message: "Expected a list for arg: #{arg}" + end + +end diff --git a/test/matplotex_test.exs b/test/matplotex_test.exs index 5cd749d..8924fc9 100644 --- a/test/matplotex_test.exs +++ b/test/matplotex_test.exs @@ -304,4 +304,17 @@ defmodule MatplotexTest do assert data1.y == y end end + + describe "step" do + test "creates a figure for step plot" do + x = [1, 2, 3, 4, 5] + y = [1,4,9,16,25] + assert %Figure{axes: %{dataset: [data1], label: label}} = + Matplotex.spline(x, y, x_label: "X", y_label: "Y") + assert label.x == "X" + assert label.y == "Y" + assert data1.x|>Enum.uniq() == x + assert data1.y|>Enum.uniq() == y + end + end end From 68fcab079e75e7ee7c05dd0fd6c5f7e1f3d1aabb Mon Sep 17 00:00:00 2001 From: Mohammed Sadique Date: Wed, 22 Jan 2025 16:42:46 +0530 Subject: [PATCH 2/2] doc --- lib/matplotex.ex | 21 +++++++++++++++++---- test/matplotex_test.exs | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/matplotex.ex b/lib/matplotex.ex index d255a1c..0c7150d 100644 --- a/lib/matplotex.ex +++ b/lib/matplotex.ex @@ -588,6 +588,23 @@ defmodule Matplotex do Spline.create(figure, {x, y}, opts) end + @doc """ + Creates a step plot with the given parameters + ## Parameters + + - `x` (list): A list of x-coordinates for the data points. + - `y` (list): A list of y-coordinates corresponding to the x-coordinates. + ```elixir + x = [1,2,3,4,5] + y = [1,4,9,16,25] + Matplotex.step(x, y, color: "blue") + |>Matplotex.set_xlabel("Numbers") + |>Matplotex.set_ylabel("Squares") + ``` + """ + def step(x, y, opts), do: Matplotex.Figure.Areal.Step.create(x, y, opts) + def step(figure, x, y, opts), do: Matplotex.Figure.Areal.Step.create(figure, x, y, opts) + @doc """ Sets X labels for the graph with given font details @@ -597,10 +614,6 @@ defmodule Matplotex do %Matplotex.Figure{} """ - - def step(x, y, opts), do: Matplotex.Figure.Areal.Step.create(x, y, opts) - def step(figure, x, y, opts), do: Matplotex.Figure.Areal.Step.create(figure, x, y, opts) - @spec set_xlabel(Figure.t(), String.t()) :: Figure.t() def set_xlabel(figure, label, opts \\ []) do Figure.add_label(figure, {:x, label}, opts) diff --git a/test/matplotex_test.exs b/test/matplotex_test.exs index 8924fc9..f540799 100644 --- a/test/matplotex_test.exs +++ b/test/matplotex_test.exs @@ -310,7 +310,7 @@ defmodule MatplotexTest do x = [1, 2, 3, 4, 5] y = [1,4,9,16,25] assert %Figure{axes: %{dataset: [data1], label: label}} = - Matplotex.spline(x, y, x_label: "X", y_label: "Y") + Matplotex.step(x, y, x_label: "X", y_label: "Y") assert label.x == "X" assert label.y == "Y" assert data1.x|>Enum.uniq() == x