Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

When using plotly inside a loop, nothing is printed

Normally, I am able to use plotly library for visualization. However, the problem emerges when I try the same visualization inside a loop (or a function) like this:

for df in all_pages_df:

trace = go.Scatter(
    x = df['created_at'],
    y = df['count'],
    mode = 'markers'
)
data = [trace]

py.iplot(data, filename='basic-scatter')

The code above, prints nothing. When I use

print py.iplot(data, filename='basic-scatter')

Then all I see is

<plotly.tools.PlotlyDisplay object> 

as the output.

I did my search but could not see a solution to this problem. Any ideas?

@erkan A for loop does not display each plot in your notebook, but it saves the plots in Plotly cloud if you give distinct names for files.
If all_pages_df is a list of pandas.DataFrame(s) then defining:

   for k, df in enumerate(all_pages_df):
       trace = go.Scatter( x = df['created_at'],
                           y = df['count'],
                           mode = 'markers+lines'
                         )
       data = [trace]
       py.iplot(data, filename='basic-scatter'+'{:d}'.format(k+1))

your plots will be saved in your Plotly account.

If you use plotly offline, then they are displayed in the notebook, too.

1 Like

Is this still the case? How can I force plotly to display the different plots in a for loop?