0

I am following this link to study https://plot.ly/python/bar-charts/ plot graphs in python using plotly.

As per my requirement ,I have sample data of the form

data=[                                               
      ('2018-04-17','22:00:00',['p1',5],['p2',10],['p3',15]),
      ('2018-04-18','20:00:00',['p3',5],['p4',10],['p3',15])
     ]

So I want to create a stacked bar graph of this data using plotly. On X-axis,first two values of tuple will be plotted(date and time).

On y-axis , a stacked graph having three values with bar height in link with the value(which will be shown on mouse hover).As total 4 values are there in this data(p1,p2,p3,p4),four different colors will be used for them.

What I have tried : I basically tried hit and trial using the methods shown like take two data values using plotly offline graph object.But I am not able to draw it.

Can Anyone please provide suggestions for stacked histogram of these data.I have to view the generated file in browser and I am using plotly in offline mode.

What code I tried :

    import plotly
    import plotly.graph_objs as go

    plotly.offline.init_notebook_mode(connected=True)


    data=[                                               
          ('2018-04-17','22:00:00',['p1',5],['p2',10],['p3',15]),
          ('2018-04-18','20:00:00',['p3',5],['p4',10],['p3',15])
         ]


    x_axis=[]
    y_axis=[]
    plot_data=[]
    for d in data
        date,time=d[0],d[1]
        x_axis.append(d[0]+" "+d[1])
        for j in range (2, len(d))
            y_axis.append(d[j][1])

        trace[]=go.Bar(
                x=x_axis,
                y=y_axis)
        plot_data.append(trace)

    plotly.offline.plot(plot_data, filename='stacked-bar')
2
  • @Alfe: I tried. Where I am stuck is the data (p1,p2,p3,p4) is not constant through out.Like one tuple have p1,p2,p3 other have p4,p5,p6.Had it been same ,I could have easily read the list and first read the index of the string and read the value of the index in list.I actually did this for other data of my project. Commented Apr 17, 2018 at 13:58
  • Alfe: I did .Can you just suggest if not code :) Commented Apr 18, 2018 at 13:08

1 Answer 1

2

You need to use fig and layout. In layout you set barmode equal to stack:

import plotly
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)

data=[                                               
      ('2018-04-17','22:00:00',['p1',5],['p2',10],['p3',15]),
      ('2018-04-18','20:00:00',['p3',5],['p4',10],['p3',15])
     ]
x_axis=[]
y_axis=[]
plot_data=[]
for d in data:
    date,time=d[0],d[1]
    x_axis.append(d[0]+" "+d[1])
    for j in range (2, len(d)):
        y_axis.append(d[j][1])
        trace=go.Bar(
            x=x_axis,
            y=y_axis)
        plot_data.append(trace)
layout = go.Layout(barmode="stack")
fig = go.Figure(data=plot_data, layout=layout)
plotly.offline.plot(fig, filename='stacked-bar.html')

And your output should be looks like that: It is what your looking for, yep?)

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

1 Comment

Is my answer help you, @user7350953?

Your Answer

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