【发布时间】:2020-09-25 00:14:24
【问题描述】:
我已经在嵌套循环下面编写了成功生成 21 个图表(每个国家/地区一个图表,例如 german gasaustrian gas)
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 消息
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
【问题讨论】:
-
您介意分享您的数据样本吗?
-
我没有看到任何对
row和col的子图引用,例如fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]),row=1, col=1)。见here。 -
@rpanai 谢谢,我刚刚编辑了我的帖子,您可以看到奥地利数据的 df 之一,我将其转换为 dict 以便能够粘贴到此处。这是你问的吗?
-
是的,但是当您
fig.add_trace时,您需要告诉它哪个row和col... 它的 jayveesea,顺便说一句 :) -
嗨 @jayveesea 只是给你一些反馈,我仍然不知道为什么我不能运行你的代码,但我已经通过将 2 个参数行和列移出更正了我在 6 月 7 日上面的代码go.scatter 的,正如你之前提到的。现在这个版本的代码可以工作并且我有 1 个 HTML,现在我正在努力修复图例,因为有 21 x 7 个图例全部粘在 html 的顶部而不是靠近每个子图。感谢您的帮助
标签: python loops plotly subplot