【问题标题】:Coloring scatter plot points differently based on certain conditions根据特定条件对散点图点进行不同的着色
【发布时间】:2019-05-24 09:35:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: plotly-dash plotly-python


    【解决方案1】:

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

    希望有帮助!

    【讨论】:

    • 非常感谢 Jon 的回答,这正是我想要的。
    • 太棒了!你介意在那种情况下接受答案吗?
    • 也可以根据条件标注这些点吗?
    • 很抱歉不接受这个答案。我已经广泛使用 StackOverflow,但这是我第一次提出问题。
    • 你想到了什么样的注解?您可以添加一个颜色条 (plot.ly/python/colorscales)。
    猜你喜欢
    • 1970-01-01
    • 2013-06-16
    • 2012-08-06
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多