如何更改循环/if 语句中散点图的颜色? (Python)

发布于 2024-11-30 15:45:52 字数 977 浏览 0 评论 0原文

我试图做到这一点,如果事件的散点图位于硅藻土散点图之上,则图的颜色从蓝色变为红色。 (如果不是的话,同样从蓝色到黄色)。

我已经环顾四周很长一段时间了,但似乎不起作用。

代码如下:

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)

任何帮助将不胜感激! (请注意,我取出了文件的路径。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-12-07 15:45:52

看起来你几乎已经拥有它了。首先定义颜色,然后绘制散点图。因此,在 print Tave 之后,构建一个包含颜色的列表;我使用列表理解来执行此操作:

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

然后,当您创建“事件”时,设置color=y0colors,如下所示:

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

当然,根据您使用的要求,所有点都不会是蓝色的。 y0 值要么大于 Tave(“红色”),要么大于 Tave(“黄色”)。在定义散点图“事件”之后可能有一种方法可以做到这一点,但这种方法似乎更直接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文