Weird behavior of barplot from python matplotlib with datetime












0














import matplotlib.pyplot as plt
import datetime
x = [datetime.datetime(1943,3, 13,12,0,0),
datetime.datetime(1943,3, 13,12,5,0),
datetime.datetime(1943,3, 13,12,10,0),
datetime.datetime(1943,3, 13,12,15,0),
datetime.datetime(1943,3, 13,12,20,0),
datetime.datetime(1943,3, 13,12,25,0),
datetime.datetime(1943,3, 13,12,30,0),
datetime.datetime(1943,3, 13,12,35,0)]
y = [1,2,3,4,2,1,3,4]

# plot the data out but does not provide sufficient detail on the lower values
plt.figure()
plt.bar(x,y)

# plot the data out but ommit the datetime information
plt.figure()
plt.bar(range(0,len(x)),y)


Hello guys, I am just starting with the matplotlib in transition from matlab to python. However, I encountered weird behavior of matplotlib as it is not able to display the data along with the datetime element.
My question here would be the output of both bar plot yield two different results.



enter image description here



The first one directly convert the data into some kind of continuous data where as the second one more like categorical data. Do anyone encountered similar problem as mine and dont mind share their way of approaching this?



P/s: i tried seaborn and it works but somehow does not play well with dual axis plotting. I also googled for similar issue but somehow not such issue?










