简体   繁体   English

Plotly:是否可以对折线图中的特定部分进行着色?

[英]Plotly: Is it possible to color specific portion of the line in line chart?

Sample line chart示例折线图

import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x='date', y="GOOG")
fig.show()

在此处输入图像描述

I want to color the values > 1.1 and values<0.95 to a different color(ex: red)我想将values > 1.1values<0.95着色为不同的颜色(例如:红色)

在此处输入图像描述

Is it possible in plotly to color the specific portion of the line into a different color?在 plotly 中是否可以将线条的特定部分着色为不同的颜色?

  • you can add a additional traces that are data points outside desired range您可以添加额外的迹线,这些迹线是超出所需范围的数据点
  • not pandas techniques to ensure that all data points exist for additional trace (use of outer-join to get NaN for points within range)不是pandas技术,以确保所有数据点都存在以进行额外跟踪(使用外连接获取范围内点的NaN
import plotly.express as px

df = px.data.stocks()
fig = px.line(df, x="date", y="GOOG").add_traces(
    px.line(
        df.loc[~df["GOOG"].between(0.95, 1.1)].merge(
            df, on="date", how="right", suffixes=("", "_r")
        ),
        x="date",
        y="GOOG",
    )
    .update_traces(line_color="red")
    .data
)

fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM