首页 > 解决方案 > 如何停止插入情节中缺少的值?

问题描述

所以我用 plotly 创建了我的数据的面积图。我的数据有整年的时间块,我们在表中的数据只有 84 天(每月 7 天),所以就像我们没有数据的其余日子一样(不是零,它不见了)。因此,当我为这些数据绘制面积图时,在剩下的日子里,它没有为缺失值显示 0。例如: 在此处输入图像描述

您可以在图表中看到,直线实际上是数据丢失的其余日期,而聚集线是我们在表中拥有的实际数据。那么有没有办法让我们可以告诉绘图不插值缺失数据或类似的东西,或者我们可以用零代替缺失数据?我的这个图的代码是:

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

标签: pythonplotly

解决方案


推荐阅读