Skip to content

Instantly share code, notes, and snippets.

@jonatanklosko
Created December 7, 2021 15:36
Show Gist options
  • Save jonatanklosko/449485d2308a249c87c1f84c78a1a29c to your computer and use it in GitHub Desktop.
Save jonatanklosko/449485d2308a249c87c1f84c78a1a29c to your computer and use it in GitHub Desktop.

Fish visualization

Setup

Mix.install([
  {:kino, "~> 0.4.1"},
  {:vega_lite, "~> 0.1.1"}
])
alias VegaLite, as: Vl

Population growth over time

graph =
  Vl.new(height: 300, width: 300)
  |> Vl.mark(:line)
  |> Vl.encode_field(:x, "day", type: :quantitative)
  |> Vl.encode_field(:y, "count", type: :quantitative)
  # |> Vl.encode_field(:y, "count", type: :quantitative, scale: [type: :log])
  |> Kino.VegaLite.new()
  |> Kino.render()

Kino.VegaLite.periodically(
  graph,
  100,
  {0, {0, 3, 2, 0, 0, 1, 0, 1, 0}},
  fn {day, {t0, t1, t2, t3, t4, t5, t6, t7, t8} = t} ->
    count = Tuple.sum(t)
    Kino.VegaLite.push(graph, %{day: day, count: count})

    if day < 200 do
      t = {t1, t2, t3, t4, t5, t6, t7 + t0, t8, t0}
      {:cont, {day + 1, t}}
    else
      :halt
    end
  end
)

Timers histogram over time

Change the initial value and see how the histogram stabilizes over time.

graph =
  Vl.new(height: 300, width: 300)
  |> Vl.mark(:bar)
  |> Vl.encode_field(:x, "timer", type: :nominal)
  |> Vl.encode_field(:y, "count", type: :quantitative)
  |> Kino.VegaLite.new()
  |> Kino.render()

Kino.VegaLite.periodically(
  graph,
  100,
  {0, {0, 3, 2, 0, 0, 1, 0, 1, 0}},
  fn {day, {t0, t1, t2, t3, t4, t5, t6, t7, t8} = t} ->
    histogram_points =
      t
      |> Tuple.to_list()
      |> Enum.with_index()
      |> Enum.map(fn {count, timer} -> %{count: count, timer: timer} end)

    Kino.VegaLite.push_many(graph, histogram_points, window: length(histogram_points))

    if day < 200 do
      t = {t1, t2, t3, t4, t5, t6, t7 + t0, t8, t0}
      {:cont, {day + 1, t}}
    else
      :halt
    end
  end
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment