取决于X轴的图形上的颜色编码点

时间:2019-02-02 21:45:31

标签: python plotly

我有一个图。本质上,如果y轴值是给定列表中的任何值,我想做的就是更改图形上每个点的颜色。

例如:

list = ["ACACT", "TATTC", "CGATT"]

如果这是我的图形,我想将与列表中任何值相对应的所有点设为红色。

Plotly Graph

我当前的图形代码为:

trace = go.scatter(
      x = x1,
      y = x2,
      mode = 'markers'
)
data = [trace]
py.iplot(data, filename='basic-scatter')

我知道你可以使用编辑标记

marker = dict()

但你可以为特定的点不喜欢它,我想干什么?

1 个答案:

答案 0 :(得分:0)

您可以将颜色列表传递给marker中的color,例如

go.Scatter(x=x
           y=y,
           mode='markers',
           marker={'color': colors})

在下面的示例中,所有点均使用默认颜色,但是有些颜色基于dict的不同颜色,import random import plotly plotly.offline.init_notebook_mode() # generate some random x and y-values n_points = 10 y = [] for i in range(4): _y = 'AT' + 'ATGC'[i] y.extend([_y + nuc for nuc in 'ATGC']) x = [random.random() for _ in range(n_points * len(y))] y = y * n_points # the default color default_color = 'rgb(0,0,255)' # define our special colors special_colors = {'ATAT': 'rgb(255, 0, 0)', 'ATTA': 'rgb(0, 255, 0)'} # build a list of custom colors colors = [] for i, y_value in enumerate(y): colors.append(special_colors.get(y_value, default_color)) fig = plotly.graph_objs.Figure() fig.add_scatter(x=x, y=y, mode='markers', marker={'color': colors}) plotly.offline.iplot(fig) 使用y轴值作为键并使用颜色作为值。

enter image description here

{{1}}