簡體   English   中英

存在數據空白時如何在 Plotly 中創建正確填充的線條

[英]How to create properly filled lines in Plotly when there are data gaps

基於https://plot.ly/python/line-charts/#filled-lines ,可以運行下面的代碼

import plotly.graph_objects as go

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x_rev = x[::-1]
y = [5, 2.5, 5, 7.5, 5, 2.5, 7.5, 4.5, 5.5, 5]
y_upper = [5.5, 3, 5.5, 8, 6, 3, 8, 5, 6, 5.5]
y_lower = [4.5, 2, 4.4, 7, 4, 2, 7, 4, 5, 4.75]
y_lower_rev = y_lower[::-1]

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x, y=y,
    line_color='rgb(0,176,246)',
    name='Mid line',
))
fig.add_trace(go.Scatter(
    x=x+x_rev,
    y=y_upper+y_lower_rev,
    fill='toself',
    fillcolor='rgba(0,176,246,0.2)',
    line_color='rgba(255,255,255,0)',
    name='Filled lines working properly',
))
fig.update_traces(mode='lines')
fig.show()

並成功得到下面的情節在此處輸入圖片說明

但是,如果存在數據間隙,填充的部分似乎無法正常工作(例如,第一個和第二個連接的組件),至少在下面嘗試的代碼中是這樣。

成功擁有數據空白和填充行的正確方法/代碼是什么?

x_for_gaps_example = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
x_for_gaps_example_rev = x_for_gaps_example[::-1]
y_with_gaps =[5, 15, None, 10, 5, 0, 10, None, 15, 5, 5, 10, 20, 15, 5]
y_upper_with_gaps = [i+1 if i is not None else None for i in y_with_gaps]
y_lower_with_gaps = [i-2 if i is not None else None for i in y_with_gaps][::-1]
fig = go.Figure()
fig.add_trace(go.Scatter(
    x=x_for_gaps_example,
    y=y_with_gaps,
    name='Mid Line with <b>Gaps</b>'
))

fig.add_trace(go.Scatter(
    x=x_for_gaps_example+x_for_gaps_example_rev,
    y=y_upper_with_gaps+y_lower_with_gaps,
    fill='toself',
    fillcolor='rgba(0,176,246,0.2)',
    line_color='rgba(255,255,255,0)',
    name='Filled Lines not working properly with <b>gaps</b>'
))

fig.show()

在此處輸入圖片說明

這似乎是一個相當古老的情節錯誤:

參考:

https://github.com/plotly/plotly.js/issues/1132

和:

https://community.plot.ly/t/scatter-line-plot-fill-option-fills-gaps/21264

一種解決方案可能是將整個填充軌跡分解為多個部分並將它們添加到圖中。 但是,這可能有點復雜,因為它需要不同的計算來確定該填充區域的位置。

您實際上可以通過將 connectgaps 屬性設置為 true 來稍微改進您的圖表,這將導致:

在此處輸入圖片說明

但是,這看起來有點奇怪;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM