My problem is very similar to the one in: python - scatter plot with dates and 3rd variable as color
But I want the colors to vary acording to 3 set of values inside my 3rd variable.
for example:
#my 3rd variable consists of a column with these planet radii values:
radii
1 70
2 6
3 54
4 3
5 0.3
...
And I expect to vary the colors according to radii>8, 4< radii<8 and radii<4.
I've tried using the simple code, presented in the other question:
db=table_with_3_columns()
x=db['column a']
y=db['column b']
z=db['radii']
plt.scatter(x,y,c=z,s=30)
But I don't know how to specify the 'c' parameter for different sets inside z. I've also tried using:
a=[]
for i in db['radii']
if i>8:
a['bigradii']=i
elif i<4:
a['smallradii']=i
elif i<8 and i>4:
a['mediumradii']=i
return a
but I don't know how to proceed with that.
The result would be a scatter with the dots separated by colors guided by the values in the 3rd column 'radii', but all I get using the first code is all the dots black, or, by using the second code it tells me that i is a string, and I cannot put that on a list :(
How can I achieve that?
I think what you should do is:
list
which later will be passed to 'c' in the scatter function.Here is an example of what I mean:
import numpy as np
import matplotlib.pyplot as plt
# x and y will have 100 random points in the range of [0,1]
x = np.random.rand(100)
y = np.random.rand(100)
# z will have 100 numbers, in order from 1 to 100
# z represents your third variable
z = np.arange(100)
colors = []
# add 1 or 2 to colors according to the z value
for i in z:
if i > 50:
colors.append(2)
else:
colors.append(1)
# half the points will be painted with one color and the other half with another one
plt.scatter(x, y, c=colors,)
plt.show()