Plotting a grouped bar chart using Plotly from a pandas dataframe











up vote
0
down vote

favorite












Very new to Ploty so sorry if this is an obvious one... I'm trying to create a grouped bar chart which includes the following information which is stored in SQL.



|-----------|------|-------------|
| RouteName | Hour | JourneyTime |
|-----------|------|-------------|
|Route #1 |6:00 |200 |
|Route #2 |6:00 |400 |
|Route #3 |6:00 |500 |
|Route #1 |7:00 |100 |
|Route #2 |7:00 |300 |
|Route #3 |7:00 |550 |
|Route #1 |8:00 |330 |
|Route #2 |8:00 |450 |
|Route #3 |8:00 |600 |


Essentially I want the Hour column as x, the JourneyTime column as y and the RouteName column to differentiate between the routes (different colours for different bars).



I've managed to get plotly (using the following code) to create a graph from a pandas dataframe. However it only shows one bar for each Hour and not the entire dataset.



import pandas as pd
import pymssql
import plotly
import plotly.graph_objs as go
from plotly.offline import *

ServerNm = str("ServerName")
DatabaseNm = str("DatabaseName")

SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

df2 = pd.read_sql(SQL_Query2, SQLCon)
SQLCon.close

plotly.offline.plot([go.Bar(x=df2.Hour, y=df2.JourneyTime,name='RouteName')])


Looking at the link Adding group bar charts as subplots in plotly I've seen that you can give it separate series of data as trace1 and trace2. However I will ultimately be linking this up with filter boxes in a Dash Dashboard (and the number of routes will vary) so I would require flexibility to have this change on the fly.



Any help that can be offered would be greatly appreciated.



Many thanks










