如何在 plotly 的单个页面中添加两个堆叠区域图?

How to add two stacked area plots in a single page in plotly?

所以我为两种情况创建了堆积面积图:

  1. 有存储空间
  2. 无存储 他们看起来都是这样的:

带存储空间

无存储

但是它们都在不同的页面上,我希望它们合并。我写这些图的代码是:

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

#Getting the Data
app = xw.App(visible=False)
try:
    wb = app.books.open('PCM Fuel Wise Chart.xlsx')
    sheet = wb.sheets[0]
    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
    time = sheet.range('O2:O8065').value

    sheet = wb.sheets[1]
    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
    time = sheet.range('O2:O8065').value
except Exception as e:
    logging.exception("Something awful happened!")
    print(e)
finally:
    app.quit()
    app.kill()

#Plotting the Data
trace1 = []
trace1.append({'x': dateWithStorage,'y': coalWithoutStorage, 'name': 'Coal', 'mode':'lines', 'line' : dict(width = 0.5, color = 'grey'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': nuclearWithStorage, 'name': 'Nuclear', 'mode':'lines', 'line' : dict(width = 0.5, color = 'red'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': gasWithStorage, 'name': 'Gas', 'mode':'lines', 'line' : dict(width = 0.5, color = 'blue'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': bagasseWithStorage, 'name': 'Bagasse', 'mode':'lines', 'line' : dict(width = 0.5, color = 'purple'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': hydroWithStorage, 'name': 'Hydro', 'mode':'lines', 'line' : dict(width = 0.5, color = 'cyan'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': windWithStorage, 'name': 'Wind', 'mode':'lines', 'line' : dict(width = 0.5, color = 'green'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': solarWithStorage, 'name': 'Solar', 'mode':'lines', 'line' : dict(width = 0.5, color = 'yellow'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': bessCharging, 'name': 'BESS Charging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'brown'), 'stackgroup': 'one'})
trace1.append({'x': dateWithStorage,'y': bessDischarging, 'name': 'BESS Discharging', 'mode':'lines', 'line' : dict(width = 0.5, color = 'orange'), 'stackgroup': 'one'})

fig = go.Figure({'data': trace1})
pyo.plot(fig, filename= 'testing.html')

我不知道如何将子图和堆叠区域图一起添加,所以有人可以帮忙吗? 我已经使用这段代码创建了图表,我只需要一种方法将这两个图表整合为一个。

您可以创建一个包含两行的子图,然后绘制该子图。

这可能是一个最小的例子

import plotly.graph_objs as go
import plotly.offline as pyo
from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=1)

# traces WithStorage
trace1 = go.Scatter({'x': [3,3.1],'y': [1,1.1], 'name': 'Coal', 'mode':'lines', 'line' : dict(width = 0.5, color = 'grey'), 'stackgroup': 'one'})
trace2 = go.Scatter({'x': [4,4.2],'y': [2,2.1], 'name': 'Nuclear', 'mode':'lines', 'line' : dict(width = 0.5, color = 'red'), 'stackgroup': 'one'})

#traces WithoutStorage
trace3 = go.Scatter({'x': [5,5.1],'y': [2,2.1], 'name': 'Coal', 'mode':'lines', 'line' : dict(width = 0.5, color = 'grey'), 'stackgroup': 'one'})
trace4  = go.Scatter({'x': [6, 6.1],'y': [3,3.1], 'name': 'Nuclear', 'mode':'lines', 'line' : dict(width = 0.5, color = 'red'), 'stackgroup': 'one'})


fig = make_subplots(rows=2, cols=1)

# we add each trace to their subplot
fig.add_trace(trace1,1,1)
fig.add_trace(trace2,1,1)

fig.add_trace(trace3,2,1)
fig.add_trace(trace4,2,1)

fig.update_layout(height=600, width=600, title_text="Stacked Subplots")
pyo.plot(fig, filename= 'testing.html')