簡體   English   中英

如何停止插入 plotly 中缺失的值?

[英]how to stop interpolating the values that are missing in plotly?

所以我在 plotly 中創建了我的數據區域 plot。我的數據有整年的時間塊,我們在表中的數據只有 84 天(每月 7 天),所以它就像 rest 的我們沒有數據的日子(不是零,它丟失了)。 因此,當我 plot 時,此數據的面積圖在 rest 天的缺失值中不顯示 0。 例如: 在此處輸入圖像描述

您可以在圖中看到,直線實際上是數據缺失日期的 rest,聚集的線是我們在表中擁有的實際數據。 那么有沒有一種方法可以告訴 plotly 不要插入缺失數據的值或類似的東西,或者我們可以用零代替缺失數據? 我對該圖的代碼是:

import pandas as pd
import plotly.graph_objs as go
import plotly.offline as pyo
from plotly.subplots import make_subplots
import xlwings as xw
import logging
import glob


year = 2025
case = 'MNRE'
path1 = glob.glob("detailed output/"+str(year)+"/"+str(case)+"/with storage/[a-z]*.xlsx")
path2 = glob.glob("detailed output/"+str(year)+"/"+str(case)+"/without storage/[a-z]*.xlsx")

#Reading the data
app = xw.App(visible=False)
try:
    wb1 = app.books.open(path1[0])
    sheet = wb1.sheets['Fuel Wise Chart']
    dateWithStorage = sheet.range('A2:A8065').value
    coalWithStorage = sheet.range('D2:D8065').value
    nuclearWithStorage = sheet.range('E2:E8065').value
    gasWithStorage = sheet.range('F2:F8065').value
    bagasseWithStorage = sheet.range('G2:G8065').value
    hydroWithStorage = sheet.range('H2:H8065').value
    windWithStorage = sheet.range('I2:I8065').value
    solarWithStorage = sheet.range('J2:J8065').value
    bessCharging = sheet.range('L2:L8065').value
    bessDischarging = sheet.range('M2:M8065').value

    importSheet = wb1.sheets['Fuelwise Dispatch']
    importWithStorage = importSheet.range('CF2:CF8065').value

    wb2 = app.books.open(path2[0])
    sheet = wb2.sheets['Fuel Wise Chart']
    dateWithoutStorage = sheet.range('A2:A8065').value
    coalWithoutStorage = sheet.range('D2:D8065').value
    nuclearWithoutStorage = sheet.range('E2:E8065').value
    gasWithoutStorage = sheet.range('F2:F8065').value
    bagasseWithoutStorage = sheet.range('G2:G8065').value
    hydroWithoutStorage = sheet.range('H2:H8065').value
    windWithoutStorage = sheet.range('I2:I8065').value
    solarWithoutStorage = sheet.range('J2:J8065').value

    importSheet = wb2.sheets['Fuelwise Dispatch']
    importWithoutStorage = importSheet.range('CF2:CF8065').value

except Exception as e:
    logging.exception("Something awful happened!")
    print(e)
finally:
    app.quit()
    app.kill()
#Creating the area plot for with and without storage cases
fig = make_subplots(rows=2, cols=1, subplot_titles=("<b>Without Storage(With min run constraints relaxed for old plants)</b>", "<b>With Storage</b>"), shared_xaxes=True,
                    vertical_spacing = 0.06, y_title='<b>Fuel Wise Dispatch</b>')

