diff --git a/docs/cheatsheets/cheatsheet1.pdf b/docs/cheatsheets/cheatsheet1.pdf new file mode 100644 index 0000000..cf6b1f2 Binary files /dev/null and b/docs/cheatsheets/cheatsheet1.pdf differ diff --git a/lib/matplotex/blueprint/frame.ex b/lib/matplotex/blueprint/frame.ex index 4e45f60..6c09dc3 100644 --- a/lib/matplotex/blueprint/frame.ex +++ b/lib/matplotex/blueprint/frame.ex @@ -4,6 +4,8 @@ defmodule Matplotex.Blueprint.Frame do @default_margin 0.05 @show_by_default true @valid_by_default true + @default_stroke_grid "#C5C5C5" + @default_stroke_grid_width 1 defmacro frame(opts \\ []) do build_struct(opts) end @@ -70,6 +72,8 @@ defmodule Matplotex.Blueprint.Frame do label: nil, scale: nil, grid: nil, + stroke_grid: @default_stroke_grid, + stroke_grid_width: @default_stroke_grid_width, size: nil, axis: nil, center: nil, diff --git a/lib/matplotex/figure/areal/bar_chart.ex b/lib/matplotex/figure/areal/bar_chart.ex index bc91075..d1d9a64 100644 --- a/lib/matplotex/figure/areal/bar_chart.ex +++ b/lib/matplotex/figure/areal/bar_chart.ex @@ -125,7 +125,10 @@ defmodule Matplotex.Figure.Areal.BarChart do %Dataset{ color: color, width: width, - pos: pos_factor + pos: pos_factor, + edge_color: edge_color, + alpha: alpha, + line_width: line_width } = dataset, bly ) do @@ -139,7 +142,11 @@ defmodule Matplotex.Figure.Areal.BarChart do y: y, width: width, height: bly - y, - color: color + color: color, + stroke: edge_color || color, + fill_opacity: alpha, + stroke_opacity: alpha, + stroke_width: line_width } ], dataset, diff --git a/lib/matplotex/figure/areal/histogram.ex b/lib/matplotex/figure/areal/histogram.ex index b842469..21258a8 100644 --- a/lib/matplotex/figure/areal/histogram.ex +++ b/lib/matplotex/figure/areal/histogram.ex @@ -115,7 +115,7 @@ defmodule Matplotex.Figure.Areal.Histogram do width: region_width / length(bins), height: bly - y, color: color, - stroke: edge_color, + stroke: edge_color || color, fill_opacity: alpha, stroke_opacity: alpha } @@ -151,7 +151,7 @@ defmodule Matplotex.Figure.Areal.Histogram do {bins_dist, hists} end - defp sanitize(%Figure{axes: %__MODULE__{data: {x, y}} = axes} = figure) do + defp sanitize(%Figure{axes: %__MODULE__{data: {x, y}, limit: %TwoD{x: xlim, y: ylim}} = axes} = figure) do {ymin, ymax} = Enum.min_max(y) {xmin, xmax} = Enum.min_max(x) @@ -159,7 +159,7 @@ defmodule Matplotex.Figure.Areal.Histogram do figure | axes: %__MODULE__{ axes - | limit: %TwoD{x: {floor(xmin), ceil(xmax)}, y: {floor(ymin), ceil(ymax)}} + | limit: %TwoD{x: xlim || {floor(xmin), ceil(xmax)}, y: ylim || {floor(ymin), ceil(ymax)}} } } end diff --git a/lib/matplotex/figure/areal/spline.ex b/lib/matplotex/figure/areal/spline.ex index 27c5693..3398a32 100644 --- a/lib/matplotex/figure/areal/spline.ex +++ b/lib/matplotex/figure/areal/spline.ex @@ -88,7 +88,6 @@ defmodule Matplotex.Figure.Areal.Spline do %Figure{figure | axes: %{axes | element: elements}} end - defp capture( %Dataset{ transformed: transformed, @@ -107,8 +106,7 @@ defmodule Matplotex.Figure.Areal.Spline do moveto: moveto, cubic: cubic, smooths: smooths, - fill: color, - stroke: edge_color, + stroke: color || edge_color, stroke_width: stroke_width } end diff --git a/lib/matplotex/figure/cast.ex b/lib/matplotex/figure/cast.ex index 8cdb99f..f4d8bf0 100644 --- a/lib/matplotex/figure/cast.ex +++ b/lib/matplotex/figure/cast.ex @@ -10,69 +10,14 @@ defmodule Matplotex.Figure.Cast do alias Matplotex.Figure.RcParams alias Matplotex.Element.Label alias Matplotex.Element.Line - alias Matplotex.Figure.Coords + # alias Matplotex.Figure.Coords alias Matplotex.Figure alias Matplotex.Figure.Dataset @xtick_type "figure.x_tick" @ytick_type "figure.y_tick" - @stroke_grid "#ddd" - @stroke_width_grid 1 @lowest_tick 0 @zero_to_move 0 - def cast_spines( - %Figure{ - axes: - %{ - coords: %Coords{ - bottom_left: {blx, bly}, - bottom_right: {brx, bry}, - top_left: {tlx, tly}, - top_right: {trx, yrt} - }, - element: elements - } = axes - } = figure - ) do - # Four Line struct representing each corners - left = %Line{ - x1: blx, - y1: bly, - x2: tlx, - y2: tly, - type: "spine.left" - } - - right = %Line{ - x1: brx, - y1: bry, - x2: trx, - y2: yrt, - type: "spine.right" - } - - top = %Line{ - x1: tlx, - y1: tly, - x2: trx, - y2: yrt, - type: "spine.top" - } - - bottom = %Line{ - x1: blx, - y1: bly, - x2: brx, - y2: bry, - type: "spine.bottom" - } - - %Figure{figure | axes: %{axes | element: elements ++ [left, right, top, bottom]}} - end - - def cast_spines(_figure) do - raise ArgumentError, message: "Figure does't contain enough data to proceed" - end def cast_spines_by_region( %Figure{ @@ -158,37 +103,6 @@ defmodule Matplotex.Figure.Cast do %Figure{figure | axes: %{axes | element: element ++ [left, right, top, bottom]}} end - def cast_title( - %Figure{ - axes: - %{ - region_title: region_title, - title: title, - element: elements - } = axes, - rc_params: %RcParams{title_font: title_font} - } = figure - ) do - {ttx, tty} = calculate_center(region_title, :x) - - title = - %Label{ - type: "figure.title", - x: ttx, - y: tty, - text: title - } - |> merge_structs(title_font) - - %Figure{figure | axes: %{axes | element: elements ++ [title]}} - end - - def cast_title(%Figure{axes: %{show_title: false}} = figure), do: figure - - def cast_title(_figure) do - raise ArgumentError, message: "Invalid figure to cast title" - end - def cast_title_by_region( %Figure{ axes: @@ -214,13 +128,6 @@ defmodule Matplotex.Figure.Cast do %Figure{figure | axes: %{axes | element: elements ++ [title]}} end - def cast_label(%Figure{axes: %{label: nil}} = figure), do: figure - - def cast_label(figure) do - figure - |> cast_xlabel() - |> cast_ylabel() - end def cast_label_by_region(figure) do figure @@ -228,30 +135,6 @@ defmodule Matplotex.Figure.Cast do |> cast_ylabel_by_region() end - def cast_xlabel( - %Figure{ - axes: %{label: %{x: x_label}, coords: coords, element: element} = axes, - rc_params: %RcParams{x_label_font: label_font} - } = figure - ) - when not is_nil(x_label) do - xlabel_coords = Map.get(coords, :x_label) - {x, y} = calculate_center(coords, xlabel_coords, :x) - - x_label = - %Label{ - type: "figure.x_label", - x: x, - y: y, - text: x_label - } - |> merge_structs(label_font) - - element = element ++ [x_label] - %Figure{figure | axes: %{axes | element: element}} - end - - def cast_xlabel(figure), do: figure def cast_xlabel_by_region( %Figure{ @@ -276,32 +159,7 @@ defmodule Matplotex.Figure.Cast do %Figure{figure | axes: %{axes | element: element}} end - def cast_ylabel( - %Figure{ - axes: %{label: %{y: y_label}, coords: coords, element: element} = axes, - rc_params: %RcParams{y_label_font: label_font} - } = figure - ) - when not is_nil(y_label) do - ylabel_coords = Map.get(coords, :y_label) - - {x, y} = calculate_center(coords, ylabel_coords, :y) - - y_label = - %Label{ - type: "figure.y_label", - x: x, - y: y, - text: y_label, - rotate: rotate_label(:y) - } - |> merge_structs(label_font) - - element = element ++ [y_label] - %Figure{figure | axes: %{axes | element: element}} - end - - def cast_ylabel(figure), do: figure + def cast_xlabel_by_region(figure), do: figure def cast_ylabel_by_region( %Figure{ @@ -325,7 +183,7 @@ defmodule Matplotex.Figure.Cast do element = element ++ [y_label] %Figure{figure | axes: %{axes | element: element}} end - + def cast_ylabel_by_region(figure), do: figure def cast_xticks_by_region( %Figure{ axes: @@ -453,6 +311,7 @@ defmodule Matplotex.Figure.Cast do } } = figure ) do + y_ticks = confine_ticks(y_ticks, ylim) y_data = confine_data(y_data, ylim) dataset = confine_data(dataset, ylim, :y) @@ -518,42 +377,13 @@ defmodule Matplotex.Figure.Cast do def cast_yticks_by_region(figure), do: figure - def cast_hgrids(%Figure{axes: %{coords: %{hgrids: nil}}} = figure), do: figure - def cast_hgrids(%Figure{axes: %{show_h_grid: false}} = figure), do: figure - - def cast_hgrids( - %Figure{ - axes: - %{ - show_h_grid: true, - coords: %Coords{hgrids: hgrids, top_right: {rightx, _topy}}, - element: elements - } = axes - } = figure - ) do - hgrid_elements = - Enum.map(hgrids, fn {x, y} -> - %Line{ - x1: x, - x2: rightx, - y1: y, - y2: y, - type: "figure.h_grid", - stroke: @stroke_grid, - stroke_width: @stroke_width_grid - } - end) - - elements = elements ++ hgrid_elements - - %Figure{figure | axes: %{axes | element: elements}} - end - def cast_hgrids_by_region( %Figure{ axes: %{ show_h_grid: true, + stroke_grid: stroke_grid, + stroke_grid_width: stroke_grid_width, region_y: %Region{coords: %XyCoords{grids: hgrids}}, region_x: %Region{width: width_region_x}, element: elements @@ -568,8 +398,8 @@ defmodule Matplotex.Figure.Cast do y1: y, y2: y, type: "figure.h_grid", - stroke: @stroke_grid, - stroke_width: @stroke_width_grid + stroke: stroke_grid, + stroke_width: stroke_grid_width } end) @@ -580,42 +410,13 @@ defmodule Matplotex.Figure.Cast do def cast_hgrids_by_region(figure), do: figure - def cast_vgrids(%Figure{axes: %{coords: %{vgrids: nil}}} = figure), do: figure - def cast_vgrids(%Figure{axes: %{show_v_grid: false}} = figure), do: figure - - def cast_vgrids( - %Figure{ - axes: - %{ - show_v_grid: true, - coords: %Coords{vgrids: vgrids, top_right: {_rightx, topy}}, - element: elements - } = axes - } = figure - ) do - vgrid_elements = - Enum.map(vgrids, fn {x, y} -> - %Line{ - x1: x, - x2: x, - y1: y, - y2: topy, - type: "figure.v_grid", - stroke: @stroke_grid, - stroke_width: @stroke_width_grid - } - end) - - elements = elements ++ vgrid_elements - - %Figure{figure | axes: %{axes | element: elements}} - end - def cast_vgrids_by_region( %Figure{ axes: %{ show_v_grid: true, + stroke_grid: stroke_grid, + stroke_grid_width: stroke_grid_width, region_x: %Region{coords: %XyCoords{grids: vgrids}}, region_y: %Region{height: height_region_y}, element: elements @@ -630,8 +431,8 @@ defmodule Matplotex.Figure.Cast do y1: y, y2: y - height_region_y, type: "figure.v_grid", - stroke: @stroke_grid, - stroke_width: @stroke_width_grid + stroke: stroke_grid, + stroke_width: stroke_grid_width } end) @@ -692,14 +493,6 @@ defmodule Matplotex.Figure.Cast do def cast_legends(figure), do: figure - defp calculate_center(%Coords{bottom_left: bottom_left, bottom_right: bottom_right}, {x, y}, :x) do - {calculate_distance(bottom_left, bottom_right) / 2 + x, y} - end - - defp calculate_center(%Coords{bottom_left: bottom_left, top_left: top_left}, {x, y}, :y) do - {x, calculate_distance(bottom_left, top_left) / 2 + y} - end - defp calculate_center(%Region{x: x, y: y, width: width}, :x) do {calculate_distance({x, y}, {x + width, y}) / 2 + x, y} end @@ -708,12 +501,6 @@ defmodule Matplotex.Figure.Cast do {x, calculate_distance({x, y}, {x, y + height}) / 2 + y} end - defp merge_structs(%module{} = st, sst) do - sst = Map.from_struct(sst) - params = st |> Map.from_struct() |> Map.merge(sst) - struct(module, params) - end - defp calculate_distance({x1, y1}, {x2, y2}) do :math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) end @@ -751,19 +538,4 @@ defmodule Matplotex.Figure.Cast do tick >= min && tick <= max end) end - - # defp append_lim([first | [second | _]] = ticks, {min, max}) do - # with_min = - # if Enum.min(ticks) > min + (second - first) do - # [min] ++ ticks - # else - # ticks - # end - - # if Enum.max(with_min) < max - (second - first) do - # with_min ++ [max] - # else - # with_min - # end - # end end diff --git a/lib/matplotex/figure/dataset.ex b/lib/matplotex/figure/dataset.ex index 99a5908..d47febd 100644 --- a/lib/matplotex/figure/dataset.ex +++ b/lib/matplotex/figure/dataset.ex @@ -5,19 +5,18 @@ defmodule Matplotex.Figure.Dataset do @default_linestyle "_" @default_width 0.2 @default_marker_size 3.5 - @default_stroke "black" @default_alpha 1.0 @line_width 2 defstruct [ :label, :pos, + :edge_color, width: @default_width, x: [], y: [], transformed: [], color: @default_color, - edge_color: @default_stroke, alpha: @default_alpha, marker: @default_marker, linestyle: @default_linestyle, diff --git a/lib/matplotex/figure/lead.ex b/lib/matplotex/figure/lead.ex index 181b666..11b5f06 100644 --- a/lib/matplotex/figure/lead.ex +++ b/lib/matplotex/figure/lead.ex @@ -66,15 +66,16 @@ defmodule Matplotex.Figure.Lead do } = axes } = figure ) do + {x_ticks, x_lim} = maybe_generate_ticks(x_ticks, x_lim, x_data, x_ticks_count || width) - {y_ticks, y_lim} = maybe_generate_ticks(y_ticks, y_lim, y_data, y_ticks_count || height) + {y_ticks, y_limt} = maybe_generate_ticks(y_ticks, y_lim, y_data, y_ticks_count || height) %Figure{ figure | axes: %{ axes | tick: %TwoD{ticks | x: x_ticks, y: y_ticks}, - limit: %TwoD{limit | x: x_lim, y: y_lim} + limit: %TwoD{limit | x: x_lim, y: y_lim || y_limt} } } end diff --git a/lib/matplotex/helpers.ex b/lib/matplotex/helpers.ex deleted file mode 100644 index 850191e..0000000 --- a/lib/matplotex/helpers.ex +++ /dev/null @@ -1,437 +0,0 @@ -defmodule Matplotex.Helpers do -@moduledoc false - def copy(term) do - text = - if is_binary(term) do - term - else - inspect(term, limit: :infinity, pretty: true) - end - - port = - if :os.type() == {:unix, :linux}, - do: Port.open({:spawn, "xclip -selection clipboard"}, [:binary]), - else: Port.open({:spawn, "pbcopy"}, []) - - true = Port.command(port, text) - true = Port.close(port) - - :ok - end - - def line_plot() do - x = [1, 2, 3, 4, 6, 6, 7] - y = [1, 3, 4, 4, 5, 6, 7] - - frame_width = 6 - frame_height = 6 - size = {frame_width, frame_height} - margin = 0.05 - font_size = "16pt" - title_font_size = "18pt" - ticks = [1, 2, 3, 4, 5, 6, 7] - - x - |> Matplotex.plot(y) - |> Matplotex.figure(%{figsize: size, margin: margin}) - |> Matplotex.set_title("The Plot Title") - |> Matplotex.set_xlabel("X Axis") - |> Matplotex.set_ylabel("Y Axis") - |> Matplotex.set_xticks(ticks) - |> Matplotex.set_yticks(ticks) - |> Matplotex.set_xlim({4, 7}) - |> Matplotex.set_ylim({4, 7}) - # TODO: Setting limits are not taking the proper xy values - |> Matplotex.set_rc_params( - x_tick_font_size: font_size, - y_tick_font_size: font_size, - title_font_size: title_font_size, - x_label_font_size: font_size, - y_label_font_size: font_size, - title_font_size: title_font_size - ) - |> Matplotex.show() - |> copy() - end - - def line_plot_by_options() do - x = [1, 2, 3, 4, 6, 6, 7] - y = [1, 3, 4, 4, 5, 6, 7] - - frame_width = 6 - frame_height = 6 - size = {frame_width, frame_height} - margin = 0.05 - font_size = "16pt" - title_font_size = "18pt" - ticks = [0, 1, 2, 3, 4, 5, 6, 7] - - x - |> Matplotex.plot(y, - figsize: size, - margin: margin, - title: "The plot title", - x_label: "X Axis", - y_label: "Y Axis", - x_tick: ticks, - y_tick: ticks, - x_limit: {0, 7}, - y_limit: {0, 7}, - x_tick_font_size: font_size, - y_tick_font_size: font_size, - title_font_size: title_font_size, - x_label_font_size: font_size, - y_label_font_size: font_size, - y_tick_font_text_anchor: "start" - ) - |> Matplotex.show() - |> copy() - end - - def line_plotc() do - x = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] - y = [1, 3, 4, 4, 5, 6, 7] - - frame_width = 8 - frame_height = 6 - size = {frame_width, frame_height} - margin = 0.05 - font_size = "16pt" - title_font_size = "18pt" - ticks = [1, 2, 3, 4, 5, 6, 7] - xticks = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] - - x - |> Matplotex.plot(y) - |> Matplotex.figure(%{figsize: size, margin: margin}) - |> Matplotex.set_title("Sample plot") - |> Matplotex.set_xlabel("X Axis") - |> Matplotex.set_ylabel("Y Axis") - |> Matplotex.set_xticks(xticks) - |> Matplotex.set_yticks(ticks) - |> Matplotex.set_xlim({1, 7}) - |> Matplotex.set_ylim({1, 7}) - |> Matplotex.set_rc_params( - x_tick_font_size: font_size, - y_tick_font_size: font_size, - title_font_size: title_font_size, - x_label_font_size: font_size, - y_label_font_size: font_size, - title_font_size: title_font_size - ) - |> Matplotex.show() - |> copy() - end - - # def bar() do - # values = [2, 1, 3, 7, 3, 5, 4] - # categories = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] - - # frame_width = 8 - # frame_height = 6 - # size = {frame_width, frame_height} - # margin = 0.05 - # font_size = 0 - # title_font_size = 0 - # yticks = [0, 1, 2, 3, 4, 5, 6, 7] - - # categories - # |> Matplotex.bar(values) - # |> Matplotex.figure(%{figsize: size, margin: margin}) - # |> Matplotex.set_title("Sample barchart") - # |> Matplotex.set_xticks(categories) - # |> Matplotex.set_yticks(yticks) - # |> Matplotex.set_xlabel("Category") - # |> Matplotex.set_ylabel("Values") - # |> Matplotex.set_xlim({1, 7}) - # |> Matplotex.set_ylim({0, 7}) - # |> Matplotex.hide_v_grid() - # |> Matplotex.set_rc_params( - # x_tick_font_size: font_size, - # y_tick_font_size: font_size, - # title_font_size: title_font_size, - # x_label_font_size: font_size, - # y_label_font_size: font_size, - # title_font_size: title_font_size, - # y_padding: 0 - # ) - # |> Matplotex.show() - # |> copy() - # end - - def scatter() do - # x = [10, 20, 3, 4, 6, 6, 7] - # y = [1, 3, 4, 4, 5, 6, 7] - x = [0, 10, 20] - y = [0, 10, 20] - frame_width = 10 - frame_height = 10 - size = {frame_width, frame_height} - margin = 0.05 - font_size = "16pt" - title_font_size = "18pt" - ticks = [0, 10, 20] - - x - |> Matplotex.scatter(y) - |> Matplotex.figure(%{figsize: size, margin: margin}) - |> Matplotex.set_title("Sample Scatter plot") - |> Matplotex.set_xlabel("X Axis") - |> Matplotex.set_ylabel("Y Axis") - |> Matplotex.set_xticks(ticks) - |> Matplotex.set_yticks(ticks) - |> Matplotex.set_xlim({0, 20}) - |> Matplotex.set_ylim({0, 20}) - # TODO: Setting limits are not taking the proper xy values - |> Matplotex.set_rc_params( - x_tick_font_size: font_size, - y_tick_font_size: font_size, - title_font_size: title_font_size, - x_label_font_size: font_size, - y_label_font_size: font_size, - title_font_size: title_font_size - ) - |> Matplotex.show() - |> copy() - end - - def multiline_plot() do - x = [1, 2, 3, 4, 5] - # Dataset 1 - y1 = [1, 4, 9, 16, 25] - # Dataset 2 - y2 = [1, 3, 6, 10, 15] - # Dataset 3 - y3 = [2, 5, 7, 12, 17] - - x - |> Matplotex.plot(y1, color: "blue", linestyle: "_", marker: "o", label: "Dataset 1") - |> Matplotex.plot(x, y2, color: "red", linestyle: "--", marker: "^", label: "Dataset 2") - |> Matplotex.plot(x, y3, color: "green", linestyle: "-.", marker: "s", label: "Dataset 3") - |> Matplotex.set_title("Title") - |> Matplotex.set_xticks([1, 2, 3, 4, 5]) - |> Matplotex.set_xlabel("X-Axis") - |> Matplotex.set_ylabel("Y-Axis") - |> Matplotex.show() - |> copy() - end - - def minscatter() do - x= Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list() - y1 = Nx.Random.key(13) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list() - y2 = Nx.Random.key(14) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list() - x - |>Matplotex.scatter(y1, color: "#EE3377", x_label: "", y_label: "", label: "Amet") - |>Matplotex.scatter(x,y2, color: "#0077BB", x_label: "", y_label: "", label: "Donor") - |>Matplotex.show() - |>copy() - - - end - - def multi_bar() do - categories = ["apple", "banana", "fig", "avocado"] - values1 = [22, 33, 28, 34] - values2 = [53, 63, 59, 60] - width = 0.22 - - Matplotex.bar(width, values1, width, label: "Dataset1", color: "#255199") - |> Matplotex.bar(-width, values2, width, label: "Dataset2", color: "orange") - |> Matplotex.set_xticks(categories) - |> Matplotex.figure(%{figsize: {10, 10}, margin: 0.05}) - |> Matplotex.set_title("Bar chart") - |> Matplotex.set_xlabel("X-axis") - |> Matplotex.set_ylabel("Y-Axis") - |> Matplotex.hide_v_grid() - |> Matplotex.set_ylim({0, 70}) - |> Matplotex.show() - |> copy() - end - - def multiline_scatter() do - x = [1, 2, 3, 4, 5] - # Dataset 1 - y1 = [20, 5, 12, 16, 25] - # Dataset 2 - y2 = [10, 1, 6, 10, 15] - # Dataset 3 - y3 = [17, 5, 8, 12, 17] - - x - |> Matplotex.scatter(y1, color: "blue", linestyle: "_", marker: "o", label: "Dataset 1") - |> Matplotex.scatter(x, y2, color: "red", linestyle: "--", marker: "^", label: "Dataset 2") - |> Matplotex.scatter(x, y3, color: "green", linestyle: "-.", marker: "s", label: "Dataset 3") - |> Matplotex.figure(%{figsize: {8, 8}, margin: 0.05}) - |> Matplotex.set_title("Title") - |> Matplotex.set_xlabel("X-Axis") - |> Matplotex.set_ylabel("Y-Axis") - |> Matplotex.show() - |> copy() - end - - def pie() do - # Percentages for each slice - sizes = [25, 35, 20, 20, 10] - # Labels for each slice - labels = ["A", "B", "C", "D", "E"] - # Colors for the slices - colors = ["#33BBEE", "#EE7733", "#EE3377", "#0077BB","#919D41"] - - sizes - |> Matplotex.pie(colors: colors, labels: labels) - |> Matplotex.set_title("Pie chart") - |> Matplotex.figure(%{figsize: {4, 3}, margin: 0.05}) - |> Matplotex.set_rc_params(%{line_width: 1, legend_width: 0.25}) - |> Matplotex.show() - |> copy() - end - - def photon_scatter() do - {density, energy} = - "samples/photon.csv" - |> Path.expand() - |> File.stream!() - |> CSV.decode!() - |> Stream.drop(1) - |> Stream.map(fn [density, energy, _continent, _emission, _, _] -> - {String.to_float(density), String.to_float(energy)} - end) - |> Enum.unzip() - - Matplotex.scatter(density, energy, color: "#FBD1A2", marker: "o", label: "Photo energy") - |> Matplotex.figure(%{figsize: {10, 4}, margin: 0.05}) - |> Matplotex.set_title("Photon density and photon energy") - |> Matplotex.set_xlabel("Photon density") - |> Matplotex.set_ylabel("Photon energy") - |> Matplotex.show() - |> copy() - end - - def emission_pie() do - categories = ["2008", "2009", "2010", "2011"] - values = [18.48923375, 17.1923791, 17.48479218, 17.02021634] - - colors = ["#76b5c5", "#DEDEDE", "#FBD1A2", "#6195B4"] - - values - |> Matplotex.pie(colors: colors, labels: categories) - |> Matplotex.set_title("Asias Emission distribution(2008-2011)") - |> Matplotex.set_rc_params(%{line_width: 1, legend_width: 0.5}) - |> Matplotex.figure(%{figsize: {10, 4}, margin: 0.15}) - |> Matplotex.show() - |> copy() - end - - def rdpie() do - # Percentages for each slice - sizes = [25, 35, 20, 20, 10] - # Labels for each slice - labels = ["Lorem", "Ipsum", "Donor", "Bit", "Amet"] - # Colors for the slices - colors = ["#33BBEE", "#EE7733", "#EE3377", "#EE3377","#008080"] - - sizes - |> Matplotex.pie(colors: colors, labels: labels) - |> Matplotex.set_rc_params(%{line_width: 1, legend_width: 0.25}) - |> Matplotex.show() - |>copy() - end - - def hist() do - values = Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list() - bins = 100 - - Matplotex.hist(values, bins, - x_label: "Value", - y_label: "Frequency", - title: "Histogram", - color: "blue", - edge_color: "black", - alpha: 0.7, - x_ticks_count: 9 - ) - |> Matplotex.show() - |> 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 rdspline() do - x = Nx.linspace(0, 10, n: 100)|> Nx.to_list() - y1 = Nx.Random.key(12) |> Nx.Random.normal(0, 10, shape: {100}) |> elem(0) |> Nx.to_list() - y2 = Nx.Random.key(13) |> Nx.Random.normal(0, 10, shape: {100}) |> elem(0) |> Nx.to_list() - - Matplotex.spline(x, y1, x_label: "", y_label: "", edge_color: "#919D41", line_width: 3) - |>Matplotex.spline(x,y2, x_label: "", y_label: "", edge_color: "#008080", line_width: 3) - |> Matplotex.set_ylim({-30, 30}) - |> Matplotex.show() - |> copy() - end - def minhist() do - values = Nx.Random.key(12) |> Nx.Random.normal(0, 1, shape: {1000}) |> elem(0) |> Nx.to_list() - bins = 100 - - Matplotex.hist(values, bins, - x_label: "", - y_label: "", - color: "#EE7733", - edge_color: "#EE7733" - ) - |> Matplotex.set_ylim({0, 50}) - |> Matplotex.show() - |> copy() - end - - def line_n() do - x = Nx.linspace(0, 10, n: 100)|> Nx.to_list() - y1 = Nx.Random.key(12) |> Nx.Random.normal(0, 10, shape: {100}) |> elem(0) |> Nx.to_list() - y2 = Nx.Random.key(13) |> Nx.Random.normal(0, 10, shape: {100}) |> elem(0) |> Nx.to_list() - - x - |> Matplotex.plot(y1, color: "#EE3377", x_label: "", y_label: "", label: "Tarus") - |> Matplotex.plot(x,y2, color: "#0077BB", x_label: "", y_label: "", label: "Ipsum") - |> 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