0

I have a set of lines with markers. I want these markers to fade through the colormap, say prism for example, dependant on a function, which gives their temperature. Here is my code:

def virial_temp(M=row1[i][2],z=row1[i][3]):
     X = 0.7
     Y = 0.3
     mu = 1/(2*X + 0.75*Y)   #from GL's code
     v_v = math.sqrt(G*M*m_sun/(virial_rad(M,z)*1E+3*parsec))      #virial velocity [m/s]
     return (mu*m_p*v_v**2/(2*k_b))


line1 = plt.plot([namei - len(row1)*0.5, namej - len(row2)*0.5],[row1[i][3]*10, row2[j][3]*10], c=plt.cm.jet(j), lw=2, marker='o', markerfacecolor=plt.cm.rainbow(virial_temp(M=row1[i][2],z=row2[j][3])), markersize=1.5) 

It is only the markerfacecolor part I am struggling with. Lets say if temperature is low (value returned from function is low), the color will the same as the color at the 'first' color on the colormap, and if the tempreture is very high (value returned from function is high), it will be the 'last' color on the colormap. Is there a way to do this?

Any help would be really appreciated as I've been struggling on this for a while now!

Thanks in advance

ali_m
  • 71,714
  • 23
  • 223
  • 298
lyche
  • 125
  • 2
  • 10

1 Answers1

0

Check out this answer

Here's a bit of code to demonstrate my suggestion:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
import random

maximum = 10

n = 10

def pick_color():
    closest_match = 10*maximum #arbitrary high number
    for item in color_pairs:
        if abs(item[0] - num) < closest_match: 
            closest_match = abs(item[0] - num)
            c = item[1]
    return c 

color = iter(cm.rainbow(np.linspace(0,1,n)))
color_pairs = [(value, c) for value, c in zip(range(n+1), color)]

Xs = [i for i in range(n+1)]
random_list = [random.choice(range(maximum +1)) for i in range(n+1)]


for x, num in zip(Xs,random_list):
    c = pick_color()    
    plt.scatter(x, num, s=50, c=c)

plt.show()

Obviously, you wanted a line, etc, but the principal is the same.

Community
  • 1
  • 1
mauve
  • 2,707
  • 1
  • 20
  • 34