Skip to content

Commit 9f2b500

Browse files
author
Mohammed Sadique
committed
fixes
1 parent 08e480b commit 9f2b500

6 files changed

Lines changed: 171 additions & 68 deletions

File tree

lib/matplotex/figure/areal.ex

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ defmodule Matplotex.Figure.Areal do
141141
def materialized_by_region(figure) do
142142
figure
143143
|> Lead.set_regions_areal()
144+
|> Lead.transform_sizes()
144145
|> Cast.cast_xticks_by_region()
145146
|> Cast.cast_yticks_by_region()
146147
|> Cast.cast_hgrids_by_region()
@@ -184,6 +185,7 @@ defmodule Matplotex.Figure.Areal do
184185

185186
{x, y}
186187
end
188+
187189
def flatten_for_data(_, _bottom) do
188190
raise InputError, bottom: "Wrong data provided for opts bottom"
189191
end
@@ -439,18 +441,28 @@ defmodule Matplotex.Figure.Areal do
439441
%Dataset{dataset | transformed: transformed}
440442
end
441443

442-
defp maybe_wrap_with_sizes(transformed, %Dataset{sizes: sizes} = dataset) when length(transformed) == length(sizes) do
443-
{Enum.zip(transformed,sizes), dataset}
444+
defp maybe_wrap_with_sizes(transformed, %Dataset{sizes: sizes} = dataset)
445+
when length(transformed) == length(sizes) do
446+
{Enum.zip(transformed, sizes), dataset}
444447
end
445-
defp maybe_wrap_with_sizes(transformed, %Dataset{colors: colors, marker_size: marker_size} = dataset) when length(transformed) == length(colors) do
446-
{Enum.zip(transformed,List.duplicate(marker_size,length(transformed))),dataset }
448+
449+
defp maybe_wrap_with_sizes(
450+
transformed,
451+
%Dataset{colors: colors, marker_size: marker_size} = dataset
452+
)
453+
when length(transformed) == length(colors) do
454+
{Enum.zip(transformed, List.duplicate(marker_size, length(transformed))), dataset}
447455
end
456+
448457
defp maybe_wrap_with_sizes(transformed, dataset) do
449458
{transformed, dataset}
450459
end
451-
defp maybe_wrap_with_colors({transformed, %Dataset{colors: colors}}) when length(transformed) == length(colors) do
460+
461+
defp maybe_wrap_with_colors({transformed, %Dataset{colors: colors}})
462+
when length(transformed) == length(colors) do
452463
Enum.zip(transformed, colors)
453464
end
465+
454466
defp maybe_wrap_with_colors({transformed, _dataset}), do: transformed
455467

456468
defp transform_with_bottom(x, y, xlim, ylim, width, height, transition) when is_list(y) do

lib/matplotex/figure/areal/scatter.ex

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Matplotex.Figure.Areal.Scatter do
3232
def create(%Figure{axes: %__MODULE__{dataset: data} = axes} = figure, {x, y}, opts \\ []) do
3333
x = determine_numeric_value(x)
3434
y = determine_numeric_value(y)
35-
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts)|> Dataset.update_cmap()
35+
dataset = Dataset.cast(%Dataset{x: x, y: y}, opts) |> Dataset.update_cmap()
3636
datasets = data ++ [dataset]
3737
xydata = flatten_for_data(datasets)
3838

@@ -89,19 +89,26 @@ defmodule Matplotex.Figure.Areal.Scatter do
8989
elements = elements ++ line_elements
9090
%Figure{figure | axes: %{axes | element: elements}}
9191
end
92+
9293
def capture(%Dataset{transformed: transformed} = dataset, concurrency) do
9394
if concurrency do
9495
process_concurrently(transformed, concurrency, [[], dataset])
9596
else
9697
capture(transformed, [], dataset)
9798
end
9899
end
99-
def capture([{{{x, y}, s},color}| to_capture],captured, %Dataset{
100-
marker: marker,
101-
colors: colors,
102-
cmap: cmap
103-
}= dataset) do
104-
color = colors |> Enum.min_max()|>Garner.garn_color(color, cmap)
100+
101+
def capture(
102+
[{{{x, y}, s}, color} | to_capture],
103+
captured,
104+
%Dataset{
105+
marker: marker,
106+
colors: colors,
107+
cmap: cmap
108+
} = dataset
109+
) do
110+
color = colors |> Enum.min_max() |> Garner.garn_color(color, cmap)
111+
105112
capture(
106113
to_capture,
107114
captured ++
@@ -111,10 +118,15 @@ defmodule Matplotex.Figure.Areal.Scatter do
111118
dataset
112119
)
113120
end
114-
def capture([{{x, y}, s}| to_capture],captured, %Dataset{
115-
color: color,
116-
marker: marker,
117-
}= dataset) do
121+
122+
def capture(
123+
[{{x, y}, s} | to_capture],
124+
captured,
125+
%Dataset{
126+
color: color,
127+
marker: marker
128+
} = dataset
129+
) do
118130
capture(
119131
to_capture,
120132
captured ++
@@ -144,7 +156,6 @@ defmodule Matplotex.Figure.Areal.Scatter do
144156
)
145157
end
146158

