Skip to content

Commit fd2b653

Browse files
author
Mohammed Sadique
committed
updated color scheme
1 parent 327f293 commit fd2b653

5 files changed

Lines changed: 32 additions & 19 deletions

File tree

lib/matplotex/figure/areal/plot_options.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Matplotex.Figure.Areal.PlotOptions do
22
@moduledoc false
3+
alias Matplotex.Colorscheme.Colormap
34
alias Matplotex.Figure
45
alias Matplotex.Figure.TwoD
56
alias Matplotex.Figure.RcParams
@@ -15,11 +16,11 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
1516
:region_legend,
1617
:region_color_bar
1718
]
19+
@default_cmap :viridis
1820

1921
@spec set_options_in_figure(Figure.t(), keyword()) :: Figure.t()
2022
def set_options_in_figure(%Figure{} = figure, opts) do
2123
opts = sanitize(opts)
22-
2324
figure
2425
|> cast_figure(opts)
2526
|> cast_axes(opts)
@@ -31,11 +32,14 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
3132
end
3233

3334
defp cast_axes(%Figure{axes: axes} = figure, opts) do
35+
3436
opts = Keyword.delete(opts, :label)
37+
cmap = Keyword.get(opts, :cmap)
38+
colors = Keyword.get(opts, :colors)
3539

3640
%Figure{
3741
figure
38-
| axes: axes |> struct(opts) |> cast_two_d_structs(opts)
42+
| axes: axes |> struct(opts) |> cast_two_d_structs(opts)|> update_cmap(cmap, colors)
3943
}
4044
end
4145

@@ -58,4 +62,14 @@ defmodule Matplotex.Figure.Areal.PlotOptions do
5862
defp sanitize(opts) do
5963
Keyword.drop(opts, @immutable_keys)
6064
end
65+
defp update_cmap(axes, nil, colors) when is_list(colors) do
66+
67+
%{axes | cmap: Colormap.fetch_cmap(@default_cmap)}
68+
end
69+
70+
defp update_cmap(axes, cmap, colors) when is_list(colors) do
71+
%{axes | cmap: Colormap.fetch_cmap(cmap)}
72+
end
73+
74+
defp update_cmap(figure, _, _), do: figure
6175
end

lib/matplotex/figure/cast.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule Matplotex.Figure.Cast do
22
@moduledoc false
33
alias Matplotex.Element.Rect
4-
alias Matplotex.Colorscheme.Colormap
54
alias Matplotex.Element.Cmap
65
alias Matplotex.Figure.Lead
76
alias Matplotex.Element.Legend
@@ -20,6 +19,7 @@ defmodule Matplotex.Figure.Cast do
2019
@ytick_type "figure.y_tick"
2120
@lowest_tick 0
2221
@zero_to_move 0
22+
@cmap_id "colorGradient"
2323

2424
def cast_spines_by_region(
2525
%Figure{
@@ -516,7 +516,7 @@ defmodule Matplotex.Figure.Cast do
516516
}
517517
} = figure
518518
)
519-
when not is_nil(cmap) do
519+
when is_list(cmap) do
520520
cmap_elements =
521521
datasets
522522
|> Enum.with_index()
@@ -575,8 +575,8 @@ defmodule Matplotex.Figure.Cast do
575575

576576
[
577577
%Cmap{
578-
id: "colorGradient",
579-
cmap: Colormap.fetch_cmap(cmap),
578+
id: @cmap_id,
579+
cmap: cmap,
580580
container: %Rect{
581581
x: x_cmap,
582582
y: y_cmap,

test/matplotex/colorscheme/garner_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
defmodule Matplotex.Colorscheme.GarnerTest do
2+
alias Matplotex.Colorscheme.Colormap
23
alias Matplotex.Colorscheme.Garner
34
use Matplotex.PlotCase
45

56
describe "garn_color/3" do
67
test "garn the color for specific point in the number" do
78
colorset = [3,4,5,6,6,4,9]
8-
color = Garner.garn_color(Enum.min_max(colorset), Enum.min(colorset), "viridis")
9+
viridis = Colormap.fetch_cmap(:viridis)
10+
color = Garner.garn_color(Enum.min_max(colorset), Enum.min(colorset), viridis)
911
assert is_color(color)
1012
end
1113
end

test/matplotex/figure/areal/plot_options_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Matplotex.Figure.Areal.PlotOptionsTest do
2+
alias Matplotex.Figure
23
use Matplotex.PlotCase
34

45
setup do
@@ -27,5 +28,13 @@ defmodule Matplotex.Figure.Areal.PlotOptionsTest do
2728
refute figure.axes.element == nil
2829
refute figure.axes == nil
2930
end
31+
test "updates cmap as a list by its name" do
32+
assert %Figure{axes: %{cmap: cmap}} = [1, 2, 3, 4, 5, 6]
33+
|> Matplotex.plot([1, 2, 3, 4, 5],
34+
cmap: "viridis",
35+
colors: [3, 4, 5, 6, 7, 3, 2, 4]
36+
)
37+
assert is_list(cmap)
38+
end
3039
end
3140
end

test/matplotex/figure/areal/scatter_test.exs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ defmodule Matplotex.Figure.Areal.ScatterTest do
1212
assert %Figure{axes: %{data: {x, _y}, element: elements}} = Scatter.materialize(figure)
1313
assert Enum.count(elements, fn elem -> elem.type == "plot.marker" end) == length(x)
1414
end
15-
test "adds point with specific color with color scheme" do
16-
x = [1, 3, 7, 4, 2, 5, 6]
17-
y = [1, 3, 7, 4, 2, 5, 6]
18-
colors = [1, 3, 7, 4, 2, 5, 6]
19-
20-
assert %Figure{axes: %{element: elements}} = x
21-
|> Matplotex.scatter(y, colors: colors)
22-
|> Matplotex.figure(%{figsize: size, margin: margin})
23-
|> Scatter.materialize()
24-
25-
assert elements|>Enum.filter(fn elem -> elem.type == "point.color" end)|>length()
26-
end
2715
end
2816

2917
describe "generate_ticks/2" do

0 commit comments

Comments
 (0)