UnicodePlots.jl
repository·main·Indexed 23 days ago
https://github.com/juliaplots/unicodeplots.jlA Julia library for high-quality plotting directly within the terminal (REPL) using Unicode characters. It provides a variety of plot types including line, scatter, bar, histogram, box, density, contour, polar, heatmap, surface, and isosurface plots. The library features a low-level Canvas API with multiple implementations such as BrailleCanvas, OctantCanvas, BlockCanvas, and AsciiCanvas to support different resolutions and terminal capabilities.
What's inside UnicodePlots.jl
- UnicodePlots is an advanced Unicode plotting library specifically designed for use within the Julia REPL. It allows users to render plots directly in the terminal using Unicode characters.
How mutating plot methods work
mainMany plot functions have a mutating variant ending in
!(e.g.,lineplot!). These allow you to add data to an existingPlotobject.Important: Mutating methods cannot update the axis limits because they draw onto a fixed canvas. You must set the limits beforehand when creating the initial
Plotobject or using the non-mutating constructor.Understand Canvas types
mainThe core of UnicodePlots is the
Canvasabstraction. Different canvas types provide different resolutions and rendering styles:- BrailleCanvas: Highest resolution; uses Unicode Braille symbols (8 pixels per character).
- BlockCanvas: Medium resolution; uses Unicode blocks (4 pixels per character).
- HeatmapCanvas: Lower resolution; uses foreground/background colors (2 pixels per character).
- AsciiCanvas / DotCanvas: Uses standard ASCII characters; best for file output or environments with font issues.
- DensityCanvas: Tracks pixel frequency per character to visualize data density.
- BarplotGraphics: Specialized for barplots; does not support pixel manipulation, only
addrow!for adding bars.
import UnicodePlots: lines!, points!, pixel! canvas = BrailleCanvas(15, 40, # number of rows and columns (characters) origin_y=0., origin_x=0., # position in virtual space height=1., width=1.) # size of the virtual space lines!(canvas, 0., 0., 1., 1.; color=:cyan) # virtual space points!(canvas, rand(50), rand(50); color=:red) # virtual space lines!(canvas, 0., 1., .5, 0.; color=:yellow) # virtual space pixel!(canvas, 5, 8; color=:red) # pixel space Plot(canvas)Choose the appropriate Canvas type for your plot
mainDepending on your resolution and terminal support requirements, you can choose from several
Canvasimplementations:Canvas Type Description BrailleCanvasHigh resolution. Uses Braille symbols where each character represents 8 pixels via binary operations. OctantCanvasHigh resolution. Uses Unicode octant symbols (8 pixels per character). Requires Unicode 16 support (released 2024). BlockCanvasMedium resolution. Uses block characters where each character represents 4 pixels. Pixels have no visible spacing. HeatmapCanvasLow resolution. Uses foreground and background terminal colors to represent 2 color pixels per character. The number of rows is half the number of ycoordinates.AsciiCanvasLow resolution. Uses standard ASCII characters. Best for environments with limited Unicode support or when printing to files. DotCanvasLow resolution. Uses standard ASCII characters. DensityCanvasInstead of marking pixels, it increments a counter per character to track pixel frequency, allowing for density-based data visualization. BarplotGraphicsA special graphics area for barplots that does not support pixel manipulation. It only supports the addrow!method to add bars.Configure 3D Plot Views
main3D plots use a Model-View-Projection (MVP) transformation. You can control the camera using the following keywords:
elevation: Angle above/below the floor plane ($-90 ≤ θ ≤ 90$).azimuth: Azimuthal angle around theupvector ($-180^° ≤ φ ≤ 180^°$).up: The up vector (:x,:y, or:z). Prefix withmorpto change the sign (e.g.,:mzfor $-z$).zoom: Zooming factor.
Other 3D options:
projection: Set to:persp(ective)or:ortho(graphic).axes3d: Boolean to display $x$, $y$, and $z$ axes.near/far: Clipping plane distances (for:perspectiveonly).
Quickstart with UnicodePlots
mainTo create a basic line plot, use the
lineplotfunction. You can providexandycoordinates, along with metadata liketitle,name(for the series), and axis labels.For better results when printing to a file, you can specify different
Canvastypes likeAsciiCanvas,DotCanvas, orBlockCanvasusing thecanvaskeyword.using UnicodePlots lineplot([-1, 2, 3, 7], [-1, 2, 9, 4], title="Example", name="my line", xlabel="x", ylabel="y") # Using a different canvas for file output plt = lineplot([-1, 2, 3, 7], [-1, 2, 9, 4], title="Example", name="my line", xlabel="x", ylabel="y", canvas=DotCanvas, border=:ascii)Get started with UnicodePlots.jl
mainA basic line plot can be created using the
lineplotfunction. You can providetitle,name(for the legend), and axis labels usingxlabelandylabel.using UnicodePlots lineplot([-1, 2, 3, 7], [-1, 2, 9, 4], title="Example", name="my line", xlabel="x", ylabel="y")Create complex layouts with gridplot
mainWhile
UnicodePlotsintegrates withPlots.jlfor basic layouts, complex grids require thegridplotfunction and theTerm.jlpackage.Use
UnicodePlots.panel()to wrap individual plots before combining them with arithmetic operators (*,/) or usinggridplot().Example using arithmetic operators:
using UnicodePlots, Term (UnicodePlots.panel(lineplot(1:2)) * UnicodePlots.panel(scatterplot(rand(100)))) / (UnicodePlots.panel(lineplot(2:-1:1)) * UnicodePlots.panel(densityplot(randn(1_000), randn(1_000))))Example using
gridplot:# Grid with specific layout gridplot(map(i -> lineplot(-i:i), 1:3); layout=(2, nothing)) # Grid with placeholders gridplot(map(i -> lineplot(-i:i), 1:5); show_placeholder=true)using UnicodePlots, Term ( UnicodePlots.panel(lineplot(1:2)) * UnicodePlots.panel(scatterplot(rand(100))) ) / ( UnicodePlots.panel(lineplot(2:-1:1)) * UnicodePlots.panel(densityplot(randn(1_000), randn(1_000))) )Install UnicodePlots.jl
mainTo install UnicodePlots, use Julia's native package manager
Pkgwithin the Julia REPL:using Pkg Pkg.add("UnicodePlots")Save plots as PNG or TXT
mainYou can save plots using the
savefigcommand.- TXT: Supported by default.
- PNG: Experimental. Requires
import FreeType, FileIOto be called before loadingUnicodePlots.
To recover a plot as a string containing ANSI color codes, use
string(p; color=true).Install UnicodePlots
mainTo install UnicodePlots, use the native Julia package manager
Pkgwithin the Julia REPL.using Pkg Pkg.add("UnicodePlots")Configure plot dimensions and axes
mainYou can customize the appearance and orientation of your plots using several keywords:
- Dimensions: Use
height=:autoand/orwidth=:autoto match the current terminal size. When usingwidth=:auto, it is recommended to setcompact=trueto maximize the plot size. - Flipping: Use
xflip=trueand/oryflip=trueto reverse/flip the axes. - Canvas Types: Use
canvas=DotCanvas,canvas=AsciiCanvas, orcanvas=BlockCanvasfor different rendering styles.
- Dimensions: Use