Plotly.py: fill between lines, different color por positive/negative

为君一笑 提交于 2019-12-23 04:38:14

问题


With Plotly, I can easily plot two lines and fill the area between them:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4],
    y=[-1, -.2, 1, 2],
    fill=None,
    mode='lines',
))
fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4],
    y=[-1, -.5, 0.2, .5],
    fill='tonexty',
    mode='lines',
))
fig.update_layout(title_text='hello world')
fig.show()

How can I make it to separate the filled area in two? In particular, filling with red where y < 0 and with green where y > 0. While maintaining the fill only between the lines.

I would like to maintain the line colors and legend. That means, I am not interested in just drawing two separate filled pollygons.

Note that the lines do not necessarily have values at y == 0.

来源:https://stackoverflow.com/questions/57421334/plotly-py-fill-between-lines-different-color-por-positive-negative

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!