147-
148159
def capture(_, captured, _), do: captured
149160

150161
@impl Areal

lib/matplotex/figure/dataset.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ defmodule Matplotex.Figure.Dataset do
3737
def update_cmap(%__MODULE__{cmap: cmap} = dataset) do
3838
%__MODULE__{dataset | cmap: Colormap.fetch_cmap(cmap)}
3939
end
40-
4140
end

lib/matplotex/figure/lead.ex

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,45 @@ defmodule Matplotex.Figure.Lead do
4646
ty = fig_height - fig_height * margin
4747
%Figure{figure | axes: %{axes | border: {lx, by, rx, ty}}}
4848
end
49+
4950
def transform_sizes(%Figure{axes: %{dataset: datasets} = axes} = figure) do
5051
datasets = transform_dataset_sizes(figure, datasets, [])
5152
%Figure{figure | axes: %{axes | dataset: datasets}}
5253
end
53-
defp transform_dataset_sizes(%Figure{axes: %{region_content: %Region{width: width_region_content, height: height_region_content}}}=figure, [%Dataset{sizes: sizes} = dataset | to_transorm], transformed) do
54+
55+
defp transform_dataset_sizes(_figure, [%Dataset{sizes: nil}| _to_transorm] = datasets, _),do: datasets
56+
57+
defp transform_dataset_sizes(
58+
%Figure{
59+
axes: %{
60+
region_content: %Region{width: width_region_content, height: height_region_content}
61+
}
62+
} = figure,
63+
[%Dataset{sizes: sizes} = dataset | to_transorm],
64+
transformed
65+
) when length(sizes) > 0 do
5466
content_area = width_region_content * height_region_content
5567
total_size = Enum.sum(sizes)
68+
5669
area_size_ratio =
57-
if total_size > 0 do
58-
(content_area / total_size) * 0.7
59-
else
60-
raise InputError, message: "Invalid sizes for fractionizing area"
61-
end
70+
if total_size > 0 do
71+
content_area / total_size * 2
72+
else
73+
raise InputError, message: "Invalid sizes for fractionizing area"
74+
end
6275

6376
sizes =
64-
sizes
65-
|> Nx.tensor()
66-
|> Nx.multiply(area_size_ratio)
67-
|> Nx.to_list()
77+
sizes
78+
|> Nx.tensor()
79+
|> Nx.multiply(area_size_ratio)
80+
|> Nx.to_list()
81+
6882
transform_dataset_sizes(figure, to_transorm, [%Dataset{dataset | sizes: sizes} | transformed])
6983
end
70-
defp transform_dataset_sizes(_, [], transformed), do: transformed
84+
defp transform_dataset_sizes(_figure, [%Dataset{sizes: []}| _to_transorm] = datasets, _), do: datasets
85+
7186

87+
defp transform_dataset_sizes(_, [], transformed), do: transformed
7288

7389
defp set_frame_size(%Figure{margin: margin, figsize: {fwidth, fheight}, axes: axes} = figure) do
7490
frame_size = {fwidth - fwidth * 2 * margin, fheight - fheight * 2 * margin}

test/matplotex/figure/areal/scatter_test.exs

Lines changed: 97 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,50 @@ defmodule Matplotex.Figure.Areal.ScatterTest do
1414
assert %Figure{axes: %{data: {x, _y}, element: elements}} = Scatter.materialize(figure)
1515
assert Enum.count(elements, fn elem -> elem.type == "plot.marker" end) == length(x)
1616
end
17+
1718
test "generates elements with various saizes if it passed a size attrbute" do
18-
x = [1,2,3,4,5]
19+
x = [1, 2, 3, 4, 5]
1920
y = [10, 20, 30, 40, 50]
2021
sizes = [1, 2, 3, 4, 5]
21-
assert %Figure{axes: %{element: elements}} = x|>Matplotex.scatter(y, sizes: sizes)|>Figure.materialize()
22-
[h | tail] = elements|>Enum.filter(fn x -> x.type == "plot.marker" end)|>Enum.map(fn x ->
23-
x.r
24-
end)
25-
refute Enum.all?(tail, fn x -> x == h end)
2622

