I have a dataframe ‘df’ with 36 columns, these columns are plotted onto a single plotly chart and displayed in html format using the code below.
JavaScript
x
9
1
import plotly.offline as py
2
import plotly.io as pio
3
4
pio.write_html(py.offline.plot([{
5
'x': df.index,
6
'y': df[col],
7
'name': col
8
}for col in trend_data.columns], filename=new_file_path))
9
I want to iterate through each column and create a subplot for each one. I have tried;
JavaScript
1
7
1
from plotly.subplots import make_subplots
2
3
sub_titles = df.columns()
4
fig = make_subplots(rows=6, cols=6, start_cell="bottom-left", subplot_titles=sub_titles)
5
for i in df.columns:
6
fig.add_trace(i)
7
I created 6 rows and columns as that would give 36 plots and tried to use the header names as subplot titles but I get a ValueError stating it was expecting a 2d list of dictionaries.
Also, I have tried to add subplot titles by;
JavaScript
1
3
1
sub_titles = list(df)
2
fig = py.subplots.make_subplots(rows=6, cols=6, sub_titles=sub_titles)
3
This also returns an error. Any help is appreciatted.
Advertisement
Answer
Plot:
Code:
JavaScript
1
39
39
1
# imports
2
from plotly.subplots import make_subplots
3
import plotly.graph_objs as go
4
import pandas as pd
5
import numpy as np
6
7
# data
8
np.random.seed(123)
9
frame_rows = 50
10
n_plots = 36
11
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
12
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
13
index=pd.date_range('1/1/2020', periods=frame_rows),
14
columns=frame_columns)
15
df=df.cumsum()+100
16
df.iloc[0]=100
17
18
# plotly setup
19
plot_rows=6
20
plot_cols=6
21
fig = make_subplots(rows=plot_rows, cols=plot_cols)
22
23
# add traces
24
x = 0
25
for i in range(1, plot_rows + 1):
26
for j in range(1, plot_cols + 1):
27
#print(str(i)+ ', ' + str(j))
28
fig.add_trace(go.Scatter(x=df.index, y=df[df.columns[x]].values,
29
name = df.columns[x],
30
mode = 'lines'),
31
row=i,
32
col=j)
33
34
x=x+1
35
36
# Format and show fig
37
fig.update_layout(height=1200, width=1200)
38
fig.show()
39
Addition: 1-column solution:
Code:
JavaScript
1
44
44
1
# imports
2
from plotly.subplots import make_subplots
3
import plotly.graph_objs as go
4
import pandas as pd
5
import numpy as np
6
7
# data
8
np.random.seed(123)
9
frame_rows = 50
10
frame_columns = ['V_'+str(e) for e in list(range(1,37))]
11
df = pd.DataFrame(np.random.uniform(-8,10,size=(frame_rows, len(frame_columns))),
12
index=pd.date_range('1/1/2020', periods=frame_rows),
13
columns=frame_columns)
14
df=df.cumsum()+100
15
df.iloc[0]=100
16
17
# plotly setup
18
plot_rows=6
19
plot_cols=6
20
21
lst1 = list(range(1,plot_rows+1))
22
lst2 = list(range(1,plot_cols+1))
23
24
fig = make_subplots(rows=36, cols=1, subplot_titles=df.columns, insets=[{'l': 0.1, 'b': 0.1, 'h':1}])
25
26
# add traces
27
x = 1
28
for i in lst1:
29
for j in lst2:
30
#print(str(i)+ ', ' + str(j))
31
fig.add_trace(go.Scatter(x=df.index, y=df[df.columns[x-1]].values,
32
name = df.columns[x-1],
33
mode = 'lines',
34
),
35
36
row=x,
37
col=1)
38
39
x=x+1
40
41
fig.update_layout(height=12000, width=1200)
42
43
fig.show()
44
Plots: