Annotations and line widths in Plots.jl heatmaps

An example speaks louder than a thousand words.

using Plots
@userplot RecipesAreEasy
@recipe function f(x::RecipesAreEasy; annotationargs = ())
    y = x.args[1]              #Get the input arguments, stored in x.args - in this case there's only one
    typeof(y) <: AbstractMatrix || error("Pass a Matrix as the arg to heatmap")

    grid := false                      # turn off the background grid
    
    @series begin                      # the main series, showing the heatmap
        seriestype := :heatmap
        y
    end

    rows, cols = size(y)

    #horizontal lines
    for i in 0:cols         # each line is added as its own series, for clearer code
        @series begin
            seriestype := :path
            primary := false          # to avoid showing the lines in a legend
            linecolor --> :white
            [i, i] .+ 0.5, [0, rows] .+ 0.5  # x and y values of lines
        end
    end

    for i in 0:rows
        @series begin
            seriestype := :path
            primary := false
            linecolor --> :white
            [0, cols] .+ 0.5, [i,i] .+ 0.5
        end
    end

    @series begin
        seriestype := :scatter
        markerstrokecolor := RGBA(0,0,0,0.)  # make the points transparent - setting marker (or seriestype) to :none doesn't currently work right
        seriescolor := RGBA(0,0,0,0.)        # do
        series_annotations := text.(1:(cols*rows), annotationargs...)
        primary := false
        repeat(1:cols, inner = rows), repeat(1:rows, outer = cols)
    end
end

Then

using Plots; pyplot() # currently broken in gr :-(
recipesareeasy(reshape(1:40, 8,5), annotationargs = (10, "Arial", :lightgrey)) #you can also use linecolor , linestyle, linewidth etc to modify the lines

8 Likes