Plotly Dash - 渐变线 [英] Plotly Dash - Gradient Lines

查看:54
本文介绍了Plotly Dash - 渐变线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我尝试使用类似

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

plotly dash 中的整个图形代码示例

 dcc.Graph(id='抵押贷款利率',数字={'数据': [{ "x": MORTGAGE30US['date'],"y": MORTGAGE30US['value'],"mode": "lines","name": '30 YR', 'line': {'color': '线性渐变(90 度,红色,红色 60%,白色)' }},{ "x": MORTGAGE15US['date'],"y": MORTGAGE15US['value'],"mode": "lines","name": '15 YR',},],'布局': {'title': '抵押贷款利率',"paper_bgcolor": "rgb(46, 54, 65)","plot_bgcolor": "rgb(46, 54, 65)",'字体':{'颜色':RGB(255,255,255)"}}})

解决方案

此功能尚不适用于 2D 线图,目前仅适用于 3D 线图,请参阅

Is it possible using Plotly Dash to create line graphs in which the line colors are gradient (purely for aesthetics) ?

I tried using something like

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

example of entire graph code in 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)"
                }
            }
        }
    )

解决方案

This feature is not yet available for 2D line plots, it is currently only available for 3D line plots, see https://github.com/plotly/plotly.js/issues/581. It is however possible to use a colorscale in a 2D plot if you use markers instead of lines, see the example below.

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()

这篇关于Plotly Dash - 渐变线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