8 days ago
Advanced Unicode plotting library designed for use in Julia's REPL.
To install UnicodePlots, start up Julia and type the following code-snipped into the REPL. It makes use of the native Julia package manger.
Pkg.add("UnicodePlots") using UnicodePlots
For the latest developer version:
There are a couple of ways to generate typical plots without much verbosity. Here is a list of the main high-level functions for common scenarios:
Here is a quick hello world example of a typical use-case:
myPlot = lineplot([-1, 2, 3, 7], [1, 2, 9, 4], title = "My Plot", name = "my line")
There are other types of
Canvas available (see section "Low-level Interface").
In some situations, such as printing to a file, using
BlockCanvas might lead to better results.
lineplot([-1, 2, 3, 7], [1, 2, 9, 4], title = "My Plot", name = "my line", canvas = AsciiCanvas, border = :ascii)
Every plot has a mutating variant that ends with a exclamation mark.
lineplot!(myPlot, [0, 4, 8], [10, 1, 10], color = :yellow, name = "other line")
scatterplot(randn(50), randn(50), title = "My Scatterplot", color = :red)
lineplot([1, 2, 7], [9, -6, 8], title = "My Lineplot", color = :blue)
It's also possible to specify a function and a range.
myPlot = lineplot([cos, sin], -π/2, 2π)
You can also plot lines by specifying an intercept and slope
lineplot!(myPlot, -0.5, .2)
Accepts either two vectors or a dictionary
barplot(["Paris", "New York", "Moskau", "Madrid"], [2.244, 8.406, 11.92, 3.165], title = "Population")
Note: You can use the keyword argument
symb to specify the character that should
be used to plot the bars. For example
symb = "#"
# supported style are :pre and :post stairs([1, 2, 4, 7, 8], [1, 3, 4, 2, 7], color = :red, style = :post, title = "My Staircase Plot")
histogram(randn(1000), bins = 15, title = "Histogram")
spy(sprandn(50, 120, .05))
myPlot = densityplot(randn(1000), randn(1000), color = :blue) densityplot!(myPlot, randn(1000) + 2, randn(1000) + 2, color = :red)
All plots support a common set of named parameters
title::String = "":
Text to display on the top of the plot.
name::String = "":
Annotation of the current drawing to displayed on the right
width::Int = 40:
Number of characters per row that should be used for plotting.
lineplot(sin, 1:.5:20, width = 80)
height::Int = 20:
Number of rows that should be used for plotting. Not applicable to
lineplot(sin, 1:.5:20, height = 18)
xlim::Vector = [0, 1]:
Plotting range for the x coordinate
ylim::Vector = [0, 1]:
Plotting range for the y coordinate
margin::Int = 3:
Number of empty characters to the left of the whole plot.
border::Symbol = :solid:
The style of the bounding box of the plot. Supports
lineplot([-1.,2, 3, 7], [1.,2, 9, 4], border=:bold) lineplot([-1.,2, 3, 7], [1.,2, 9, 4], border=:dashed) lineplot([-1.,2, 3, 7], [1.,2, 9, 4], border=:dotted) lineplot([-1.,2, 3, 7], [1.,2, 9, 4], border=:none)
padding::Int = 1:
Space of the left and right of the plot between the labels and the canvas.
labels::Bool = true:
Can be used to hide the labels by setting
lineplot(sin, 1:.5:20, labels=false)
grid::Bool = true:
Can be used to hide the gridlines at the origin
color::Symbol = :blue:
Color of the drawing. Can be any of
canvas::Type = BrailleCanvas:
The type of canvas that should be used for drawing (see section "Low-level Interface")
symb::AbstractString = "▪":
Barplot only. Specifies the character that should be used to render the bars
Note: If you want to print the plot into a file but have monospace issues with your font, you should probably try
border = :ascii and
canvas = AsciiCanvas (or
canvas = DotCanvas for scatterplots).
titlethe string to write in the top center of the plot window. If the title is empty the whole line of the title will not be drawn
xlabelthe string to display on the bottom of the plot window. If the title is empty the whole line of the label will not be drawn
ylabelthe string to display on the far left of the plot window.
annotate! is responsible for the setting all the textual decorations of a plot. It has two functions:
annotate!(plot::Plot, where::Symbol, value::String)
wherecan be any of:
annotate!(plot::Plot, where::Symbol, row::Int, value::String)
wherecan be any of:
rowcan be between 1 and the number of character rows of the canvas
The primary structures that do all the heavy lifting behind the curtain are subtypes of
Canvas. A canvas is a graphics object for rasterized plotting. Basically it uses Unicode characters to represent pixel.
Here is a simple example:
canvas = BrailleCanvas(40, 10, # number of columns and rows (characters) origin_x = 0., origin_y = 0., # position in virtual space width = 1., height = 1.) # size of the virtual space lines!(canvas, 0., 0., 1., 1., :blue) # virtual space points!(canvas, rand(50), rand(50), :red) # virtual space lines!(canvas, 0., 1., .5, 0., :yellow) # virtual space pixel!(canvas, 5, 8, :red) # pixel space
You can access the height and width of the canvas (in characters) with
ncols(canvas) respectively. You can use those functions in combination with
printrow to embed the canvas anywhere you wish. For example,
printrow(STDOUT, canvas, 3) writes the third character row of the canvas to the standard output.
As you can see, one issue that arises when multiple pixel are represented by one character is that it is hard to assign color. That is because each of the "pixel" of a character could belong to a different color group (each character can only have a single color). This package deals with this using a color-blend for the whole group.
At the moment there are the following types of Canvas implemented:
BrailleCanvas: This type of canvas is probably the one with the highest resolution for Unicode plotting. It essentially uses the Unicode characters of the Braille symbols as pixel. This effectively turns every character into 8 pixels that can individually be manipulated using binary operations.
BlockCanvas: This canvas is also Unicode-based. It has half the resolution of the BrailleCanvas. In contrast to BrailleCanvas, the pixels don't have visible spacing between them. This canvas effectively turns every character into 4 pixels that can individually be manipulated using binary operations.
AsciiCanvas and DotCanvas: These two canvas utilizes only standard ASCII character for drawing. Naturally, it doesn't look quite as nice as the Unicode-based ones. However, in some situations it might yield better results. Printing plots to a file is one of those situations.
DensityCanvas: Unlike the BrailleCanvas, the density canvas does not simply mark a "pixel" as set. Instead it increments a counter per character that keeps track of the frequency of pixels drawn in that character. Together with a variable that keeps track of the maximum frequency, the canvas can thus draw the density of datapoints.
This graphics area is special in that it does not support any pixel manipulation. It is essentially the barplot without decorations but the numbers. It does only support one method
addrow! which allows the user to add additional bars to the graphics object
This code is free to use under the terms of the MIT license.