Enola Henrotin
Enola Henrotin

Reputation: 55

Change points colors on only few data stored in a list scatter plot

I have a scatter plot as you can see below.

enter image description here

I want to change color points for each year which is stored in a list, here is a reproducible code :

import matplotlib.pyplot as plt
from adjustText import adjust_text

histo = [1949,1976,1990,1991] #here is the list with years that I want in red 
a, b, c, size, texts=[], [], [], [], []
year=[1970, 1971, 1974, 1976, 1978, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999]
meanYear={1970: 6.524064171122993, 1971: 11.934731934731936, 1974: 20.320855614973265, 1976: 5.925925925925922, 1978: 10.43478260869565, 1980: 4.242424242424238, 1981: 16.363636363636367, 1982: 17.748917748917748, 1983: 3.6363636363636394, 1984: 2.857142857142852, 1985: 12.855831037649216, 1986: 9.836829836829834, 1988: 17.045454545454547, 1989: 18.859784283513093, 1990: 21.346801346801346, 1991: 20.559440559440556, 1992: 15.75757575757576, 1993: 1.818181818181812, 1994: 6.363636363636365, 1996: 16.363636363636363, 1997: 10.267379679144385, 1998: 6.77685950413223, 1999: 7.27272727272728}

sumDif={1970: 110.90909090909088, 1971: 465.4545454545455, 1974: 690.909090909091, 1976: 159.9999999999999, 1978: 239.99999999999994, 1980: 12.727272727272714, 1981: 98.1818181818182, 1982: 372.7272727272727, 1983: 18.181818181818198, 1984: 19.99999999999964, 1985: 1272.7272727272723, 1986: 383.6363636363635, 1988: 136.36363636363637, 1989: 1112.7272727272725, 1990: 1152.7272727272727, 1991: 534.5454545454545, 1992: 47.27272727272728, 1993: 7.272727272727248, 1994: 12.72727272727273, 1996: 49.09090909090909, 1997: 523.6363636363636, 1998: 74.54545454545453, 1999: 7.27272727272728}
nbDay={1985: 99, 1989: 59, 2016: 55, 1990: 54, 1997: 51, 1971: 39, 1986: 39, 1974: 34, 1976: 27, 1991: 26, 1978: 23, 1982: 21, 1970: 17, 1998: 11, 1988: 8, 1984: 7, 1981: 6, 1983: 5, 1993: 4, 1980: 3, 1992: 3, 1996: 3, 2018: 3, 1994: 2, 1999: 1}

for key in year : 
    pos_x= nbDay[key]
    pos_y=meanYear[key]
    size=sumDif[key] #etiquette
    a.append(pos_x)
    b.append(pos_y)
    c.append(size)
    texts.append(plt.text(pos_x,pos_y,key))
    
d=[0.5*n for n in c] #point size 

plt.scatter(a,b,s=d,c="orange")
adjust_text(texts,a,b)   
plt.xlabel("titlex")
plt.ylabel("titley")
plt.title("title")

What I want : The point corresponding to years in histo in red and the others in orange.

I already tried to create another scatter plot with only these points but it doesn't work because the point size and their position change (I don't know why)

I didn't use annotate because my plot usually are on a lot of data and it was really messy and illegible. It works just fine with adjustText.

If you can help me a bit ! Thank you !

Upvotes: 0

Views: 127

Answers (1)

RandomGuy
RandomGuy

Reputation: 1207

Simply pass a list of colors as an argument :

colors = [('red' if y in histo else 'orange') for y in year]
plt.scatter(a,b,s=d,c=colors)

See the plt.scatter documentation for more details.

Upvotes: 3

Related Questions