开发者

How to change the colour of a scatterplot in a loop/if statement? (Python)

开发者 https://www.devze.com 2023-03-28 20:45 出处:网络
I\'m trying to make it so that if the scatterplot for Events is above the Diatomite scatterplot, then the color of the plot changes from blue to red. (Similarly from blue to yellow if otherwise).

I'm trying to make it so that if the scatterplot for Events is above the Diatomite scatterplot, then the color of the plot changes from blue to red. (Similarly from blue to yellow if otherwise).

I've looked around for quite some time, but it doesn't seem to be working.

Here's the code as follows:

for line in open('C:\...'):
    line = line.split()
    y = line[9]
    if y == "END":
        break
    x = line[10]
    if x == 'END':
        break
    z = line[11]
    if z == 'END':
    break
    x0.append(float(x))
    y0.append(float(y))
    z0.append(float(z))

for line in open('C:\...'):
    line=line.split()
    c = line[0]
    a = line[1]
    b = line[2]
    a0.append(float(a))
    b0.append(float(b))
    c0.append(float(c))
##    print c0
    Tave = average(c0)
    print Tave


fig = pylab.figure() 
ax = p3.Axes3D(fig) 
Diatomite = ax.scatter(a0,b0,c0, color='green')
Events = ax.scatter(x0, z0,开发者_如何学编程 y0, color='b')
pyplot.show() 

for value in y0[:]:
    if value > Tave:
        ax.scatter(color = 'red')
    else:
        ax.scatter(color = 'yellow')

scatter(color=colors)

Any help would be greatly appreciated! (Please note that I took out the path to the file.)


It looks like you almost have it. First define the colors, then make the scatter plot. So after print Tave, build a list containing the colors; I used a list comprehension to do this:

y0colors = ['red' if value > Tave else 'yellow' for value in y0]

Then when you make 'Events', set color=y0colors, like so:

Events = ax.scatter(x0, z0, y0, color=y0colors)

Of course with the requirement that you're using, none of the points will be blue. The y0 values are either greater than Tave ('red') or not ('yellow'). There might be a way to do this after defining the scatter plot 'Events', but this way seems more direct.

0

精彩评论

暂无评论...
验证码 换一张
取 消