首页 > 解决方案 > 根据特定条件对散点图点进行不同的着色

问题描述

我有一个使用 plotly.py 制作的散点图,我想根据特定条件用不同的颜色为散点图中的某些点着色。我在下面附上了一个示例代码:

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import plot

data = [4.1, 2.1, 3.1, 4.4, 3.2, 4.1, 2.2]

trace_1 = go.Scatter(
    x = [1, 2, 3, 4, 5, 6, 7],
    y = data
)

layout = go.Layout(
    paper_bgcolor='rgb(255,255,255)',
    plot_bgcolor='rgb(229,229,229)',
    title = "Sample Plot",
    showlegend = False,
    xaxis = dict(
        mirror = True,
        showline = True,
        showticklabels = True,
        ticks = 'outside',
        gridcolor = 'rgb(255,255,255)',
    ),
    yaxis = dict(
        mirror = True,
        showline = True,
        showticklabels = False,
        gridcolor = 'rgb(255,255,255)',
    ),
    shapes = [{
            'type': 'line',
            'x0': 1,
            'y0': 4,
            'x1': len(data),
            'y1': 4,
            'name': 'First',
            'line': {
                'color': 'rgb(147, 19, 19)',
                'width': 1,
                'dash': 'longdash'
            }
        }, 
        {
            'type': 'line',
            'x0': 1,
            'y0': 3,
            'x1': len(data),
            'y1': 3,
            'line': {
                'color': 'rgb(147, 19, 19)',
                'width': 1,
                'dash': 'longdash'
            }
        }
    ]
)

fig = dict(data = [trace_1], layout = layout)
plot(fig, filename = "test_plot.html")

这是输出输出散点图

这里长的水平虚线分别具有对应的 x 值 4 和 3。可以看到,点 1、2、4、6 和 7 位于虚线之外。有没有办法根据条件 (x > 3) 和 (x<4) 对它们进行不同的着色。

这是我在寻找解决方案时发现的参考: Python Matplotlib scatter plot: Specify color points based on conditions

如何在 plotly.py 中实现这一点?

标签: plotly-dashplotly-python

解决方案


您可以通过使用数值数组来指定标记颜色来完成此操作。请参阅https://plot.ly/python/line-and-scatter/#scatter-with-a-color-dimension

调整您的特定示例以显示低于 3 的红色标记、高于 4 的绿色标记和介于 3 和 4 之间的灰色标记:

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode()

data = [4.1, 2.1, 3.1, 4.4, 3.2, 4.1, 2.2]

color = [
    -1 if v < 3 else 1 if v > 4 else 0
    for v in data
]

colorscale = [[0, 'red'], [0.5, 'gray'], [1.0, 'green']]

trace_1 = go.Scatter(
    x = [1, 2, 3, 4, 5, 6, 7],
    y = data,
    marker = {'color': color,
              'colorscale': colorscale,
              'size': 10
             }
)

layout = go.Layout(
    paper_bgcolor='rgb(255,255,255)',
    plot_bgcolor='rgb(229,229,229)',
    title = "Sample Plot",
    showlegend = False,
    xaxis = dict(
        mirror = True,
        showline = True,
        showticklabels = True,
        ticks = 'outside',
        gridcolor = 'rgb(255,255,255)',
    ),
    yaxis = dict(
        mirror = True,
        showline = True,
        showticklabels = False,
        gridcolor = 'rgb(255,255,255)',
    ),
    shapes = [{
            'type': 'line',
            'x0': 1,
            'y0': 4,
            'x1': len(data),
            'y1': 4,
            'name': 'First',
            'line': {
                'color': 'rgb(147, 19, 19)',
                'width': 1,
                'dash': 'longdash'
            }
        }, 
        {
            'type': 'line',
            'x0': 1,
            'y0': 3,
            'x1': len(data),
            'y1': 3,
            'line': {
                'color': 'rgb(147, 19, 19)',
                'width': 1,
                'dash': 'longdash'
            }
        }
    ]
)

fig = dict(data = [trace_1], layout = layout)
iplot(fig)

示例图

希望有帮助!


推荐阅读