根据特定条件对散点图点进行不同着色

Coloring scatter plot points differently based on certain conditions

我有一个使用 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")

这是输出 Output Scatter plot

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

这是我在搜索解决方案时发现的一些参考资料: Python Matplotlib scatter plot: Specify color points depending on conditions

我怎样才能在 plotly.py 中做到这一点?

您可以通过使用数值数组指定标记颜色来完成此操作。参见 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)

希望对您有所帮助!