# Makie.jl - How to plot a colorlegend?

I’m trying to plot a colorlegend when its colorrang is (::Float64,::Float64).
But this sample code doesn’t work.

``````using Makie
using FileIO
using LinearAlgebra
using AbstractPlotting

r = LinRange(-20, 20, 500);  # our value range

ρ(x, y, z) = exp(-((x)^2 + (y)^2 + (z)^2)) # function (charge density)

# create a Scene with the attribute `backgroundcolor = :white`,
# can be any compatible color.  Useful for better contrast and not killing your eyes with a white background.
scene = Scene(backgroundcolor = :white)

volume!(
scene,
r, r, r,          # coordinates to plot on
ρ,                # charge density (functions as colorant)
algorithm = :mip,  # maximum-intensity-projection
colorrange = (0,0.5),
colormap = :blues,
transparency = true
)

update_cam!(scene, Vec3f0(1,1.5,1.0), Vec3f0(0))
scene[Axis].names.textcolor = :gray # let axis labels be seen on dark background

cm = colorlegend(
scene[end],             # access the plot of scene
raw = true,          # without axes or grid
camera = campixel!,  # gives a concrete bounding box in pixels
# so that the `vbox` gives you the right size
width = (            # make the colorlegend longer so it looks nicer
30,              # the width
540              # the height
)
)

scene_final = vbox(scene, cm) # put the colorlegend and the plot together in a `vbox`

save("test.png",scene_final)
``````
``````MethodError: no method matching optimize_ticks(::Int64, ::Float64; k_min=4, k_max=8)
Closest candidates are:
optimize_ticks(::T, !Matched::T; extend_ticks, Q, k_min, k_max, k_ideal, granularity_weight, simplicity_weight, coverage_weight, niceness_weight, strict_span, span_buffer) where T at /Users/username/.julia/packages/PlotUtils/10ojB/src/ticks.jl:136
``````

If colorrange = (0,0.5) → colorrange = (0,1.0), the code passes.
Is there a method to display a colorlegend with colorrange = (0,0.5)?

Could it be that the working code was `colorrange = (0.0, 1.0)` rather than `colorrange = (0, 1.0)`?

I think Makie is just being picky with the types: using `colorrange = (0.0, 0.5)` works for me.

1 Like