23+
assert %Figure{axes: %{element: elements}} =
24+
x |> Matplotex.scatter(y, sizes: sizes) |> Figure.materialize()
25+
26+
[h | tail] =
27+
elements
28+
|> Enum.filter(fn x -> x.type == "plot.marker" end)
29+
|> Enum.map(fn x ->
30+
x.r
31+
end)
32+
33+
refute Enum.all?(tail, fn x -> x == h end)
2734
end
2835

2936
test "generates elements with various saizes and colors if it passed a size and color attrbute" do
30-
x = [1,2,3,4,5]
37+
x = [1, 2, 3, 4, 5]
3138
y = [10, 20, 30, 40, 50]
3239
sizes = [1, 2, 3, 4, 5]
33-
assert %Figure{axes: %{element: elements}} = x|>Matplotex.scatter(y, sizes: sizes, colors: sizes)|>Figure.materialize()
34-
[h | tail] = elements|>Enum.filter(fn x -> x.type == "plot.marker" end)|>Enum.map(fn x ->
35-
x.r
36-
end)
37-
38-
refute Enum.all?(tail, fn x -> x == h end)
39-
[h | tail] = elements|>Enum.filter(fn x -> x.type == "plot.marker" end)|>Enum.map(fn x ->
40-
x.fill
41-
end)
42-
refute Enum.all?(tail, fn x -> x == h end)
40+
41+
assert %Figure{axes: %{element: elements}} =
42+
x |> Matplotex.scatter(y, sizes: sizes, colors: sizes) |> Figure.materialize()
43+
44+
[h | tail] =
45+
elements
46+
|> Enum.filter(fn x -> x.type == "plot.marker" end)
47+
|> Enum.map(fn x ->
48+
x.r
49+
end)
50+
51+
refute Enum.all?(tail, fn x -> x == h end)
52+
53+
[h | tail] =
54+
elements
55+
|> Enum.filter(fn x -> x.type == "plot.marker" end)
56+
|> Enum.map(fn x ->
57+
x.fill
58+
end)
59+
60+
refute Enum.all?(tail, fn x -> x == h end)
4361
end
4462
end
4563

@@ -53,56 +71,99 @@ defmodule Matplotex.Figure.Areal.ScatterTest do
5371
assert length(ticks) == 6
5472
end
5573
end
74+
5675
describe "do_transform" do
5776
test "zips transformed values with sizes if the dataset contains sizes in eaqual size" do
58-
x = [1,2,3,4,5]
77+
x = [1, 2, 3, 4, 5]
5978
y = [10, 20, 30, 40, 50]
6079
sizes = [1, 2, 3, 4, 5]
6180
width = 2
6281
height = 2
63-
assert %Figure{axes: %{dataset: [dataset]}} = x|>Matplotex.scatter(y,figsize: {width, height}, sizes: sizes)
64-
assert %Dataset{transformed: transformed} = Areal.do_transform(dataset, Enum.min_max(x), Enum.min_max(y),width, height, {0,0})
65-
assert Enum.all?(transformed, &match?({{_,_},_}, &1))
6682

