# Alternate color for each point in scatter plot and add legend

**URL:** <https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856>\
**Category:** New to Julia\
**Tags:** plots\
**Created:** [March 10, 2021, 5:58am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856 "2021-03-10T05:58:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [March 10, 2021, 5:58am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/1 "2021-03-10T05:58:47Z")

</div>

I’m plotting a series where I’d like to alternate colors for each point. This works fine via the `mc` keyword, but I can’t get the legend to indicate the color coding scheme correctly:

```julia
using Plots
a = rand(10); b = rand(10);
scatter(a, b, mc = [:navy, :crimson], label = ["odd", "even"])

```

![hi](https://global.discourse-cdn.com/julialang/original/3X/8/b/8b8b42122365ccb206e00f3cc71db328a4cc7f78.png)

How can I fix the legend?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 10, 2021, 6:26am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/2 "2021-03-10T06:26:25Z")

</div>

Just plot the two sets separately?

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [March 10, 2021, 6:36am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/3 "2021-03-10T06:36:47Z")

</div>

This is fine in the two-color case, but suppose I wanted to code them based on their index mod 5.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 10, 2021, 6:53am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/4 "2021-03-10T06:53:07Z")

</div>

You can do a `for` loop over the 5 sets.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 10, 2021, 7:08am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/5 "2021-03-10T07:08:38Z")

</div>

I think you need to transpose the label vector

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [March 11, 2021, 6:07am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/6 "2021-03-11T06:07:52Z")

</div>

@dpsanders Of course, if there’s no way to accomplish this via the `label` keyword, one can write a for loop. But this is a rather inelegant solution, don’t you agree?

```julia
using Plots

function plotter()
    a = rand(100); b = rand(100);
    idx_container = [i:5:100 for i in 1:5]
    colors = [:navy, :crimson, :olivedrab, :rebeccapurple, :goldenrod]
    labels = ["$i mod 5" for i in 1:5]

    fig = plot()

    for (i, idx) in enumerate(idx_container)
        scatter!(fig, a[idx], b[idx], mc = colors[i], label=labels[i])
    end
    return fig
end

plotter()

```

![hi](https://global.discourse-cdn.com/julialang/original/3X/3/d/3dfa3ff42f449d7626d37278a118991885f60bbe.png)

You have to create a one array (or `@view`) for the 1st, 6th, 11th entries, another for the 2nd, 7th, and 12th, etc. This results in code far less legible than a list of labels, and calls `plot` more times then seemingly necessary.

@nilshg Removing the comma from the label vector doesn’t work, unfortunately:

![hi](https://global.discourse-cdn.com/julialang/original/3X/2/3/23bb84a643a62d38ea9d92084a76d1bcdcaac1f6.png)

`plots` still regards the data as only one series, labeled “odd.”

---

<div class="post-metadata">

**Author:** ![ivirshup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivirshup/32/16364_2.png) [@ivirshup](https://discourse.julialang.org/u/ivirshup)\
**Post date:** [March 11, 2021, 7:08am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/7 "2021-03-11T07:08:42Z")

</div>

The for loop doesn’t seem so bad to me, but you could reshape? Something like:

```julia
group_by_mod(x, n) = reshape(x, (n, :))'

x = repeat(1:5, outer=5)
y = repeat(1:5, inner=5)

scatter(group_by_mod.((x, y), 5)...)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/8/0/807e0959fb4f1cb3a6d90f71157479d5490dbe7e.png)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 11, 2021, 7:13am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/8 "2021-03-11T07:13:32Z")

</div>

Ah fair enough - I have to agree with `Plots`, you are passing just one series. The best best is then probably reshaping as above or just passing two series:

```julia
julia> scatter([a[1:2:end], a[2:2:end]], [b[1:2:end], b[2:2:end]], mc = [:navy :crimson], label = ["odd" "even"])

```

But if you are looking to plot loads of series, reshape is the way to go I’d think.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 11, 2021, 7:23am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/9 "2021-03-11T07:23:18Z")

</div>

That seems like a nice, clear solution to me.

---

<div class="post-metadata">

**Author:** ![ivirshup](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivirshup/32/16364_2.png) [@ivirshup](https://discourse.julialang.org/u/ivirshup)\
**Post date:** [March 11, 2021, 7:24am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/10 "2021-03-11T07:24:38Z")

</div>

Oh, or `group`. `group` is probably what you want.

```julia
scatter(x, y, group=repeat('a':'e', 5))

```

![image](https://global.discourse-cdn.com/julialang/original/3X/e/d/ede6bbd41efa4910ae22a55e14a7594f180ca659.png)

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [March 11, 2021, 7:49am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/11 "2021-03-11T07:49:44Z")

</div>

This is exactly what I was hoping for! Thank you. Always pleasant not to have to reinvent the wheel for tasks like this.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 11, 2021, 8:04am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/12 "2021-03-11T08:04:03Z")

</div>

The comprehension option below is compact enough and allows controlling the markers’ colors, sizes, shapes, etc.:

```julia
using Plots; gr() 
N=100; a=rand(N); b=rand(N);
Nc=5; colors=[:navy, :crimson, :olivedrab, :rebeccapurple, :goldenrod]
mshape=[:circle, :rect, :star5, :diamond, :hexagon]
labels=["$i mod $Nc" for i in 1:Nc]
fig = plot(legend=:outertopright)
[scatter!(fig, a[j:Nc:end], b[j:Nc:end], mc=colors[j], label=labels[j], shape=mshape[j], ms=j+3) for j in 1:Nc]
display(fig)

```

 ![scatter_alternate_Nc_points](https://global.discourse-cdn.com/julialang/original/3X/8/4/84876e867b196ecf179f061796b096ed6eda0544.png)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 11, 2021, 8:07am UTC](https://discourse.julialang.org/t/alternate-color-for-each-point-in-scatter-plot-and-add-legend/56856/13 "2021-03-11T08:07:28Z")

</div>

Nice solution! I personally prefer to avoid array comprehensions containing functions with side effects and just write a for loop instead.
