Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

python 3.x - Changing Opacity by a function

I have created a 3d scatter plot that changes colour based around a separate variable, Intensity. Ideally I would change the opacity so that the lower intensity colours are less visible. To do this I created a separate function that should return a different value, to be placed as the alpha value, for each intensity value based upon its size compared to the max intensity value. When I run this though all scatter points receive the first opacity value I put into my function. I can not spot the error and would appreciate if someone could cast there eye over it.

Section of code that generates plot and opacity function:

'''
Trial for opacity differentiation
'''
   
def OP(b):
    for i in range(len(b)):
        Imx = np.amax(b) 
        print(Imx)
        if b[i] > .9*Imx:
            return .9
        elif b[i] <= .9*Imx:
            return 0.1
        else:
            return 0
    
'''
3d Colour scatterplot of Intensity
'''
def hlkplt(filename):
    h = np.linspace(0,4,9)
    l = np.linspace(0,4,9)
    k = np.linspace(0,4,9)
    I = []
    for j in range(len(h)):
        for i in range(len(l)):
            for n in range(len(k)):
                IStot = Int2(filename,h[j],l[i],k[n])
                p = IStot.real
                I.append(p)
    

    
    b = np.array(I)
    
    hh, ll, kk = np.meshgrid(h,l,k)
    
    cm = plt.get_cmap('RdYlGn')
    fig = plt.figure()

    ax3D = plt.axes(projection = '3d')    
    ax3D.set_xlabel('h plane')
    ax3D.set_ylabel('l plane')
    ax3D.set_zlabel('k plane')
           
    p3d = ax3D.scatter(hh,ll,kk, s = 30, c = b,alpha =OP(b), marker ='o',label = filename)

    plt.legend()
    cbar = plt.colorbar(p3d)
    cbar.set_label('Scattering Intensity of neutrons')
    plt.show()
    
    return

The Int2 function just produces a complex number which is turned real and placed into a list

question from:https://stackoverflow.com/questions/65885828/changing-opacity-by-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...