首页 文章

根据数字阴影背景的特定部分

提问于
浏览
0

我需要创建一个双图 . 基本上,一个图表有一个“正常”的图 - 它是长颈鹿随着时间推移的价格 . 另一个情节是根据我当时拥有的长颈鹿的数量 .

另一个图需要通过相应地着色图的背景来表示 .

enter image description here
如何创建阴影图形?

谢谢!

1 回答

  • 0

    您可以在布局设置中使用Plotly的shape .

    enter image description here

    import plotly
    import time
    import random
    import datetime
    
    #create some random input data
    N = 100
    
    starttime = time.time()
    x = [datetime.date.fromtimestamp(starttime - 60*60*24*N).strftime('%Y-%m-%d')]
    y = [10]
    for i in range(N):
        x.append(datetime.date.fromtimestamp(starttime - 60*60*24*N + i*60*60*24).strftime('%Y-%m-%d'))
        y.append(abs(y[-1] + random.randint(-7, 5 + int(N/25))))
    
    trace = plotly.graph_objs.Scatter(
        x = x,
        y = y,
        mode='line',
    )
    
    layout = dict(shapes = list())
    #highlight the minimum in red
    layout['shapes'].append(
        {
            'type': 'rect',
            'xref': 'x',
            'yref': 'paper',
            'x0': trace.x[trace.y.index(min(trace.y)) - 5],
            'y0': 0,
            'x1': trace.x[trace.y.index(min(trace.y)) + 5],
            'y1': max(trace.y),
            'fillcolor': '#ff0000',
            'opacity': 0.2,
            'line': {
                'width': 0,
            }
        }
    )
    
    #highlight the maximum in green
    layout['shapes'].append(
        {
            'type': 'rect',
            'xref': 'x',
            'yref': 'paper',
            'x0': trace.x[trace.y.index(max(trace.y)) - 5],
            'y0': 0,
            'x1': trace.x[(trace.y.index(max(trace.y)) + 5) - ((trace.y.index(max(trace.y)) + 6) % len(trace.y))],
            'y1': max(trace.y),
            'fillcolor': '#00ff00',
            'opacity': 0.2,
            'line': {
                'width': 0,
            }
        }
    )
    
    fig = dict(data=[trace], layout=layout)
    plotly.plotly.sign_in('user', 'token')
    plot_url = plotly.plotly.plot(fig)
    

相关问题