Gadfly and controling colors and label

I like the syntax of plotting using Gadfly, but it is relatively far from my experience with plotting with Matlab and Plots.

I wonder if there is an easy way how to control the colors and labels in the following example:

using Gadfly, ColorSchemes

p = plot()

for ii = 1:10

    l = layer(y = rand(10), Geom.line)

    push!(p, l)

end

p
using  Cairo, Fontconfig
draw(PNG("test.png"), p)

I get this output
test

To change the color, just add color=[ii] to your layer (note ii can be a string).

Gadfly can plot wide-format data (i.e. data in a matrix) too, see examples in Gadfly docs. In your example, if your data was in a matrix, when plotting in wide-format Gadfly puts 10 lines in 1 layer (and colors them), whereas your example above has 1 line in 10 layers (layers are computationally more expensive than lines).

2 Likes

That was easy, thank you very much.

You are right with the wide-format, but in my real use case the number of data points will vary in each iteration.

Is there an easy way how to change the color scheme. I have found only examples for continuous scale

cpalette(p) = get(ColorSchemes.viridis, p)
p1 = plot(D[1], y=:y, x=:x, color=0:29, Geom.bar,
    Scale.color_continuous(colormap=cpalette),
    Theme(bar_spacing=-0.2mm, key_position=:none))

Maybe like this?

using Gadfly
using ColorSchemes: viridis

palette = [ get(viridis, i) for i in range(0, length=10, stop=1) ]

p = plot()

for i in 1:10
    l = layer(y = rand(10), Geom.line, color=[palette[i]])
    push!(p, l)
end

push!(p, Guide.manual_color_key("", ["label $i" for i in 1:10] , [palette...]))
1 Like

Thank you

Here’s a plot that combines an example of plotting a “vector of vectors” (of different lengths), and an example of a viridis discrete colorscheme:

import ColorSchemes as cs

ys = [cumsum(randn(rand(10:20))) for _ in 1:5]

plot(ys, x=Row.index, y=Col.value, color=Col.index, Geom.line,
        Scale.color_discrete(n->get(cs.viridis, range(0, 1, length=n))))

two_examples

The “vector of vectors” bit assumes that the x’s are the same, we should investigate handling a “vector of matrices with x & y”.

2 Likes

Very nice

Sorry to ask you again. Is there way how to set the background colour of colorkey?
I would like to place it inside the plot.

Its an open issue and would be easy to add, but time is against me atm :hourglass_flowing_sand:

I see.

Thank you for answers and good work done on Gadfly