[英]How to color markers based on another column in the dataframe in Plotly?
I have a dataframe as shown below with 3 columns.我有一个 dataframe 如下所示,有 3 列。 I am using clump as my x values and Unif size as my y values to form a scatterplot.我使用 clump 作为我的 x 值和 Unif 大小作为我的 y 值来形成散点图。 But I want to color the individual points based on the third column class.但我想根据第三列 class 为各个点着色。 Points having class values 2 as green and 4 as blue. class 值 2 为绿色和 4 为蓝色的点。
So taking the first and last points in the dataframe as examples.所以以dataframe中的第一个和最后一个点为例。 The first point will have an x-value of 5, y-value of 1 with color green, while the last point will have an x-value of 4, y-value of 8 and color blue第一个点的 x 值为 5,y 值为 1,颜色为绿色,而最后一个点的 x 值为 4,y 值为 8,颜色为蓝色
I tried using if statement as shown, but I get syntax errors.如图所示,我尝试使用 if 语句,但出现语法错误。 Any ideas on how to do this?关于如何做到这一点的任何想法?
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'
)
)))
You can do for example this:例如,您可以这样做:
Create example x
and y
data, with an array containing the condition on which the color will depend:创建示例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)]
The x
and y
points which have an index which corresponds to a 0
in the condition array are:在条件数组中具有对应于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]
If we want the elements in the x and y arrays which have an index corresponding to a 1
in the condition array we just change the 0
to 1
:如果我们想要 x 和 y arrays 中的元素在条件数组中具有对应于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]
Alternatively, you could use zip
:或者,您可以使用zip
:
[eachx for eachx, eachcondition in zip(x, condition) if eachcondition==0]
And so on for the others.以此类推。
This is list comprehension with a condition, well explained here: https://stackoverflow.com/a/4260304/8565438 .这是一个有条件的列表理解,这里有很好的解释: https://stackoverflow.com/a/4260304/8565438 。
Then plot the 2 pair of arrays with 2 go.Scatter
calls.然后 plot 2 对 arrays 与 2 go.Scatter
调用。
The whole thing together:整件事在一起:
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()
This will give you:这会给你:
Which is what we wanted I believe.我相信这就是我们想要的。
For converting to list
from DataFrame
column, recommend this: get list from pandas dataframe column .要从DataFrame
列转换为list
,建议这样做: 从 pandas dataframe 列获取列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.