简体   繁体   English

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

[英]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:创建示例xy数据,其中包含颜色所依赖的条件的数组:

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的索引的xy点是:

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

相关问题 如何根据另一列设置 pandas dataframe 背景颜色 - How to set pandas dataframe background color based on another column 如何基于另一列对数据框列进行切片 - How to slice a dataframe column based on another column 使用 plotly python 为标记添加颜色 - add color to markers with plotly python 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame 如何根据 PySpark 中的另一个数据框列处理数据框列? - How to process a dataframe column based on another dataframe column in PySpark? 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 如何基于另一个数据框设置列值 - How to set column values based on another dataframe 如何基于另一列将 append 数据转换为 dataframe? - How to append data to dataframe based on another column? 如何在 plotly (python) 中根据我的数据集中的“颜色”列更改散点图点的颜色? - How can I change the color of my scatter plot points based on a "color" column in my dataset in plotly (python)? 带有基于另一列的标记的熊猫线图 - Pandas line plot with markers based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM