Plotly Dash - 渐变线

问题描述 投票:0回答:1

是否可以使用Plotly Dash来创建线条图,其中的线条颜色是渐变的(纯粹是为了美观)?

我试过使用类似

'line': {'color': 'linear-gradient(90deg, red, red 60%, white)' }

plotly dassh中整个图形代码的例子。

    dcc.Graph(
        id='MORTGAGE_RATES',
        figure={
            'data': [
                 { "x": MORTGAGE30US['date'],"y": MORTGAGE30US['value'],"mode": "lines","name": '30 YR', 'line': {'color': 'linear-gradient(90deg, red, red 60%, white)' }},
                 { "x": MORTGAGE15US['date'],"y": MORTGAGE15US['value'],"mode": "lines","name": '15 YR',},


            ],
            'layout': {
                'title': 'MORTGAGE RATES',
                "paper_bgcolor": "rgb(46, 54, 65)",
                "plot_bgcolor": "rgb(46, 54, 65)",
                'font': {
                    'color': "rgb(255,255,255)"
                }
            }
        }
    )

python plotly plotly-dash plotly.js plotly-python
1个回答
3
投票

这个功能还不能用于二维线图,目前只适用于三维线图,见 https:/github.comotlyplotly.jsissues581。. 然而,如果你使用标记而不是线条,也可以在二维图中使用色标,见下面的例子。

import plotly.graph_objects as go
import numpy as np

t = np.linspace(0, 10, 1000)
x, y = t, np.cos(t)

data = go.Scatter(x=x, y=y, mode='markers', marker={'color': x, 'colorscale': 'Rainbow', 'size': 10})

layout = dict(plot_bgcolor='white', margin=dict(t=0, b=0, r=0, l=0, pad=0),
              xaxis=dict(showgrid=False, zeroline=False, mirror=True, linecolor='gray'),
              yaxis=dict(showgrid=False, zeroline=False, mirror=True, linecolor='gray'))

fig = go.Figure(data=data, layout=layout)

fig.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.