5

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.

import plotly.offline as py
import plotly.io as pio

pio.write_html(py.offline.plot([{
'x': df.index,
'y': df[col],
'name': col
}for col in trend_data.columns], filename=new_file_path))

I want to iterate through each column and create a subplot for each one. I have tried;

from plotly.subplots import make_subplots

sub_titles = df.columns()
fig = make_subplots(rows=6, cols=6, start_cell="bottom-left", subplot_titles=sub_titles)
for i in df.columns:
    fig.add_trace(i)

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;

sub_titles = list(df)
fig = py.subplots.make_subplots(rows=6, cols=6, sub_titles=sub_titles)

This also returns an error. Any help is appreciatted.

2 Answers 2

7

Plot:

enter image description here

Code:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 50
n_plots = 36
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# plotly setup
plot_rows=6
plot_cols=6
fig = make_subplots(rows=plot_rows, cols=plot_cols)

# add traces
x = 0
for i in range(1, plot_rows + 1):
    for j in range(1, plot_cols + 1):
        #print(str(i)+ ', ' + str(j))
        fig.add_trace(go.Scatter(x=df.index, y=df[df.columns[x]].values,
                                 name = df.columns[x],
                                 mode = 'lines'),
                     row=i,
                     col=j)

        x=x+1

# Format and show fig
fig.update_layout(height=1200, width=1200)
fig.show()

Addition: 1-column solution:

Code:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 50
frame_columns = ['V_'+str(e) for e in list(range(1,37))]
df = pd.DataFrame(np.random.uniform(-8,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# plotly setup
plot_rows=6
plot_cols=6

lst1 = list(range(1,plot_rows+1))
lst2 = list(range(1,plot_cols+1))

fig = make_subplots(rows=36, cols=1, subplot_titles=df.columns, insets=[{'l': 0.1, 'b': 0.1, 'h':1}])

# add traces
x = 1
for i in lst1:
    for j in lst2:
        #print(str(i)+ ', ' + str(j))
        fig.add_trace(go.Scatter(x=df.index, y=df[df.columns[x-1]].values,
                                 name = df.columns[x-1],
                                 mode = 'lines',
                                 ),

                      row=x,
                     col=1)

        x=x+1

fig.update_layout(height=12000, width=1200)

fig.show()

Plots:

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this, is it possible to make the graphs larger then scroll through the page to view them in more detail? I can' think of a way to fo this
@Jay That depends on where you are producing the plots. A Jupyter Notebook perhaps?
I'm using PyCharm to produce the plots. Did you just adjust fig.update_layout to have a huge length?
@Jay Yes, I did : fig.update_layout(height=12000, width=1200)
I'm wondering whether to learn plotly after using matplotlib and this surprised me. There is nothing as simple as fig, axes = plt.subplots(36, 1); for ax, col in zip(axes, df): df[col].plot(ax=ax) in the plotly API?
4

See documentation on how to use subplots. This could work:

UPDATE including subplot titles

fig = py.subplots.make_subplots(rows=36, cols=1, subplot_titles=df.columns)
j = 1
for i in df.columns:
    fig.add_trace(
        go.Scatter(
            {'x': df.index, 
             'y': df[i]}), 
             row=j, col=1)
    j += 1

This results in the following plot (with my data):

df = pd.DataFrame(np.random.randint(5, size=(5, 3)), columns=['one', 'two', 'three'])

subplots

3 Comments

Thats works well! If i wanted to assign the column headers are subplot names, how would I do so? I tried; sub_titles = list(df) fig = py.subplots.make_subplots(rows=6, cols=6, sub_titles=sub_titles)
@Jay Did you want this in a 6x6 setup?
Thanks. @vestland Ideally I would like to be able to have 1 column but when I do this the graphs are tiny and not viewable. Is it possible to make the resulting html page scrollable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.