'how to stop interpolating the values that are missing in plotly?
So I created the area plot of my data in plotly. My data has the whole year of the time block and the data which we have in the table is only for 84 days( 7 days per month), so it's like for the rest of the days we don't have the data(not zero, it's missing). So when I plot the area graph for this data its not showing 0 for the missing values in the rest of the days. for example:
You can see in the graph that the straight lines are actually the rest of the days where the data is missing and the clustered lines are the actual data that we have in the table. So is there a way so that we can tell the plotly that don't interpolate the values for the missing data or something like that or may be we can put zero in place of missing data? My code for this graph is:
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')
Solution 1:[1]
I know this question is old but since it is the first answer on duckduckgo when looking for the topic I think it is worth adding an answer. the behaviour of stackgaps is defined in the respective option. It is described as follows:
stackgaps – Only relevant when stackgroup is used, and only the first stackgaps found in the stackgroup will be used - including if visible is “legendonly” but not if it is false. Determines how we handle locations at which other traces in this group have data but this one does not. With infer zero we insert a zero at these locations. With “interpolate” we linearly interpolate between existing values, and extrapolate a constant beyond the existing values.
https://plotly.com/python-api-reference/generated/plotly.graph_objects.Scatter.html
If you set stackgaps="interpolate"
your plot should look as expected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user12669405 |