83+
assert %Figure{axes: %{dataset: [dataset]}} =
84+
x |> Matplotex.scatter(y, figsize: {width, height}, sizes: sizes)
85+
86+
assert %Dataset{transformed: transformed} =
87+
Areal.do_transform(
88+
dataset,
89+
Enum.min_max(x),
90+
Enum.min_max(y),
91+
width,
92+
height,
93+
{0, 0}
94+
)
95+
96+
assert Enum.all?(transformed, &match?({{_, _}, _}, &1))
6797
end
98+
6899
test "zips transformed values with marker size if colors exist without sizes" do
69-
x = [1,2,3,4,5]
100+
x = [1, 2, 3, 4, 5]
70101
y = [10, 20, 30, 40, 50]
71102
colors = [1, 2, 3, 4, 5]
72103
width = 2
73104
height = 2
74-
assert %Figure{axes: %{dataset: [%Dataset{marker_size: _marker_size} = dataset]}} = x|>Matplotex.scatter(y,figsize: {width, height}, colors: colors)
75-
assert %Dataset{transformed: transformed} = Areal.do_transform(dataset, Enum.min_max(x), Enum.min_max(y),width, height, {0,0})
76-
assert Enum.all?(transformed, &match?({{{_,_},_marker_size},_}, &1))
105+
106+
assert %Figure{axes: %{dataset: [%Dataset{marker_size: _marker_size} = dataset]}} =
107+
x |> Matplotex.scatter(y, figsize: {width, height}, colors: colors)
108+
109+
assert %Dataset{transformed: transformed} =
110+
Areal.do_transform(
111+
dataset,
112+
Enum.min_max(x),
113+
Enum.min_max(y),
114+
width,
115+
height,
116+
{0, 0}
117+
)
118+
119+
assert Enum.all?(transformed, &match?({{{_, _}, _marker_size}, _}, &1))
77120
end
121+
78122
test "zips transformed values with colors if the dataset contanis colors" do
79-
x = [1,2,3,4,5]
123+
x = [1, 2, 3, 4, 5]
80124
y = [10, 20, 30, 40, 50]
81125
colors = [1, 2, 3, 4, 5]
82126
width = 2
83127
height = 2
84-
assert %Figure{axes: %{dataset: [dataset]}} = x|>Matplotex.scatter(y,figsize: {width, height}, colors: colors)
85-
assert %Dataset{transformed: transformed} = Areal.do_transform(dataset, Enum.min_max(x), Enum.min_max(y),width, height, {0,0})
86128

87-
assert Enum.all?(transformed, &match?({{{_,_},_},_}, &1))
129+
assert %Figure{axes: %{dataset: [dataset]}} =
130+
x |> Matplotex.scatter(y, figsize: {width, height}, colors: colors)
88131

132+
assert %Dataset{transformed: transformed} =
133+
Areal.do_transform(
134+
dataset,
135+
Enum.min_max(x),
136+
Enum.min_max(y),
137+
width,
138+
height,
139+
{0, 0}
140+
)
141+
142+
assert Enum.all?(transformed, &match?({{{_, _}, _}, _}, &1))
89143
end
90144

91145
test "zips both size and colors if the dataset contains size and color" do
92-
x = [1,2,3,4,5]
146+
x = [1, 2, 3, 4, 5]
93147
y = [10, 20, 30, 40, 50]
94148
sizes = [1, 2, 3, 4, 5]
95149
colors = [1, 2, 3, 4, 5]
96150
width = 2
97151
height = 2
98-
assert %Figure{axes: %{dataset: [dataset]}} = x|>Matplotex.scatter(y,figsize: {width, height}, sizes: sizes, colors: colors)
99-
assert %Dataset{transformed: transformed} = Areal.do_transform(dataset, Enum.min_max(x), Enum.min_max(y),width, height, {0,0})
100152

101-
assert Enum.all?(transformed, &match?({{{_,_},_},_}, &1))
153+
assert %Figure{axes: %{dataset: [dataset]}} =
154+
x |> Matplotex.scatter(y, figsize: {width, height}, sizes: sizes, colors: colors)
102155

103-
end
156+
assert %Dataset{transformed: transformed} =
157+
Areal.do_transform(
158+
dataset,
159+
Enum.min_max(x),
160+
Enum.min_max(y),
161+
width,
162+
height,
163+
{0, 0}
164+
)
104165

166+
assert Enum.all?(transformed, &match?({{{_, _}, _}, _}, &1))
167+
end
105168
end
106-
107-
108169
end

test/matplotex/figure/lead_test.exs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,18 +263,22 @@ defmodule Matplotex.Figure.LeadTest do
263263

264264
describe "transform_sizes/1" do
265265
test "converts sizes to equivalent radius to the buble" do
266-
x = [1,2,3,4,5]
266+
x = [1, 2, 3, 4, 5]
267267
y = [10, 20, 30, 40, 50]
268268
sizes = [1, 2, 3, 4, 5]
269269
width = 2
270270
height = 2
271-
figure = x|>Matplotex.scatter(y,figsize: {width, height}, sizes: sizes)|> Lead.set_regions_areal()
271+
272+
figure =
273+
x
274+
|> Matplotex.scatter(y, figsize: {width, height}, sizes: sizes)
275+
|> Lead.set_regions_areal()
272276

273277
assert %Figure{axes: %{dataset: [%Dataset{sizes: transformed_sizes}]}} =
274278
Lead.transform_sizes(figure)
275279

276280
assert length(transformed_sizes) == length(sizes)
277-
assert Enum.sum(transformed_sizes) < (width * height) * 0.7
281+
assert Enum.sum(transformed_sizes) < width * height * 1.5
278282
end
279283
end
280284
end

0 commit comments

Comments
 (0)