trace1 = go.Scatter({'x': dateWithStorage,'y': coalWithStorage, 'name': 'Coal', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(71, 69, 69)'), 'stackgroup': 'one'})
trace2 = go.Scatter({'x': dateWithStorage,'y': nuclearWithStorage, 'name': 'Nuclear', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(255, 0, 0)'), 'stackgroup': 'one'})
trace3 = go.Scatter({'x': dateWithStorage,'y': gasWithStorage, 'name': 'Gas', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(0, 235, 227)'), 'stackgroup': 'one'})
trace4 = go.Scatter({'x': dateWithStorage,'y': bagasseWithStorage, 'name': 'Bagasse', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(154, 3, 255)'), 'stackgroup': 'one'})
trace5 = go.Scatter({'x': dateWithStorage,'y': hydroWithStorage, 'name': 'Hydro', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(0, 145, 255)'), 'stackgroup': 'one'})
trace6 = go.Scatter({'x': dateWithStorage,'y': windWithStorage, 'name': 'Wind', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(5, 252, 14)'), 'stackgroup': 'one'})
trace7 = go.Scatter({'x': dateWithStorage,'y': solarWithStorage, 'name': 'Solar', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(246, 255, 0)'), 'stackgroup': 'one'})
trace8 = go.Scatter({'x': dateWithStorage,'y': bessCharging, 'name': 'BESS Charging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(145, 77, 4)'), 'stackgroup': 'one'})
trace9 = go.Scatter({'x': dateWithStorage,'y': bessDischarging, 'name': 'BESS Discharging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(21, 102, 24)'), 'stackgroup': 'one'})
trace10 = go.Scatter({'x': dateWithStorage,'y': importWithStorage, 'name': 'Import', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(255, 161, 220)'), 'stackgroup': 'one'})

trace11 = go.Scatter({'x': dateWithStorage,'y': coalWithoutStorage, 'name': 'Coal', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(71, 69, 69)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace12 = go.Scatter({'x': dateWithStorage,'y': nuclearWithoutStorage, 'name': 'Nuclear', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(255, 0, 0)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace13 = go.Scatter({'x': dateWithStorage,'y': gasWithoutStorage, 'name': 'Gas','mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(0, 235, 227)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace14 = go.Scatter({'x': dateWithStorage,'y': bagasseWithoutStorage, 'name': 'Bagasse','mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(154, 3, 255)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace15 = go.Scatter({'x': dateWithStorage,'y': hydroWithoutStorage, 'name': 'Hydro','mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(0, 145, 255)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace16 = go.Scatter({'x': dateWithStorage,'y': windWithoutStorage, 'name': 'wind','mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(5, 252, 14)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace17 = go.Scatter({'x': dateWithStorage,'y': solarWithoutStorage, 'name': 'solar','mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(246, 255, 0)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
trace18 = go.Scatter({'x': dateWithStorage,'y': importWithoutStorage, 'name': 'Import', 'mode':'lines', 'line' : dict(width = 0.5, color = 'rgb(255, 161, 220)'), 'stackgroup': 'one', 'xaxis': 'x', 'showlegend': False})
# trace2 = go.Scatter({'x': dateWithStorage,'y': bessCharging, 'name': 'BESS Charging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'brown'), 'stackgroup': 'one'})
# trace2 = go.Scatter({'x': dateWithStorage,'y': bessDischarging, 'name': 'BESS Discharging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'orange'), 'stackgroup': 'one'})

fig.add_trace(trace1, 2, 1)
fig.add_trace(trace2, 2, 1)
fig.add_trace(trace3, 2, 1)
fig.add_trace(trace4, 2, 1)
fig.add_trace(trace5, 2, 1)
fig.add_trace(trace6, 2, 1)
fig.add_trace(trace7, 2, 1)
fig.add_trace(trace8, 2, 1)
fig.add_trace(trace9, 2, 1)
fig.add_trace(trace10, 2, 1)

fig.add_trace(trace11, 1, 1)
fig.add_trace(trace12, 1, 1)
fig.add_trace(trace13, 1, 1)
fig.add_trace(trace14, 1, 1)
fig.add_trace(trace15, 1, 1)
fig.add_trace(trace16, 1, 1)
fig.add_trace(trace17, 1, 1)
fig.add_trace(trace18, 1, 1)

fig.update_layout(title_text="<b>"+str(year)+' '+str(case)+" Scenario</b>",legend = dict(
    font=dict(
        family="Arial",
        size = 9
    )
))

# fig = go.Figure({'data': trace2}) #, 'layout': layout})
# fig.show()

pyo.plot(fig, filename= str(year)+' '+str(case)+' Plot with updated start up cost.html')

我知道這個問題很老但是因為它是尋找主題時 duckduckgo 上的第一個答案我認為值得添加一個答案。 stackgaps 的行為在相應的選項中定義。 描述如下:

stackgaps – 僅在使用堆棧組時相關,並且僅使用在堆棧組中找到的第一個堆棧間隙 - 包括 visible 是否為“legendonly”,但如果為 false 則不包括在內。 確定我們如何處理該組中其他軌跡有數據但此軌跡沒有數據的位置。 使用推斷零,我們在這些位置插入一個零。 使用“插值”,我們在現有值之間進行線性插值,並推斷出超出現有值的常數。

https://plotly.com/python-api-reference/generated/plotly.graph_objects.Scatter.html

如果您設置stackgaps="interpolate" ,您的 plot 應該看起來像預期的那樣。

暫無
暫無

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

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