share|improve this question


























    up vote
    0
    down vote

    favorite












    Very new to Ploty so sorry if this is an obvious one... I'm trying to create a grouped bar chart which includes the following information which is stored in SQL.



    |-----------|------|-------------|
    | RouteName | Hour | JourneyTime |
    |-----------|------|-------------|
    |Route #1 |6:00 |200 |
    |Route #2 |6:00 |400 |
    |Route #3 |6:00 |500 |
    |Route #1 |7:00 |100 |
    |Route #2 |7:00 |300 |
    |Route #3 |7:00 |550 |
    |Route #1 |8:00 |330 |
    |Route #2 |8:00 |450 |
    |Route #3 |8:00 |600 |


    Essentially I want the Hour column as x, the JourneyTime column as y and the RouteName column to differentiate between the routes (different colours for different bars).



    I've managed to get plotly (using the following code) to create a graph from a pandas dataframe. However it only shows one bar for each Hour and not the entire dataset.



    import pandas as pd
    import pymssql
    import plotly
    import plotly.graph_objs as go
    from plotly.offline import *

    ServerNm = str("ServerName")
    DatabaseNm = str("DatabaseName")

    SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

    SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

    df2 = pd.read_sql(SQL_Query2, SQLCon)
    SQLCon.close

    plotly.offline.plot([go.Bar(x=df2.Hour, y=df2.JourneyTime,name='RouteName')])


    Looking at the link Adding group bar charts as subplots in plotly I've seen that you can give it separate series of data as trace1 and trace2. However I will ultimately be linking this up with filter boxes in a Dash Dashboard (and the number of routes will vary) so I would require flexibility to have this change on the fly.



    Any help that can be offered would be greatly appreciated.



    Many thanks










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Very new to Ploty so sorry if this is an obvious one... I'm trying to create a grouped bar chart which includes the following information which is stored in SQL.



      |-----------|------|-------------|
      | RouteName | Hour | JourneyTime |
      |-----------|------|-------------|
      |Route #1 |6:00 |200 |
      |Route #2 |6:00 |400 |
      |Route #3 |6:00 |500 |
      |Route #1 |7:00 |100 |
      |Route #2 |7:00 |300 |
      |Route #3 |7:00 |550 |
      |Route #1 |8:00 |330 |
      |Route #2 |8:00 |450 |
      |Route #3 |8:00 |600 |


      Essentially I want the Hour column as x, the JourneyTime column as y and the RouteName column to differentiate between the routes (different colours for different bars).



      I've managed to get plotly (using the following code) to create a graph from a pandas dataframe. However it only shows one bar for each Hour and not the entire dataset.



      import pandas as pd
      import pymssql
      import plotly
      import plotly.graph_objs as go
      from plotly.offline import *

      ServerNm = str("ServerName")
      DatabaseNm = str("DatabaseName")

      SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

      SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

      df2 = pd.read_sql(SQL_Query2, SQLCon)
      SQLCon.close

      plotly.offline.plot([go.Bar(x=df2.Hour, y=df2.JourneyTime,name='RouteName')])


      Looking at the link Adding group bar charts as subplots in plotly I've seen that you can give it separate series of data as trace1 and trace2. However I will ultimately be linking this up with filter boxes in a Dash Dashboard (and the number of routes will vary) so I would require flexibility to have this change on the fly.



      Any help that can be offered would be greatly appreciated.



      Many thanks










      share|improve this question













      Very new to Ploty so sorry if this is an obvious one... I'm trying to create a grouped bar chart which includes the following information which is stored in SQL.



      |-----------|------|-------------|
      | RouteName | Hour | JourneyTime |
      |-----------|------|-------------|
      |Route #1 |6:00 |200 |
      |Route #2 |6:00 |400 |
      |Route #3 |6:00 |500 |
      |Route #1 |7:00 |100 |
      |Route #2 |7:00 |300 |
      |Route #3 |7:00 |550 |
      |Route #1 |8:00 |330 |
      |Route #2 |8:00 |450 |
      |Route #3 |8:00 |600 |


      Essentially I want the Hour column as x, the JourneyTime column as y and the RouteName column to differentiate between the routes (different colours for different bars).



      I've managed to get plotly (using the following code) to create a graph from a pandas dataframe. However it only shows one bar for each Hour and not the entire dataset.



      import pandas as pd
      import pymssql
      import plotly
      import plotly.graph_objs as go
      from plotly.offline import *

      ServerNm = str("ServerName")
      DatabaseNm = str("DatabaseName")

      SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

      SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

      df2 = pd.read_sql(SQL_Query2, SQLCon)
      SQLCon.close

      plotly.offline.plot([go.Bar(x=df2.Hour, y=df2.JourneyTime,name='RouteName')])


      Looking at the link Adding group bar charts as subplots in plotly I've seen that you can give it separate series of data as trace1 and trace2. However I will ultimately be linking this up with filter boxes in a Dash Dashboard (and the number of routes will vary) so I would require flexibility to have this change on the fly.



      Any help that can be offered would be greatly appreciated.



      Many thanks







      python pandas plotly plotly-dash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 19:04









      Gavin

      176




      176
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          you can change the number of traces you pass on the fly too. Maybe something like this:



          df = pd.read_sql(query, con)
          traces = [go.Bar(x=subset.Hour,
          y=subset.JourneyTime,
          name=route)
          for route, subset in df.groupby("RouteName")]
          plotly.offline.plot(traces)





          share|improve this answer





















          • Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
            – Gavin
            Nov 20 at 12:36












          • There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
            – Robert
            Nov 20 at 12:56


















          up vote
          0
          down vote













          It is possible to create a loop using a separate dataframe as a filter. Not the most elegant solution, but it does work.



          In the code below df1 gets a list of just the Routes and df2 is our complete dataset. From this it is possible to loop through the rows and generate a new dataframe (df3). This dataframe creates each bar which are all appended together to create the final plot.



          import pandas as pd
          import pymssql
          import plotly
          import plotly.graph_objs as go
          from plotly.offline import *

          ServerNm = str("ServerName")
          DatabaseNm = str("DatabaseName")

          SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

          SQL_Query1 = '''SELECT [RouteName] FROM [Dashboard].[dbo].[JTs_v2] GROUP BY [RouteName]'''
          SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

          df1 = pd.read_sql(SQL_Query1, SQLCon)
          df2 = pd.read_sql(SQL_Query2, SQLCon)

          SQLCon.close

          bars =

          for index, row in df1.iterrows():

          route=row['RouteName']
          df3 = df2[df2.RouteName == route][['Hour', 'JourneyTime']]
          bars.append(
          go.Bar(
          x=df3.Hour,
          y=df3.JourneyTime,
          name=route))


          fig = go.Figure(data=bars)
          plotly.offline.plot(fig)





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381074%2fplotting-a-grouped-bar-chart-using-plotly-from-a-pandas-dataframe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            you can change the number of traces you pass on the fly too. Maybe something like this:



            df = pd.read_sql(query, con)
            traces = [go.Bar(x=subset.Hour,
            y=subset.JourneyTime,
            name=route)
            for route, subset in df.groupby("RouteName")]
            plotly.offline.plot(traces)





            share|improve this answer





















            • Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
              – Gavin
              Nov 20 at 12:36












            • There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
              – Robert
              Nov 20 at 12:56















            up vote
            0
            down vote













            you can change the number of traces you pass on the fly too. Maybe something like this:



            df = pd.read_sql(query, con)
            traces = [go.Bar(x=subset.Hour,
            y=subset.JourneyTime,
            name=route)
            for route, subset in df.groupby("RouteName")]
            plotly.offline.plot(traces)





            share|improve this answer





















            • Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
              – Gavin
              Nov 20 at 12:36












            • There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
              – Robert
              Nov 20 at 12:56













            up vote
            0
            down vote










            up vote
            0
            down vote









            you can change the number of traces you pass on the fly too. Maybe something like this:



            df = pd.read_sql(query, con)
            traces = [go.Bar(x=subset.Hour,
            y=subset.JourneyTime,
            name=route)
            for route, subset in df.groupby("RouteName")]
            plotly.offline.plot(traces)





            share|improve this answer












            you can change the number of traces you pass on the fly too. Maybe something like this:



            df = pd.read_sql(query, con)
            traces = [go.Bar(x=subset.Hour,
            y=subset.JourneyTime,
            name=route)
            for route, subset in df.groupby("RouteName")]
            plotly.offline.plot(traces)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 19:17









            Robert

            34429




            34429












            • Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
              – Gavin
              Nov 20 at 12:36












            • There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
              – Robert
              Nov 20 at 12:56


















            • Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
              – Gavin
              Nov 20 at 12:36












            • There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
              – Robert
              Nov 20 at 12:56
















            Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
            – Gavin
            Nov 20 at 12:36






            Thanks for your help. It does load in all the bars, however it just loads in the same route 3 times e.g. Route 2 would be shown in 6:00am in 3 separate bars instead of all routes. I think I may need to set up a loop on the dataframe first (to filter for each route) before trying to load it into a bar. Will let you know how it goes.
            – Gavin
            Nov 20 at 12:36














            There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
            – Robert
            Nov 20 at 12:56




            There’s already a loop here inside the list comprehension. Yeah you could just do a loop, filter the df, create the Bar and append the result to a list, that should give you the same results. Not sure why it wasn’t working as above but if you play around you’ll develop more intuition around it
            – Robert
            Nov 20 at 12:56












            up vote
            0
            down vote













            It is possible to create a loop using a separate dataframe as a filter. Not the most elegant solution, but it does work.



            In the code below df1 gets a list of just the Routes and df2 is our complete dataset. From this it is possible to loop through the rows and generate a new dataframe (df3). This dataframe creates each bar which are all appended together to create the final plot.



            import pandas as pd
            import pymssql
            import plotly
            import plotly.graph_objs as go
            from plotly.offline import *

            ServerNm = str("ServerName")
            DatabaseNm = str("DatabaseName")

            SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

            SQL_Query1 = '''SELECT [RouteName] FROM [Dashboard].[dbo].[JTs_v2] GROUP BY [RouteName]'''
            SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

            df1 = pd.read_sql(SQL_Query1, SQLCon)
            df2 = pd.read_sql(SQL_Query2, SQLCon)

            SQLCon.close

            bars =

            for index, row in df1.iterrows():

            route=row['RouteName']
            df3 = df2[df2.RouteName == route][['Hour', 'JourneyTime']]
            bars.append(
            go.Bar(
            x=df3.Hour,
            y=df3.JourneyTime,
            name=route))


            fig = go.Figure(data=bars)
            plotly.offline.plot(fig)





            share|improve this answer

























              up vote
              0
              down vote













              It is possible to create a loop using a separate dataframe as a filter. Not the most elegant solution, but it does work.



              In the code below df1 gets a list of just the Routes and df2 is our complete dataset. From this it is possible to loop through the rows and generate a new dataframe (df3). This dataframe creates each bar which are all appended together to create the final plot.



              import pandas as pd
              import pymssql
              import plotly
              import plotly.graph_objs as go
              from plotly.offline import *

              ServerNm = str("ServerName")
              DatabaseNm = str("DatabaseName")

              SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

              SQL_Query1 = '''SELECT [RouteName] FROM [Dashboard].[dbo].[JTs_v2] GROUP BY [RouteName]'''
              SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

              df1 = pd.read_sql(SQL_Query1, SQLCon)
              df2 = pd.read_sql(SQL_Query2, SQLCon)

              SQLCon.close

              bars =

              for index, row in df1.iterrows():

              route=row['RouteName']
              df3 = df2[df2.RouteName == route][['Hour', 'JourneyTime']]
              bars.append(
              go.Bar(
              x=df3.Hour,
              y=df3.JourneyTime,
              name=route))


              fig = go.Figure(data=bars)
              plotly.offline.plot(fig)





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It is possible to create a loop using a separate dataframe as a filter. Not the most elegant solution, but it does work.



                In the code below df1 gets a list of just the Routes and df2 is our complete dataset. From this it is possible to loop through the rows and generate a new dataframe (df3). This dataframe creates each bar which are all appended together to create the final plot.



                import pandas as pd
                import pymssql
                import plotly
                import plotly.graph_objs as go
                from plotly.offline import *

                ServerNm = str("ServerName")
                DatabaseNm = str("DatabaseName")

                SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

                SQL_Query1 = '''SELECT [RouteName] FROM [Dashboard].[dbo].[JTs_v2] GROUP BY [RouteName]'''
                SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

                df1 = pd.read_sql(SQL_Query1, SQLCon)
                df2 = pd.read_sql(SQL_Query2, SQLCon)

                SQLCon.close

                bars =

                for index, row in df1.iterrows():

                route=row['RouteName']
                df3 = df2[df2.RouteName == route][['Hour', 'JourneyTime']]
                bars.append(
                go.Bar(
                x=df3.Hour,
                y=df3.JourneyTime,
                name=route))


                fig = go.Figure(data=bars)
                plotly.offline.plot(fig)





                share|improve this answer












                It is possible to create a loop using a separate dataframe as a filter. Not the most elegant solution, but it does work.



                In the code below df1 gets a list of just the Routes and df2 is our complete dataset. From this it is possible to loop through the rows and generate a new dataframe (df3). This dataframe creates each bar which are all appended together to create the final plot.



                import pandas as pd
                import pymssql
                import plotly
                import plotly.graph_objs as go
                from plotly.offline import *

                ServerNm = str("ServerName")
                DatabaseNm = str("DatabaseName")

                SQLCon = pymssql.connect(host=ServerNm,database=DatabaseNm)

                SQL_Query1 = '''SELECT [RouteName] FROM [Dashboard].[dbo].[JTs_v2] GROUP BY [RouteName]'''
                SQL_Query2 = '''SELECT [RouteName], [Hour], [JourneyTime] FROM [Dashboard].[dbo].[JTs_v2]'''

                df1 = pd.read_sql(SQL_Query1, SQLCon)
                df2 = pd.read_sql(SQL_Query2, SQLCon)

                SQLCon.close

                bars =

                for index, row in df1.iterrows():

                route=row['RouteName']
                df3 = df2[df2.RouteName == route][['Hour', 'JourneyTime']]
                bars.append(
                go.Bar(
                x=df3.Hour,
                y=df3.JourneyTime,
                name=route))


                fig = go.Figure(data=bars)
                plotly.offline.plot(fig)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 14:52









                Gavin

                176




                176






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381074%2fplotting-a-grouped-bar-chart-using-plotly-from-a-pandas-dataframe%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen