Skip to content

Commit

Permalink
fix legend ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
daviehh committed Mar 5, 2019
1 parent 0ff5b81 commit 83c2a27
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/basic_recipes/legend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ function make_label(scene, plot, labeltext, i, attributes)
:labelwidth, :gap, :markersize, :textsize,
:linepattern, :scatterpattern, :padding
))

num_of_labels = length(getindex(attributes, :labels)[])

_scale(x, w, pad, g, t) = Point2f0(
pad + (x[1]w),
pad + floor(t/2) + x[2]w + ((i - 1) * g)
pad + floor(t/2) + x[2]w + ((num_of_labels - i) * g)
)
scale(args...) = _scale.(args...)

Expand Down Expand Up @@ -98,8 +101,9 @@ function plot!(plot::Legend)

map_once(labels, args...) do labels, w, gap, tgap, padding, font...
start!(textbuffer)
for i = 1:length(labels)
yposition = (i - 1) * gap
num_of_labels = length(labels)
for i = 1:num_of_labels
yposition = (num_of_labels - i) * gap
tsize = floor(font[1] / 2) # textsize at position one, half of it since we used centered align
xy = Point2f0(w + padding + tgap, yposition + tsize + padding)
push!(
Expand Down

1 comment on commit 83c2a27

@daviehh
Copy link
Owner Author

@daviehh daviehh commented on 83c2a27 Mar 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerning: MakieOrg/Makie.jl#308 and MakieOrg/Makie.jl#302

e.g. the example listed in MakieOrg/Makie.jl#308,
https://simondanisch.github.io/ReferenceImages/gallery///labels//index.html

the labels are printed back-to-front, e.g. attribute 4 ... 1 instead of 1...4.


Changes order of legends: currently, legends are printed back-to-front (with increasing y position value); this PR changes the y position of text and graphics from i-1 to n-i, where n=num_of_labels is the total number of labels. Changes are made to the text and graphical part of the legends.

  • For the text part: labels is an array of strings, so num_of_labels = length(labels)
    [in map_once(labels, args...)]

  • For the graphical part [in function make_label], the total number of labels is obtained from attributes[:labels], which is an Observable, so I used [] to convert to a normal array (is this the acceptable way of doing this?)


example:

using Makie
using Colors

xspan = 0:.01:4*pi
expm(x) = exp(-x)
fs = [sin, cos, sinc, expm]
y = [fs[i](x) for x in xspan, i in 1:size(fs,1)]


function plot2d(x::Union{AbstractRange,AbstractArray{<: AbstractFloat, 1}}, y::AbstractArray{<: AbstractFloat, 2};legend_desc=[], kwargs...)
    scene = Scene()
    ny = size(y,2)
    colors = distinguishable_colors(ny+1,[RGB(1,1,1)])[2:end]
    if size(legend_desc,1) != ny
        legend_desc = ["data $i" for i in 1:ny]
    end
    p=[]
    for i in 1:ny
            pli=lines!(scene, x, y[:,i], color = colors[i];kwargs...)
            push!(p,pli[end])
    end
    ls = legend(p, legend_desc, camera = campixel!, raw = true)

    vbox(scene, ls)
end


@time plot2d(collect(xspan), y, linewidth = 2,legend_desc=string.(fs))

screen shot 2019-03-05 at 4 33 51 pm

julia> Pkg.test("AbstractPlotting")
   Testing AbstractPlotting
 Resolving package versions...
    Status `/var/folders/d8/hcnrglbn5ts52xx1hp_wx4480000gn/T/tmp38RytH/Manifest.toml`
  [621f4979] AbstractFFTs v0.3.2
  [537997a7] AbstractPlotting v0.9.5 [`~/code/learn/julia/dev_makie/dev/AbstractPlotting`]
  [9e28174c] BinDeps v0.8.10
  [b99e7846] BinaryProvider v0.5.3
  [a2cac450] ColorBrewer v0.4.0
  [3da002f7] ColorTypes v0.7.5
  [c3611d14] ColorVectorSpace v0.6.2
  [5ae59095] Colors v0.9.5
  [34da2185] Compat v1.5.1
  [8f4d0f93] Conda v1.2.0
  [d38c429a] Contour v0.5.1
  [864edb3b] DataStructures v0.15.0
  [7a1cc6ca] FFTW v0.2.4
  [5789e2e9] FileIO v1.0.5
  [53c48c17] FixedPointNumbers v0.5.3
  [b38be410] FreeType v2.1.1
  [663a7486] FreeTypeAbstraction v0.4.2
  [4d00f742] GeometryTypes v0.7.3
  [a2bd30eb] Graphics v0.4.0
  [a09fc81d] ImageCore v0.7.4
  [6218d12a] ImageMagick v0.7.1
  [8197267c] IntervalSets v0.3.1
  [c8e1da08] IterTools v1.1.1
  [682c06a0] JSON v0.20.0
  [dbb5928d] MappedArrays v0.2.1
  [e1d29d7a] Missings v0.4.0
  [77ba4419] NaNMath v0.3.2
  [510215fc] Observables v0.2.3
  [6fe1bfb0] OffsetArrays v0.10.0
  [bac558e1] OrderedCollections v1.0.2
  [19eb6ba3] Packing v0.3.0
  [5432bcbf] PaddedViews v0.4.2
  [995b91a9] PlotUtils v0.5.5
  [189a3867] Reexport v0.2.0
  [992d4aef] Showoff v0.2.1
  [73760f76] SignedDistanceFields v0.4.0
  [a2af1166] SortingAlgorithms v0.3.1
  [276daf66] SpecialFunctions v0.7.2
  [90137ffa] StaticArrays v0.10.3
  [2913bbd2] StatsBase v0.29.0
  [30578b45] URIParser v0.4.0
  [1cfade01] UnicodeFun v0.4.0
  [81def892] VersionParsing v1.1.3
  [2a0f44e3] Base64  [`@stdlib/Base64`]
  [ade2ca70] Dates  [`@stdlib/Dates`]
  [8bb1440f] DelimitedFiles  [`@stdlib/DelimitedFiles`]
  [8ba89e20] Distributed  [`@stdlib/Distributed`]
  [b77e0a4c] InteractiveUtils  [`@stdlib/InteractiveUtils`]
  [76f85450] LibGit2  [`@stdlib/LibGit2`]
  [8f399da3] Libdl  [`@stdlib/Libdl`]
  [37e2e46d] LinearAlgebra  [`@stdlib/LinearAlgebra`]
  [56ddb016] Logging  [`@stdlib/Logging`]
  [d6f4376e] Markdown  [`@stdlib/Markdown`]
  [a63ad114] Mmap  [`@stdlib/Mmap`]
  [44cfe95a] Pkg  [`@stdlib/Pkg`]
  [de0858da] Printf  [`@stdlib/Printf`]
  [3fa0cd96] REPL  [`@stdlib/REPL`]
  [9a3f8284] Random  [`@stdlib/Random`]
  [ea8e919c] SHA  [`@stdlib/SHA`]
  [9e88b42a] Serialization  [`@stdlib/Serialization`]
  [1a1011a3] SharedArrays  [`@stdlib/SharedArrays`]
  [6462fe0b] Sockets  [`@stdlib/Sockets`]
  [2f01184e] SparseArrays  [`@stdlib/SparseArrays`]
  [10745b16] Statistics  [`@stdlib/Statistics`]
  [8dfed614] Test  [`@stdlib/Test`]
  [cf7118a7] UUIDs  [`@stdlib/UUIDs`]
  [4ec0a83e] Unicode  [`@stdlib/Unicode`]
   Testing AbstractPlotting tests passed

Please sign in to comment.