Histogram per hour - matplotlib











up vote
0
down vote

favorite












I'm analyzing public data on transport accidents in the UK.



My dataframe looks like this :



Index     Time

0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05


I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :



  import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')


This is the output:



Graph



In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?










share|improve this question
























  • Same as in this question you would just keep the other half of the strings.
    – ImportanceOfBeingErnest
    Nov 11 at 0:47






  • 1




    @mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
    – Fra
    Nov 11 at 10:41










  • No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
    – Fra
    Nov 11 at 10:58










  • @mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
    – Fra
    Nov 11 at 11:39










  • @mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
    – Fra
    Nov 11 at 13:38















up vote
0
down vote

favorite












I'm analyzing public data on transport accidents in the UK.



My dataframe looks like this :



Index     Time

0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05


I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :



  import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')


This is the output:



Graph



In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?










share|improve this question
























  • Same as in this question you would just keep the other half of the strings.
    – ImportanceOfBeingErnest
    Nov 11 at 0:47






  • 1




    @mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
    – Fra
    Nov 11 at 10:41










  • No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
    – Fra
    Nov 11 at 10:58










  • @mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
    – Fra
    Nov 11 at 11:39










  • @mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
    – Fra
    Nov 11 at 13:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm analyzing public data on transport accidents in the UK.



My dataframe looks like this :



Index     Time

0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05


I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :



  import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')


This is the output:



Graph



In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?










share|improve this question















I'm analyzing public data on transport accidents in the UK.



My dataframe looks like this :



Index     Time

0 02:30
1 00:37
2 01:25
3 09:15
4 07:53
5 09:29
6 08:53
7 10:05


I'm trying to plot a histogram showing accident distribution by time of day,
here is my code :



  import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import matplotlib.dates as mdates
df['hour']=pd.to_datetime(df['Time'],format='%H:%M')
df.set_index('hour', drop=False, inplace=True)
df['hour'].groupby(pd.Grouper(freq='60Min')).count().plot(kind='bar', color='b')


This is the output:



Graph



In this graph, I'd like to change the labels on the x-axis to the format 'hh:mm'. How would I go about doing this?







python matplotlib histogram pandas-groupby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:15









Matt

750920




750920










asked Nov 10 at 23:55









Fra

32




32












  • Same as in this question you would just keep the other half of the strings.
    – ImportanceOfBeingErnest
    Nov 11 at 0:47






  • 1




    @mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
    – Fra
    Nov 11 at 10:41










  • No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
    – Fra
    Nov 11 at 10:58










  • @mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
    – Fra
    Nov 11 at 11:39










  • @mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
    – Fra
    Nov 11 at 13:38


















  • Same as in this question you would just keep the other half of the strings.
    – ImportanceOfBeingErnest
    Nov 11 at 0:47






  • 1




    @mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
    – Fra
    Nov 11 at 10:41










  • No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
    – Fra
    Nov 11 at 10:58










  • @mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
    – Fra
    Nov 11 at 11:39










  • @mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
    – Fra
    Nov 11 at 13:38
















Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47




Same as in this question you would just keep the other half of the strings.
– ImportanceOfBeingErnest
Nov 11 at 0:47




1




1




@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41




@mostafazh, thank you for your answer. Your answer works but x-axis appeared like imgur.com/a/0gBB7KJ . So x-axis is not indexed (I was looking for x-axis like that 01:00h, 02:00h, 03:00, ..)
– Fra
Nov 11 at 10:41












No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58




No, I'd like that x-axis was indexed by time (01:00, 02:00, 03:00 ..instead of 02:30, 00:30, 01:25).
– Fra
Nov 11 at 10:58












@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39




@mostafazh, my problem is that I want to plot data grouped by hour. I was doing it right in my first solution. The only problem was the datatime format. I needed 01:00 instead of 01/01/1900 01:00
– Fra
Nov 11 at 11:39












@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38




@mostafazh, thank you!! It works fine now, had some problem plotting in Jupyter. I just started programming with Python. Any suggestions about a good IDE? I'm using Jupyter but not totally satisfied. I'm looking for something interactive.
– Fra
Nov 11 at 13:38












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










What you are missing is setting the format of the matplotlib x-axis format:



df.set_index('hour', drop=False, inplace=True)
df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
ax = df.plot(kind='bar', color='b')
ticklabels = df.index.strftime('%H:%Mh')
ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
plt.show()





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%2f53244590%2fhistogram-per-hour-matplotlib%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    What you are missing is setting the format of the matplotlib x-axis format:



    df.set_index('hour', drop=False, inplace=True)
    df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
    ax = df.plot(kind='bar', color='b')
    ticklabels = df.index.strftime('%H:%Mh')
    ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
    plt.show()





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      What you are missing is setting the format of the matplotlib x-axis format:



      df.set_index('hour', drop=False, inplace=True)
      df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
      ax = df.plot(kind='bar', color='b')
      ticklabels = df.index.strftime('%H:%Mh')
      ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
      plt.show()





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        What you are missing is setting the format of the matplotlib x-axis format:



        df.set_index('hour', drop=False, inplace=True)
        df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
        ax = df.plot(kind='bar', color='b')
        ticklabels = df.index.strftime('%H:%Mh')
        ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
        plt.show()





        share|improve this answer














        What you are missing is setting the format of the matplotlib x-axis format:



        df.set_index('hour', drop=False, inplace=True)
        df = df['hour'].groupby(pd.Grouper(freq='60Min')).count()
        ax = df.plot(kind='bar', color='b')
        ticklabels = df.index.strftime('%H:%Mh')
        ax.xaxis.set_major_formatter(matplotlib.ticker.FixedFormatter(ticklabels))
        plt.show()






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 12:02

























        answered Nov 11 at 4:22









        mostafazh

        1,523718




        1,523718






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244590%2fhistogram-per-hour-matplotlib%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python