Python Forum
[Plot a stacked bar graph using plotly offline mode]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Plot a stacked bar graph using plotly offline mode]
#1
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')
Reply
#2
I succeed in plotting using below code :
import pandas as pd

all_data = [('2018-04-09', '10:18:11', ['s1',10],['s2',15],['s3',5]),
            ('2018-04-09', '10:20:11', ['s4',8], ['s2',20],['s1',10]),
            ('2018-04-10', '10:30:11', ['s4',10],['s5',6], ['s6',3]) ]

#load data into dataframe
df = pd.DataFrame(all_data, columns = list("ABCDE"))
#combine the two descriptors
df["day/time"] = df["A"] + "\n" + df["B"]
#assign each list to a new row with the appropriate day/time label
df = df.melt(id_vars = ["day/time"], value_vars = ["C", "D", "E"])
#split each list into category and value
df[["category", "val"]] = pd.DataFrame(df.value.values.tolist(), index = df.index)
#create a table with category-value pairs from all lists, missing values are set to NaN
df = df.pivot(index = "day/time", columns = "category", values = "val")
#plot a stacked bar chart 
df.plot(kind = "bar", stacked = True)
fig=df.iplot(kin='bar',barmode='stack',asFigure=True)

plotly.offline.plt(fig,filename="stack1.html)
However I faced two errors:

1.When Time intervals are very close,Data overlaps on graph.Is there a way to overcome it.

2.When using large data set,graph us not getting plotted giving the error message :
ValueError: Index contains duplicate entries, cannot reshape.

To overcome the error 2,i tried some random integer adding to A column(date value but same error).All the time entries are unique in my data set but still giving the error.Any suggestions please.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  installing anaconda packages offline HulkHogansArmsArePythons 4 1,276 Feb-18-2020, 08:23 PM
Last Post: jefsummers
  Enhance my stacked barplot title, and flip legend Clunk_Head 0 550 Jul-08-2019, 03:30 AM
Last Post: Clunk_Head
  Automatically plot each variable on the same graph for each "ID" AdWill97 0 808 Apr-15-2019, 09:29 AM
Last Post: AdWill97
  Tensorflow offline build from source on CentOS 7 riotto 3 1,699 Mar-21-2019, 07:16 PM
Last Post: riotto
  stacked autoencoder training JohnMarie 0 742 Feb-24-2019, 12:23 AM
Last Post: JohnMarie
  Plotly Error muhsin 0 2,814 Mar-04-2018, 06:36 PM
Last Post: muhsin
  AttributeError: module 'plotly' has no attribute 'offline' charlesczc 8 10,236 Jan-21-2018, 08:34 AM
Last Post: buran
  Offline audio to text (Speech Recognition) jehoshua 14 48,590 Jan-17-2018, 11:19 AM
Last Post: jehoshua
  Stacked Barchart from Counter using matplotlib alicarlos13 4 5,323 Mar-31-2017, 08:17 PM
Last Post: alicarlos13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020