Python 如何根据Plotly中数据框中的另一列为标记着色?

Python 如何根据Plotly中数据框中的另一列为标记着色?,python,python-3.x,plot,graph,plotly,Python,Python 3.x,Plot,Graph,Plotly,我有一个数据框,如下所示,有3列。我使用“束”作为x值,使用“统一大小”作为y值,以形成散点图。但是我想根据第三列类为各个点着色。类值2为绿色,4为蓝色的点 以数据帧中的第一个点和最后一个点为例。第一个点的x值为5,y值为1,颜色为绿色,而最后一个点的x值为4,y值为8,颜色为蓝色 我尝试使用如图所示的if语句,但出现语法错误。有什么办法吗 fig = go.Figure() fig.update_layout(width = 400, height = 400, template = 'p

我有一个数据框,如下所示,有3列。我使用“束”作为x值,使用“统一大小”作为y值,以形成散点图。但是我想根据第三列类为各个点着色。类值2为绿色,4为蓝色的点

以数据帧中的第一个点和最后一个点为例。第一个点的x值为5,y值为1,颜色为绿色,而最后一个点的x值为4,y值为8,颜色为蓝色

我尝试使用如图所示的if语句,但出现语法错误。有什么办法吗

 fig = go.Figure()
 fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
 fig.add_trace(go.Scatter(x = data.Clump,
                          y = data.UnifSize,
                          mode = 'markers',
                          if data.Class == 2:
                              marker = duct(
                              color = 'green'
                              ) 
                          if data.Class == 4:
                             marker = dict(
                             color = 'yellow'
                             )
                     )))

例如,您可以执行以下操作:

创建示例
x
y
数据,使用包含颜色所依赖条件的数组:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]
具有与条件数组中的
0
相对应的索引的
x
y
点为:

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==0]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==0]
如果我们希望x和y数组中的元素具有与条件数组中的a
1
对应的索引,我们只需将
0
更改为
1

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==1]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==1]
或者,您可以使用
zip

[eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]
其他人也是如此

这是带条件的列表理解,这里有很好的解释:

然后用2个
go.Scatter
调用绘制2对数组

整个事情:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]

import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==0],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==0],
                        mode = 'markers',marker = dict(color = 'green')))
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==1],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==1],
                        mode = 'markers',marker = dict(color = 'yellow')))
fig.show()
这将为您提供:

我相信这就是我们想要的



要从
DataFrame
列转换到
list
,建议这样做:。

您可以这样做,例如:

创建示例
x
y
数据,使用包含颜色所依赖条件的数组:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]
具有与条件数组中的
0
相对应的索引的
x
y
点为:

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==0]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==0]
如果我们希望x和y数组中的元素具有与条件数组中的a
1
对应的索引,我们只需将
0
更改为
1

[eachx for indexx, eachx in enumerate(x) if condition[indexx]==1]
[eachy for indexy, eachy in enumerate(y) if condition[indexy]==1]
或者,您可以使用
zip

[eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]
其他人也是如此

这是带条件的列表理解,这里有很好的解释:

然后用2个
go.Scatter
调用绘制2对数组

整个事情:

import numpy as np
x = [x for x in range(100)]
y = [3*each*np.random.normal(loc=1.0, scale=0.1) for each in range(100)]
condition = [np.random.randint(0,2) for x in range(100)]

import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(width = 400, height = 400, template = 'plotly_white',xaxis_title = 'clump', yaxis_title = 'Unif Size')
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==0],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==0],
                        mode = 'markers',marker = dict(color = 'green')))
fig.add_trace(go.Scatter(x = [eachx for indexx, eachx in enumerate(x) if condition[indexx]==1],
                        y = [eachy for indexy, eachy in enumerate(y) if condition[indexy]==1],
                        mode = 'markers',marker = dict(color = 'yellow')))
fig.show()
这将为您提供:

我相信这就是我们想要的



对于从
DataFrame
列转换到
list
,建议这样:。

我认为
[eachx For indexx,eachx in enumerate(x)if condition[indexx]==0]
可以替换为
[eachx For eachx,eachcondition in zip(x,condition),if eachcondition==0]
现在包括它。我认为
如果条件[indexx]==0,则枚举(x)中的[eachx代表indexx,eachx中的[eachx]
可以替换为
[eachx代表eachx,如果eachcondition==0,则zip中的eachcondition(x,条件)]
现在包含它。