Symbols in Gadfly Plots

I am trying to incorporate some symbols in my xtick labels, but I have failed in using LaTeXStrings and unicode characters. I keep getting the following error:

MethodError: Cannot `convert` an object of type Char to an object of type AbstractString
Closest candidates are:
  convert(::Type{AbstractString}, !Matched::CategoricalString) at C:\Users\jmbyars\.julia\packages\CategoricalArrays\dmrjI\src\value.jl:98
  convert(::Type{T}, !Matched::T) where T<:AbstractString at strings/basic.jl:208
  convert(::Type{T}, !Matched::AbstractString) where T<:AbstractString at strings/basic.jl:209
  ...
setindex!(::Array{AbstractString,1}, ::Char, ::Int64) at array.jl:782
copyto!(::Array{AbstractString,1}, ::Int64, ::Array{Any,1}, ::Int64, ::Int64) at abstractarray.jl:842
append!(::Array{AbstractString,1}, ::Array{Any,1}) at array.jl:911
render(::Gadfly.Guide.XTicks, ::Theme, ::Gadfly.Aesthetics, ::Bool) at guide.jl:585
#render_prepared#107(::Bool, ::Bool, ::typeof(Gadfly.render_prepared), ::Plot, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics, ::Array{Gadfly.Aesthetics,1}, ::Array{Array{Gadfly.StatisticElement,1},1}, ::Array{Array{Gadfly.Aesthetics,1},1}, ::Array{Array{Gadfly.Data,1},1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Array{Gadfly.GuideElement,1}) at Gadfly.jl:833
render_prepared(::Plot, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics, ::Array{Gadfly.Aesthetics,1}, ::Array{Array{Gadfly.StatisticElement,1},1}, ::Array{Array{Gadfly.Aesthetics,1},1}, ::Array{Array{Gadfly.Data,1},1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Array{Gadfly.GuideElement,1}) at Gadfly.jl:813
render(::Plot) at Gadfly.jl:759
draw at Gadfly.jl:864 [inlined]
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("application/prs.juno.plotpane+html")}, ::Plot) at Gadfly.jl:971
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Plot) at multimedia.jl:109
displayinplotpane(::Plot) at showdisplay.jl:51
displayandrender(::Plot) at showdisplay.jl:130
(::Atom.var"#187#191"{String})() at eval.jl:90
#invokelatest#1 at essentials.jl:709 [inlined]
invokelatest at essentials.jl:708 [inlined]
macro expansion at dynamic.jl:24 [inlined]
eval(::String, ::Int64, ::String, ::String, ::Bool) at eval.jl:67
(::Atom.var"#182#183")(::Dict{String,Any}) at eval.jl:62
handlemsg(::Dict{String,Any}, ::Dict{String,Any}) at comm.jl:166
(::Atom.var"#19#21"{Array{Any,1}})() at task.jl:333

I understand that Gadfly want some String type as labels. Is there a suitable workaround or is this a fruitless endeavor? Code listed below.

using Gadfly 

graph_start = -π; graph_stop = π
tick_values = collect(graph_start:π/2:graph_stop)
tick_labels = Dict(zip(tick_values, ['\U002d'*'\U003C0','\U002d'*'\U003C0'*'/'*'2','\U0030','\U003C0'*'/'*'2','\U003C0']))

graph = plot()
Threads.@threads for rows in 1:size(data_matrix)[1]
    push!(graph, layer(x -> function(x, data_matrix[rows,:]), graph_start, graph_stop, Geom.line, Theme(default_color = data_colors[group_vars[rows]])))
end
push!(graph, Guide.xlabel("", orientation = :horizontal), Guide.ylabel("f(t)", orientation = :vertical)) 
push!(graph, Coord.cartesian(xmin = graph_start, xmax = graph_stop)) 
push!(graph, Stat.xticks(ticks = tick_values))
push!(graph, Scale.x_continuous(labels = x -> tick_labels[x]))
1 Like

Okay nevermind. I replaced

tick_values = collect(graph_start:π/2:graph_stop)
tick_labels = Dict(zip(tick_values, ['\U002d'*'\U003C0','\U002d'*'\U003C0'*'/'*'2','\U0030','\U003C0'*'/'*'2','\U003C0']))

with

tick_values = collect(graph_start:π/2:graph_stop)
tick_labels = Dict(zip(tick_values, string.(['\U002d'*'\U003C0','\U002d'*'\U003C0'*'/'*'2','\U0030','\U003C0'*'/'*'2','\U003C0'])))

and got something suitable.

1 Like

see Support LaTeXStrings.jl · Issue #1094 · GiovineItalia/Gadfly.jl · GitHub

1 Like