plotlyで、複数のグラフを縦一列に並べたいのですが、エラーが出て動かすことが出来ません。
プログラムの概要としましては、フォルダ内のCSVをすべて読み込み、そのCSV1つ1つを2つに分割させます。fig.add_trace
がfor
文内に二回出てくるのは、分割したcsvを重ねて、1つのグラフとして表示させたいからです。
そして、for
文でcsvの個数分のグラフを作成し、縦に並べたいのです。
x_axis = " G"
y_axis = " D"
graphNumber = 11
file_names = glob.glob('*.csv')
def get_colorpalette(colorpalette, file_number):
palette = sns.color_palette(
colorpalette, file_number)
rgb = ['rgb({},{},{})'.format(*[x*256 for x in rgb])
for rgb in palette]
return rgb
colors = get_colorpalette('hls', graphNumber)
fig = make_subplots(len(df_list_1), 1)
for i in range(len(df_list_1)):
for j in range(graphNumber):
print(i)
fig.add_trace(
go.Scatter(
x=df_list_0[i][x_axis][parts_number * j:parts_number * ( j + 1)],
y=df_list_0[i][y_axis][parts_number * j:parts_number * ( j + 1)],
name='forward{:.1f}'.format(j * 0.1),
marker={'color':colors[j]}, mode='lines'
), i, 1
)
fig.add_trace(
go.Scatter(
x=df_list_1[i][x_axis][parts_number * j:parts_number * ( j + 1)],
y=df_list_1[i][y_axis][parts_number * j:parts_number * ( j + 1)],
name='backward{:.1f}'.format(j * 0.1),
marker={'color':colors[j]}, mode='lines'
), i, 1
)
fig.update_xaxes(matches='x')
fig.show()
エラー
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-93-6f8e07a27f21> in <module>
5 for j in range(graphNumber):
6 print(i)
----> 7 fig.add_trace(
8 go.Scatter(
9 x=df_list_0[i][x_axis][parts_number * j:parts_number * ( j + 1)],
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\basedatatypes.py in add_trace(self, trace, row, col, secondary_y)
1646 )
1647
-> 1648 return self.add_traces(
1649 data=[trace],
1650 rows=[row] if row is not None else None,
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys)
1764 if rows is not None:
1765 for trace, row, col, secondary_y in zip(data, rows, cols, secondary_ys):
-> 1766 self._set_trace_grid_position(trace, row, col, secondary_y)
1767
1768 # Make deep copy of trace data (Optimize later if needed)
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\basedatatypes.py in _set_trace_grid_position(self, trace, row, col, secondary_y)
1846
1847 grid_ref = self._validate_get_grid_ref()
-> 1848 return _set_trace_grid_reference(
1849 trace, self.layout, grid_ref, row, col, secondary_y
1850 )
c:\users\owner\appdata\local\programs\python\python38-32\lib\site-packages\plotly\subplots.py in _set_trace_grid_reference(trace, layout, grid_ref, row, col, secondary_y)
1309
1310 if row <= 0:
-> 1311 raise Exception(
1312 "Row value is out of range. " "Note: the starting cell is (1, 1)"
1313 )
Exception: Row value is out of range. Note: the starting cell is (1, 1)
Note: the starting cell is (1, 1)
と表示されている通り、fig.add_trace(..., i, 1)
の部分で、i
が0
になっている事が原因です(subplot の行・列インデックスは 1 から始まります)。なので、for i in range(len(df_list_1)):
をfor i in range(1, len(df_list_1)+1):
にするとかfig.add_trace(..., i+1, 1)
とする必要があります。