# Using group in scatter plots correctly?

**URL:** <https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132>\
**Category:** New to Julia\
**Created:** [August 2, 2020, 1:40pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132 "2020-08-02T13:40:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 1:40pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/1 "2020-08-02T13:40:21Z")

</div>

Hello!

I am trying to make a scatterplot where there exist two groups, one called “0” and one called “1”. The group values is stored in a vector “z”. Over time the points in group “0”, come to group “1”, something like this:

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

![image](https://global.discourse-cdn.com/julialang/original/3X/2/2/222e52109552edeec306613bbd40540c4cd0aad0.png)

But in the final time step, when all have come to group “1”, the color just becomes “blue” - I want it to stay orange or some other color I set:

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

How do I achieve this, using Plots? My current command is:

```julia
p = scatter((data[1,:],data[2,:]),
            group = z,
            markersize = 3.5,
            markerstrokestyle = :dot,
            aspect_ratio=:equal
            );

```

Kind regards

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2020, 2:37pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/2 "2020-08-02T14:37:59Z")

</div>

The easiest solution here is to pull a slight of hand and make the orange group `0` and the blue group `1`, then use the `labels` option to change your legend and the `colors` option to change the colors.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 3:35pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/3 "2020-08-02T15:35:58Z")

</div>

Hi!

To do this I would need to split my data some way, and I am not sure how to do it correctly in Julia. In Matlab I would use “~” to get reverse indices, i.e. if I have a vector of 0 and 1, then using “~”, I could switch it to 1 and 0 - hope it makes sense.

How would I do that in Julia?

Kind regards

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 4:32pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/4 "2020-08-02T16:32:40Z")

</div>

I managed to get it to work now using “findall”. Now I wonder how can I remove a legend entry (a label), when no more exist?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/a/2afa7a8eb06dfc22b89a86e08ca61fde7c487824.png)

Kind regards

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 2, 2020, 4:40pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/5 "2020-08-02T16:40:47Z")

</div>

I think you are looking for:

```julia
p = scatter((data[1,:],data[2,:]),
                   group = z,
                   color_palette=[:orange,:black],
                   markercolor=:match,
                   markersize = 3.5,
                   markerstrokestyle = :dot,
                   aspect_ratio=:equal
                   )

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [August 2, 2020, 5:21pm UTC](https://discourse.julialang.org/t/using-group-in-scatter-plots-correctly/44132/6 "2020-08-02T17:21:28Z")

</div>

Thanks for your suggestion, but unfortunately, it did not work as expected.

I switched to PGFPlotsX backend, now it works as I want:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f4040ba5cce73afc39247fc5692fa74230a1d5c.png)

Kind regards, and thanks for the help everybody
