2

Julia has the delightful ability to generate plots constructed from Unicode symbols which are printed directly to the command line in a very straightforward way. For example, the following code generates a Unicode plot of a sine function directly to the command line:

using Plots
unicodeplots();
x = [0:0.1:2*pi;];
y = sin.(x);
plot(x,y)

I would like to try to find a way to create an animated plot of this form directly on the command line. Ideally, I would like to generate a single plot in Unicode that is ``updated" in such a way that it appears animated.

However, although printing hundreds of distinct frames to the command line is naturally less appealing, such a solution is acceptable if it ``looks" like an animation. Another less acceptable solution is to print such Unicode plots into a gif in a way that is consistent for all platforms; attempts to do any of this involving jury-rigging @animate and @gif have largely failed, since either function cannot even print Unicode plots to a file in the Windows form of Julia.

UPDATE: Here is an example of code that generates an "animation" in the command line that is not really acceptable, which simply plots each distinct frame followed by "spacing" in the command line provided by a special Unicode character (tip provided by niczky12):

using Plots
unicodeplots();
n = 100;
x = [0:0.1:4*pi;];

for i = 1:30
    y = sin.(x .+ (i/2));
    plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
    sleep(0.01)
    println("\33[2J")
end
  • 1
    `println("\33[2J")` seems to delete all printed elements from the console, so you might be able to plot, delete, plot, delete... I made a command-line game in Julia using this trick: https://blog.devgenius.io/make-a-command-line-game-with-julia-a408057adcfe – niczky12 Feb 01 '22 at 16:50
  • Clever trick! Unfortunately, what that command does is simply "push" the command line sufficiently down that it removes the previously printed elements from view; so when making an animation, you visibly see the graph "slide" upwards, and the position isn't stable enough for fast frame rates (say FPS 60) to make it a properly viewable animation. – aghostinthefigures Feb 01 '22 at 17:06

1 Answers1

1

A slight improvement might be this:

let
   print("\33[2J")
   for i in 1:30
      println("\33[H")
      y = sin.(x .+ (i/2));
      plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
      sleep(0.01)
   end
end

animated unicode plots

daycaster
  • 2,655
  • 2
  • 15
  • 18