0

I have three identical length lists for scatter plotting: x (float), y (float) and c (integer for colour), and would like to split up the x and y lists into subsets filtered by the colour value so that I can use a legend the delineate them in a plot

While I could achieve this with len(c) loops over the x and y lists, it is not very pythonic, and I was hoping someone could provide something a bit more elegant

I was thinking something like the following, but it's clearly not working

    c_list = list(set(c))
    xset = []
    yset = []
    for j in c_list:
        xset.append([i for i in x if j in c])
        yset.append([i for i in y if j in c])

Be gentle - I've only been learning Python for a week or so!

Thanks in advance

1 Answer 1

1

I hope this helps:

x = [1, 2, 3, 4, 5]
y = [5, 3, 1, 3, 2]
c = [1, 3, 2, 3, 1]

c_list = list(set(c))
xset = []
yset = []
for j in c_list:
    xset.append([x[i] for i, v in enumerate(c) if v == j])
    yset.append([y[i] for i, v in enumerate(c) if v == j])

print(xset)
print(yset)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.