plotly - 如何在plotly.js图表​​中添加彩色背景栏

标签 plotly plotly.js

如何将“绘图带”或图表中的阴影区域添加到plotly-js图表中?看起来像这张图片:

enter image description here

(来自其他产品:Highcharts)

具体来说,我想将背景色添加到绘图表的某些部分(多个)中,为X指定[xFrom,xTo]坐标,并使用任意颜色绘制整个Y范围。例如,突出显示范围(例如异常)中特别重要的时间窗口。

最佳答案

此示例来自文档,但正是您要寻找的内容(突出显示时间序列图中某个范围的背景):

(来源:https://plotly.com/python/shapes/#highlighting-time-series-regions-with-rectangle-shapes)

import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=['2015-02-01', '2015-02-02', '2015-02-03', '2015-02-04', '2015-02-05',
        '2015-02-06', '2015-02-07', '2015-02-08', '2015-02-09', '2015-02-10',
        '2015-02-11', '2015-02-12', '2015-02-13', '2015-02-14', '2015-02-15',
        '2015-02-16', '2015-02-17', '2015-02-18', '2015-02-19', '2015-02-20',
        '2015-02-21', '2015-02-22', '2015-02-23', '2015-02-24', '2015-02-25',
        '2015-02-26', '2015-02-27', '2015-02-28'],
    y=[-14, -17, -8, -4, -7, -10, -12, -14, -12, -7, -11, -7, -18, -14, -14,
        -16, -13, -7, -8, -14, -8, -3, -9, -9, -4, -13, -9, -6],
    mode='lines',
    name='temperature'
)
data = [trace0]
layout = {
    # to highlight the timestamp we use shapes and create a rectangular
    'shapes': [
        # 1st highlight during Feb 4 - Feb 6
        {
            'type': 'rect',
            # x-reference is assigned to the x-values
            'xref': 'x',
            # y-reference is assigned to the plot paper [0,1]
            'yref': 'paper',
            'x0': '2015-02-04',
            'y0': 0,
            'x1': '2015-02-06',
            'y1': 1,
            'fillcolor': '#d3d3d3',
            'opacity': 0.2,
            'line': {
                'width': 0,
            }
        },
        # 2nd highlight during Feb 20 - Feb 23
        {
            'type': 'rect',
            'xref': 'x',
            'yref': 'paper',
            'x0': '2015-02-20',
            'y0': 0,
            'x1': '2015-02-22',
            'y1': 1,
            'fillcolor': '#d3d3d3',
            'opacity': 0.2,
            'line': {
                'width': 0,
            }
        }
    ]
}
py.iplot({'data': data, 'layout': layout}, filename='timestamp-highlight')

结果:https://plot.ly/create/?fid=jordanpeterson:810#/

关于plotly - 如何在plotly.js图表​​中添加彩色背景栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55035114/

相关文章:

python - 用于散布大数据集的散点图3D

python - 如何使用 plotly express 更改条形图中每个特定条形的颜色?

python - 在 Plotly 3.0 中设置图例文本颜色

javascript - 在plotly中创建散点图,无需为数据声明多个变量

javascript - 是否可以更改 Plotly Js 气泡图悬停工具提示中的文本?

javascript - Plotly.js 激活悬停文本

python - 在 Plotly Express 中使用 Pandas 索引

javascript - Plotly.js 曲面图数据格式化

非常小的数字,显示为 `n` 、 `p` 或 `f`

javascript - Plotly.js - 获取堆积条形图中每个 x 的 4 个 y 值