我已经在下面编写了嵌套循环来成功生成 21 个图表(例如,每个国家一个图表德国天然气 奥地利天然气公司)
dfs 是一个字典,以 21 个国家名称为键,以各自的储气 dfs 作为值
for country in list(dfs_storage.keys()):
df_country=dfs_storage[country]
month = list(set(df_country['month']))
fig = go.Figure()
for year in set(df_country['year']):
workingGasVolume_peryear=df_country.loc[df_country['year']==year,'workingGasVolume']
gasInStorage_peryear=df_country.loc[df_country['year']==year,'gasInStorage']
# Create and style traces
fig.add_trace(go.Scatter(x=month, y=workingGasVolume_peryear, name=f'workingGasVolume{year}',
line=dict(width=4,dash='dash')))
fig.add_trace(go.Scatter(x=month, y=gasInStorage_peryear, name=f'gasInStorage{year}',
line = dict(width=4)))
# Edit the layout
fig.update_layout(title=f'{country} workingGasVolume gasInStorage',
xaxis_title='Month',
yaxis_title='Gas Volume')
offline.plot({'data':fig},filename=f'{country} gas storage.html',auto_open=False)
现在我被要求将这 21 个图表放入一个 HTML 文件中,而不更改每个图表,例如它们可以一个接一个地垂直显示
我用下面的代码尝试了 Plotly 的“子图”,并修改了几次,但从未得到所需的图表,我得到了一个无用的图表,我看不到任何值。任何人都可以帮助我吗?谢谢
countries=[]
for country in list(dfs_storage.keys()):
countries.append(country)
fig = make_subplots(
rows=len(list(dfs_storage.keys())),cols=1,
subplot_titles=(countries))
for country in countries:
df_country=dfs_storage[country]
month = list(set(df_country['month']))
for year in set(df_country['year']):
workingGasVolume_peryear=df_country.loc[df_country['year']==year,'workingGasVolume']
gasInStorage_peryear=df_country.loc[df_country['year']==year,'gasInStorage']
# Create and style traces
fig.add_trace(go.Scatter(x=month, y=workingGasVolume_peryear, name=f'workingGasVolume{year}',
line=dict(width=4,dash='dash')))
fig.add_trace(go.Scatter(x=month, y=gasInStorage_peryear, name=f'gasInStorage{year}',
line = dict(width=4)))
# Edit the layout
# fig.update_layout(title='workingGasVolume gasInStorage',
# xaxis_title='Month',
# yaxis_title='Gas Volume')
offline.plot({'data':fig},filename='gas storage.html',auto_open=False)
6 月 7 日编辑:根据 jayveesea 的建议,我在 add_trace 下添加了 row 和 col 参数,代码如下,但仍然有 Traceback:
countries=[]
for country in list(dfs_storage.keys()):
countries.append(country)
fig = make_subplots(
rows=len(list(dfs_storage.keys())),cols=1,
subplot_titles=(countries))
for i in range(len(countries)):
country=countries[i]
df_country=dfs_storage[country]
month = list(set(df_country['month']))
for year in set(df_country['year']):
workingGasVolume_peryear=df_country.loc[df_country['year']==year,'workingGasVolume']
gasInStorage_peryear=df_country.loc[df_country['year']==year,'gasInStorage']
# Create and style traces
fig.add_trace(go.Scatter(x=month, y=workingGasVolume_peryear, name=f'workingGasVolume{year}',row=i,col=1,
line=dict(width=4,dash='dash')))
fig.add_trace(go.Scatter(x=month, y=gasInStorage_peryear, name=f'gasInStorage{year}',row=i,col=1,
line = dict(width=4)))
# Edit the layout
# fig.update_layout(title='workingGasVolume gasInStorage',
# xaxis_title='Month',
# yaxis_title='Gas Volume')
offline.plot({'data':fig},filename='gas storage.html',auto_open=False)
print('the Plotly charts are saved in the same folder as the Python code')
6月8日编辑:
这是我现在运行的代码,从@jayveesea的答案复制而来,仅修改了 df 的名称
countries=[]
for country in list(dfs_storage.keys()):
countries.append(country)
# STEP 1
fig = make_subplots(
rows=len(countries), cols=1,
subplot_titles=(countries))
for i, country in enumerate(countries): #enumerate here to get access to i
years = df_country.year[df_country.country==country].unique()
for yrs in years:
focus = (df_country.country==country) & (df_country.year==yrs)
month = df_country.month[focus]
workingGasVolume_peryear = df_country.workingGasVolume[focus]
gasInStorage_peryear = df_country.gasInStorage[focus]
# STEP 2, notice position of arguments!
fig.add_trace(go.Scatter(x=month,
y=workingGasVolume_peryear,
name=f'workingGasVolume{yrs}',
line=dict(width=4,dash='dash')),
row=i+1, #index for the subplot, i+1 because plotly starts with 1
col=1)
fig.add_trace(go.Scatter(x=month,
y=gasInStorage_peryear,
name=f'gasInStorage{yrs}',
line = dict(width=4)),
row=i+1,
col=1)
fig.show()
但我仍然有回溯消息
Traceback (most recent call last):
File "<ipython-input-27-513826172e49>", line 43, in <module>
line=dict(width=4,dash='dash')),
TypeError: 'dict' object is not callable