share|improve this question





























    0














    import matplotlib.pyplot as plt
    import datetime
    x = [datetime.datetime(1943,3, 13,12,0,0),
    datetime.datetime(1943,3, 13,12,5,0),
    datetime.datetime(1943,3, 13,12,10,0),
    datetime.datetime(1943,3, 13,12,15,0),
    datetime.datetime(1943,3, 13,12,20,0),
    datetime.datetime(1943,3, 13,12,25,0),
    datetime.datetime(1943,3, 13,12,30,0),
    datetime.datetime(1943,3, 13,12,35,0)]
    y = [1,2,3,4,2,1,3,4]

    # plot the data out but does not provide sufficient detail on the lower values
    plt.figure()
    plt.bar(x,y)

    # plot the data out but ommit the datetime information
    plt.figure()
    plt.bar(range(0,len(x)),y)


    Hello guys, I am just starting with the matplotlib in transition from matlab to python. However, I encountered weird behavior of matplotlib as it is not able to display the data along with the datetime element.
    My question here would be the output of both bar plot yield two different results.



    enter image description here



    The first one directly convert the data into some kind of continuous data where as the second one more like categorical data. Do anyone encountered similar problem as mine and dont mind share their way of approaching this?



    P/s: i tried seaborn and it works but somehow does not play well with dual axis plotting. I also googled for similar issue but somehow not such issue?










    share|improve this question



























      0












      0








      0







      import matplotlib.pyplot as plt
      import datetime
      x = [datetime.datetime(1943,3, 13,12,0,0),
      datetime.datetime(1943,3, 13,12,5,0),
      datetime.datetime(1943,3, 13,12,10,0),
      datetime.datetime(1943,3, 13,12,15,0),
      datetime.datetime(1943,3, 13,12,20,0),
      datetime.datetime(1943,3, 13,12,25,0),
      datetime.datetime(1943,3, 13,12,30,0),
      datetime.datetime(1943,3, 13,12,35,0)]
      y = [1,2,3,4,2,1,3,4]

      # plot the data out but does not provide sufficient detail on the lower values
      plt.figure()
      plt.bar(x,y)

      # plot the data out but ommit the datetime information
      plt.figure()
      plt.bar(range(0,len(x)),y)


      Hello guys, I am just starting with the matplotlib in transition from matlab to python. However, I encountered weird behavior of matplotlib as it is not able to display the data along with the datetime element.
      My question here would be the output of both bar plot yield two different results.



      enter image description here



      The first one directly convert the data into some kind of continuous data where as the second one more like categorical data. Do anyone encountered similar problem as mine and dont mind share their way of approaching this?



      P/s: i tried seaborn and it works but somehow does not play well with dual axis plotting. I also googled for similar issue but somehow not such issue?










      share|improve this question















      import matplotlib.pyplot as plt
      import datetime
      x = [datetime.datetime(1943,3, 13,12,0,0),
      datetime.datetime(1943,3, 13,12,5,0),
      datetime.datetime(1943,3, 13,12,10,0),
      datetime.datetime(1943,3, 13,12,15,0),
      datetime.datetime(1943,3, 13,12,20,0),
      datetime.datetime(1943,3, 13,12,25,0),
      datetime.datetime(1943,3, 13,12,30,0),
      datetime.datetime(1943,3, 13,12,35,0)]
      y = [1,2,3,4,2,1,3,4]

      # plot the data out but does not provide sufficient detail on the lower values
      plt.figure()
      plt.bar(x,y)

      # plot the data out but ommit the datetime information
      plt.figure()
      plt.bar(range(0,len(x)),y)


      Hello guys, I am just starting with the matplotlib in transition from matlab to python. However, I encountered weird behavior of matplotlib as it is not able to display the data along with the datetime element.
      My question here would be the output of both bar plot yield two different results.



      enter image description here



      The first one directly convert the data into some kind of continuous data where as the second one more like categorical data. Do anyone encountered similar problem as mine and dont mind share their way of approaching this?



      P/s: i tried seaborn and it works but somehow does not play well with dual axis plotting. I also googled for similar issue but somehow not such issue?







      python datetime matplotlib bar-chart visualize






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 3:38









      CozyAzure

      5,33041836




      5,33041836










      asked Nov 21 at 3:31









      Billy Lau

      31




      31
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I'm not sure if I would call the observed behaviour unexpected. In the first case you provide dates to the x variable of the bar plot, hence it will plot the bars at those dates. In the second case you provide some numbers to the x variable, hence it will plot the numbers.



          Since you didn't tell which of those you actually prefer, a solution is to make them both equal visually. Still, the respective concept is different.



          import matplotlib.pyplot as plt
          from matplotlib.dates import DateFormatter
          import datetime
          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,10,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,20,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0)]
          y = [1,2,3,4,2,1,3,4]

          # plot numeric plot
          plt.figure()
          plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
          plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

          # Plot categorical plot
          plt.figure()
          plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
          plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

          plt.show()


          enter image description here



          The difference between the concepts would however be more clearly observable when using different data,



          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0),
          datetime.datetime(1943,3, 13,12,45,0),
          datetime.datetime(1943,3, 13,12,50,0)]


          enter image description here






          share|improve this answer























          • A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
            – Patol75
            Nov 21 at 4:34










          • I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
            – ImportanceOfBeingErnest
            Nov 21 at 4:41










          • Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
            – Billy Lau
            Nov 21 at 4:46












          • The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
            – ImportanceOfBeingErnest
            Nov 21 at 5:00










          • i see. Thanks for clarifying!
            – Billy Lau
            Nov 21 at 5:42



















          0














          I'm not sure about how to fix the problems with matplotlib and datetime, but pandas handles datetime objects very well. You can consider it. You can do, for example, the following:



          import pandas as pd
          df = pd.DataFrame({'date': x, 'value': y})
          df.set_index('date').plot.bar()
          plt.show()


          pandas result



          And improvements are pretty easy to do too:



          df = pd.DataFrame({'date': x, 'value': y})
          df['date'] = df['date'].dt.time
          df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
          plt.show()


          Image 2






          share|improve this answer





















          • "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
            – ImportanceOfBeingErnest
            Nov 21 at 4:06











          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',
          autoActivateHeartbeat: false,
          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%2f53404873%2fweird-behavior-of-barplot-from-python-matplotlib-with-datetime%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









          1














          I'm not sure if I would call the observed behaviour unexpected. In the first case you provide dates to the x variable of the bar plot, hence it will plot the bars at those dates. In the second case you provide some numbers to the x variable, hence it will plot the numbers.



          Since you didn't tell which of those you actually prefer, a solution is to make them both equal visually. Still, the respective concept is different.



          import matplotlib.pyplot as plt
          from matplotlib.dates import DateFormatter
          import datetime
          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,10,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,20,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0)]
          y = [1,2,3,4,2,1,3,4]

          # plot numeric plot
          plt.figure()
          plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
          plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

          # Plot categorical plot
          plt.figure()
          plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
          plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

          plt.show()


          enter image description here



          The difference between the concepts would however be more clearly observable when using different data,



          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0),
          datetime.datetime(1943,3, 13,12,45,0),
          datetime.datetime(1943,3, 13,12,50,0)]


          enter image description here






          share|improve this answer























          • A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
            – Patol75
            Nov 21 at 4:34










          • I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
            – ImportanceOfBeingErnest
            Nov 21 at 4:41










          • Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
            – Billy Lau
            Nov 21 at 4:46












          • The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
            – ImportanceOfBeingErnest
            Nov 21 at 5:00










          • i see. Thanks for clarifying!
            – Billy Lau
            Nov 21 at 5:42
















          1














          I'm not sure if I would call the observed behaviour unexpected. In the first case you provide dates to the x variable of the bar plot, hence it will plot the bars at those dates. In the second case you provide some numbers to the x variable, hence it will plot the numbers.



          Since you didn't tell which of those you actually prefer, a solution is to make them both equal visually. Still, the respective concept is different.



          import matplotlib.pyplot as plt
          from matplotlib.dates import DateFormatter
          import datetime
          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,10,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,20,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0)]
          y = [1,2,3,4,2,1,3,4]

          # plot numeric plot
          plt.figure()
          plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
          plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

          # Plot categorical plot
          plt.figure()
          plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
          plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

          plt.show()


          enter image description here



          The difference between the concepts would however be more clearly observable when using different data,



          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0),
          datetime.datetime(1943,3, 13,12,45,0),
          datetime.datetime(1943,3, 13,12,50,0)]


          enter image description here






          share|improve this answer























          • A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
            – Patol75
            Nov 21 at 4:34










          • I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
            – ImportanceOfBeingErnest
            Nov 21 at 4:41










          • Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
            – Billy Lau
            Nov 21 at 4:46












          • The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
            – ImportanceOfBeingErnest
            Nov 21 at 5:00










          • i see. Thanks for clarifying!
            – Billy Lau
            Nov 21 at 5:42














          1












          1








          1






          I'm not sure if I would call the observed behaviour unexpected. In the first case you provide dates to the x variable of the bar plot, hence it will plot the bars at those dates. In the second case you provide some numbers to the x variable, hence it will plot the numbers.



          Since you didn't tell which of those you actually prefer, a solution is to make them both equal visually. Still, the respective concept is different.



          import matplotlib.pyplot as plt
          from matplotlib.dates import DateFormatter
          import datetime
          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,10,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,20,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0)]
          y = [1,2,3,4,2,1,3,4]

          # plot numeric plot
          plt.figure()
          plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
          plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

          # Plot categorical plot
          plt.figure()
          plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
          plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

          plt.show()


          enter image description here



          The difference between the concepts would however be more clearly observable when using different data,



          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0),
          datetime.datetime(1943,3, 13,12,45,0),
          datetime.datetime(1943,3, 13,12,50,0)]


          enter image description here






          share|improve this answer














          I'm not sure if I would call the observed behaviour unexpected. In the first case you provide dates to the x variable of the bar plot, hence it will plot the bars at those dates. In the second case you provide some numbers to the x variable, hence it will plot the numbers.



          Since you didn't tell which of those you actually prefer, a solution is to make them both equal visually. Still, the respective concept is different.



          import matplotlib.pyplot as plt
          from matplotlib.dates import DateFormatter
          import datetime
          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,10,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,20,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0)]
          y = [1,2,3,4,2,1,3,4]

          # plot numeric plot
          plt.figure()
          plt.bar(x,y, width=4./24/60) # 4 minutes wide bars
          plt.gca().xaxis.set_major_formatter(DateFormatter("%H:%M"))

          # Plot categorical plot
          plt.figure()
          plt.bar(range(0,len(x)),y, width=0.8) # 0.8 units wide bars
          plt.xticks(range(0,len(x)), [d.strftime("%H:%M") for d in x])

          plt.show()


          enter image description here



          The difference between the concepts would however be more clearly observable when using different data,



          x = [datetime.datetime(1943,3, 13,12,0,0),
          datetime.datetime(1943,3, 13,12,5,0),
          datetime.datetime(1943,3, 13,12,15,0),
          datetime.datetime(1943,3, 13,12,25,0),
          datetime.datetime(1943,3, 13,12,30,0),
          datetime.datetime(1943,3, 13,12,35,0),
          datetime.datetime(1943,3, 13,12,45,0),
          datetime.datetime(1943,3, 13,12,50,0)]


          enter image description here







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 at 4:26

























          answered Nov 21 at 4:21









          ImportanceOfBeingErnest

          125k10127204




          125k10127204












          • A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
            – Patol75
            Nov 21 at 4:34










          • I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
            – ImportanceOfBeingErnest
            Nov 21 at 4:41










          • Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
            – Billy Lau
            Nov 21 at 4:46












          • The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
            – ImportanceOfBeingErnest
            Nov 21 at 5:00










          • i see. Thanks for clarifying!
            – Billy Lau
            Nov 21 at 5:42


















          • A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
            – Patol75
            Nov 21 at 4:34










          • I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
            – ImportanceOfBeingErnest
            Nov 21 at 4:41










          • Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
            – Billy Lau
            Nov 21 at 4:46












          • The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
            – ImportanceOfBeingErnest
            Nov 21 at 5:00










          • i see. Thanks for clarifying!
            – Billy Lau
            Nov 21 at 5:42
















          A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
          – Patol75
          Nov 21 at 4:34




          A small addition for the OP, the formats accepted by DateFormatter can be found here (docs.python.org/3/library/…).
          – Patol75
          Nov 21 at 4:34












          I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
          – ImportanceOfBeingErnest
          Nov 21 at 4:41




          I think the relation between matlab datetime formats ('HH:MM') and python ('%H:%M') is pretty obvious in this case, but thanks for then link anyways.
          – ImportanceOfBeingErnest
          Nov 21 at 4:41












          Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
          – Billy Lau
          Nov 21 at 4:46






          Thanks. I guess the keyword here would be the "width" of datetime. I did not realize it plays a part in determining whether the bar is continuous or discrete. I guess i do get a little bit confused about the datetime format in python. Thanks again for pointing out!
          – Billy Lau
          Nov 21 at 4:46














          The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
          – ImportanceOfBeingErnest
          Nov 21 at 5:00




          The width does not play a part in determining the axis units. It's really x itself that determines the units. If those are datetimes, the units are different than if they are integers starting at 0.
          – ImportanceOfBeingErnest
          Nov 21 at 5:00












          i see. Thanks for clarifying!
          – Billy Lau
          Nov 21 at 5:42




          i see. Thanks for clarifying!
          – Billy Lau
          Nov 21 at 5:42













          0














          I'm not sure about how to fix the problems with matplotlib and datetime, but pandas handles datetime objects very well. You can consider it. You can do, for example, the following:



          import pandas as pd
          df = pd.DataFrame({'date': x, 'value': y})
          df.set_index('date').plot.bar()
          plt.show()


          pandas result



          And improvements are pretty easy to do too:



          df = pd.DataFrame({'date': x, 'value': y})
          df['date'] = df['date'].dt.time
          df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
          plt.show()


          Image 2






          share|improve this answer





















          • "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
            – ImportanceOfBeingErnest
            Nov 21 at 4:06
















          0














          I'm not sure about how to fix the problems with matplotlib and datetime, but pandas handles datetime objects very well. You can consider it. You can do, for example, the following:



          import pandas as pd
          df = pd.DataFrame({'date': x, 'value': y})
          df.set_index('date').plot.bar()
          plt.show()


          pandas result



          And improvements are pretty easy to do too:



          df = pd.DataFrame({'date': x, 'value': y})
          df['date'] = df['date'].dt.time
          df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
          plt.show()


          Image 2






          share|improve this answer





















          • "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
            – ImportanceOfBeingErnest
            Nov 21 at 4:06














          0












          0








          0






          I'm not sure about how to fix the problems with matplotlib and datetime, but pandas handles datetime objects very well. You can consider it. You can do, for example, the following:



          import pandas as pd
          df = pd.DataFrame({'date': x, 'value': y})
          df.set_index('date').plot.bar()
          plt.show()


          pandas result



          And improvements are pretty easy to do too:



          df = pd.DataFrame({'date': x, 'value': y})
          df['date'] = df['date'].dt.time
          df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
          plt.show()


          Image 2






          share|improve this answer












          I'm not sure about how to fix the problems with matplotlib and datetime, but pandas handles datetime objects very well. You can consider it. You can do, for example, the following:



          import pandas as pd
          df = pd.DataFrame({'date': x, 'value': y})
          df.set_index('date').plot.bar()
          plt.show()


          pandas result



          And improvements are pretty easy to do too:



          df = pd.DataFrame({'date': x, 'value': y})
          df['date'] = df['date'].dt.time
          df.set_index('date').plot.bar(rot=0, figsize=(10, 5), alpha=0.7)
          plt.show()


          Image 2







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 3:44









          Julian Peller

          849511




          849511












          • "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
            – ImportanceOfBeingErnest
            Nov 21 at 4:06


















          • "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
            – ImportanceOfBeingErnest
            Nov 21 at 4:06
















          "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
          – ImportanceOfBeingErnest
          Nov 21 at 4:06




          "but pandas handles datetime objects very well" - Note that pandas in this case does not use datetime objects as real dates. You would see that if using unequal spacings between the dates. They would still be equally spaced on the axis.
          – ImportanceOfBeingErnest
          Nov 21 at 4:06


















          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%2f53404873%2fweird-behavior-of-barplot-from-python-matplotlib-with-datetime%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'