0

I'm creating a scatter plot in MatPlotLib and I'm trying to make the color of the points dependent on a third parameter (independent of X and Y). However, setting c=third_variable makes all of the dots the same color.

My plot data is in a dictionary with a tuple of x and y data as the key and the third parameter (frequency, ranges from 1 to about 1000) as its value. Sample item: {[2 10]: 50}

I want the colors of the points tied to frequency. As a starting place, I would like to make it such that dots with high frequency are darker and dots with low frequency are lighter.

Here's my current output:current output And here's my code:

for key in pairs_hash:
    plt.scatter(key[0], key[1], c=pairs_hash[key], cmap=plt.cm.coolwarm)

plt.show()

Thanks for the help!

1
  • More code is needed to have a closer look Commented Jun 26, 2017 at 22:18

1 Answer 1

2

I guess your major bad design choice is plotting the points in the loop one point at a time. Why not plot them all at once?

xy, z = zip(*pairs_hash.items())
x, y = zip(*xy)
plt.scatter(x, y, c=z, cmap=plt.cm.coolwarm)
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

I made the changes you suggested but the results are the same. Could the issue be that MatPlotLib is interpreting c as being a sequence of floats (like you would give it if you had an array of number values) rather than a singular value and trying to apply that sequence to all the dots?
Would you mind explaining the syntax of the first two lines a bit? I'm not a python programmer (school was C++, work is JavaScript) but I would guess just by looking that it's a question of tuples?
x,y,...=zip(*X) takes a list of tuples X and creates a tuple of lists of the first elements, second elements, etc. It is the opposite of X=zip(x,y,...).

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.