Skip to content

Commit 59eb57f

Browse files
Merge pull request #39 from BigThinkcode/step_plot
Step plot
2 parents ab77903 + 68fcab0 commit 59eb57f

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

lib/matplotex.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,23 @@ defmodule Matplotex do
588588
Spline.create(figure, {x, y}, opts)
589589
end
590590

591+
@doc """
592+
Creates a step plot with the given parameters
593+
## Parameters
594+
595+
- `x` (list): A list of x-coordinates for the data points.
596+
- `y` (list): A list of y-coordinates corresponding to the x-coordinates.
597+
```elixir
598+
x = [1,2,3,4,5]
599+
y = [1,4,9,16,25]
600+
Matplotex.step(x, y, color: "blue")
601+
|>Matplotex.set_xlabel("Numbers")
602+
|>Matplotex.set_ylabel("Squares")
603+
```
604+
"""
605+
def step(x, y, opts), do: Matplotex.Figure.Areal.Step.create(x, y, opts)
606+
def step(figure, x, y, opts), do: Matplotex.Figure.Areal.Step.create(figure, x, y, opts)
607+
591608
@doc """
592609
Sets X labels for the graph with given font details
593610
@@ -597,7 +614,6 @@ defmodule Matplotex do
597614
%Matplotex.Figure{}
598615
599616
"""
600-
601617
@spec set_xlabel(Figure.t(), String.t()) :: Figure.t()
602618
def set_xlabel(figure, label, opts \\ []) do
603619
Figure.add_label(figure, {:x, label}, opts)

lib/matplotex/figure/areal/step.ex

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Matplotex.Figure.Areal.Step do
2+
3+
alias Matplotex.Figure.Areal.LinePlot
4+
alias Matplotex.Figure
5+
alias Matplotex.InputError
6+
7+
def create(x, y, opts) do
8+
{x, y} = step_segments(x, y)
9+
LinePlot.create(%Figure{axes: %LinePlot{}}, {x, y}, opts)
10+
end
11+
def create(%Figure{} = figure, x, y, opts) do
12+
{x, y} = step_segments(x, y)
13+
LinePlot.create(figure, {x, y}, opts)
14+
end
15+
defp step_segments(x, y) do
16+
x = horizontal_segments(x)
17+
y = vertical_segments(y)
18+
{x,y}
19+
end
20+
defp horizontal_segments(data) when is_list(data) do
21+
first_value = List.first(data)
22+
second_last_index = length(data) - 1
23+
repeated = data|>Enum.slice(1..second_last_index)|>List.duplicate(2)|> List.flatten()
24+
[first_value | repeated]|>Enum.sort()
25+
end
26+
defp horizontal_segments(_data), do: list_error(:y)
27+
28+
defp vertical_segments(data) when is_list(data) do
29+
data|>List.duplicate(2)|>List.flatten()|>Enum.sort()
30+
end
31+
defp vertical_segments(_data), do: list_error(:y)
32+
33+
defp list_error(arg) do
34+
raise InputError, message: "Expected a list for arg: #{arg}"
35+
end
36+
37+
end

test/matplotex_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,17 @@ defmodule MatplotexTest do
304304
assert data1.y == y
305305
end
306306
end
307+
308+
describe "step" do
309+
test "creates a figure for step plot" do
310+
x = [1, 2, 3, 4, 5]
311+
y = [1,4,9,16,25]
312+
assert %Figure{axes: %{dataset: [data1], label: label}} =
313+
Matplotex.step(x, y, x_label: "X", y_label: "Y")
314+
assert label.x == "X"
315+
assert label.y == "Y"
316+
assert data1.x|>Enum.uniq() == x
317+
assert data1.y|>Enum.uniq() == y
318+
end
319+
end
307320
end

0 commit comments

Comments
 (0)