Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Two different plots in the same python script

Hi there,

I have made a script creating two offline graphs, one scattered and one line plot.
I get a result that opens two html pages automatically, but both showing only the line plot (which happens to be the second one to call from script).
The code works individually but I want my script to create both graphs with one script execution.

Any Hints?

I’ve built my own div construction function for this:

import uuid
import json

from plotly import utils

def plot_to_htmldiv(data, layout=None):
assert isinstance(data, (list, tuple))
layout = layout or {}

divid = uuid.uuid4()
script = ('Plotly.newPlot("%(id)s", %(data)s, %(layout)s, {displaylogo: false, modeBarButtonsToRemove: ["sendDataToCloud"]})' %
          dict(id=divid,
               data=json.dumps(data, cls=utils.PlotlyJSONEncoder),
               layout=json.dumps(layout, cls=utils.PlotlyJSONEncoder)))
return (
    '<div id="{id}" style="height: {height}px; width: {width}px;" '
    '     class="plotly-graph-div">'
    '</div>'
    '<script type="text/javascript">'
    'window.PLOTLYENV=window.PLOTLYENV || {{}};'
    '{script}'
    '</script>').format(
        id=divid, script=script,
        height=layout.get('height', 600),
        width=layout.get('width', 900))

Thanks auc for your answer!

Well, i am passing data in plotly through an xml parser.

I found out that I was saving the two plots to my local folder with the same - default - filename. As a result, the second plot overwrites the first one, when executing in the same script.
When I defined the first and second plot to be saved with different filenames the problem was solved.
Now with one execution I have two different plots as an output.

plotly.offline.plot({
“data”: [
Scatter(x=date1,y=impact1)
],
“layout”: Layout(
title=“Impact loadt”,yaxis=dict(title=‘Impact load’),xaxis=dict(title=‘datetime’)
)
},filename=‘graph1.html’)

plotly.offline.plot({
“data”: [
Scatter(x=date2,y=impact2)
],
“layout”: Layout(
title=“Impact load”,yaxis=dict(title=‘Impact load’),xaxis=dict(title=‘datetime’)
)
},filename=‘graph2.html’)