# Gadfly and controling colors and label

**URL:** <https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [April 14, 2021, 5:53pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290 "2021-04-14T17:53:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 14, 2021, 5:53pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/1 "2021-04-14T17:53:25Z")

</div>

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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/a/9/a98ba7d3ff9c8730332d741576022062cca6421d.png)

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [April 14, 2021, 6:24pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/2 "2021-04-14T18:24:10Z")

</div>

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](http://gadflyjl.org/stable/man/plotting/#Wide-formatted-data). 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).

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 14, 2021, 6:48pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/3 "2021-04-14T18:48:05Z")

</div>

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

```julia
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))

```

---

<div class="post-metadata">

**Author:** ![Igor\_Douven](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/igor_douven/32/8433_2.png) [@Igor\_Douven](https://discourse.julialang.org/u/Igor_Douven)\
**Post date:** [April 14, 2021, 7:07pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/4 "2021-04-14T19:07:27Z")

</div>

Maybe like this?

```julia
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...]))

```

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 14, 2021, 7:17pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/5 "2021-04-14T19:17:43Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [April 15, 2021, 12:16am UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/6 "2021-04-15T00:16:52Z")

</div>

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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/8/9/89a749323e42d31ce18b14cb1e72fe4d066ece04.png)

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

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 15, 2021, 12:02pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/7 "2021-04-15T12:02:48Z")

</div>

Very nice

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 15, 2021, 1:34pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/8 "2021-04-15T13:34:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [April 15, 2021, 2:41pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/9 "2021-04-15T14:41:53Z")

</div>

Its [an open issue](https://github.com/GiovineItalia/Gadfly.jl/issues/1515) and would be easy to add, but time is against me atm ⏳

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 15, 2021, 4:15pm UTC](https://discourse.julialang.org/t/gadfly-and-controling-colors-and-label/59290/10 "2021-04-15T16:15:25Z")

</div>

I see.

Thank you for answers and good work done on Gadfly
