Why are ticks not working for date format?












2















I have this simple python code that generates a simple X date y decimal plot.



My goal was to set the ticks to be the one unit less than the minimum TO the one unit plus the maximum, see code and plot below.



I don't see why it would not shows the X tick properly, the Y tick shows just fine.



import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure(facecolor="#979899")
ax = plt.gca()
ax.set_facecolor("#d1d1d1")
ax.set_xlim(pd.to_datetime(["2018-11-18"]),pd.to_datetime(["2018-11-22"]))

plt.grid(True)
plt.title("This is a title",fontsize=16)

plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.xticks(pd.to_datetime(["2018-11-18","2018-11-20","2018-11-22"]))

x1 = pd.to_datetime(["2018-11-19","2018-11-20","2018-11-21"])
y1 = [0.18,0.32,0.21]


for i,item in enumerate(y1):
xP = x1[i]
yP = y1[i]
plt.text(xP,yP,str(item)+"%",fontsize=11)

plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.show()


enter image description hereenter image description here










share|improve this question




















  • 2





    The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

    – jez
    Nov 22 '18 at 23:38











  • oh I totally see that you are saying

    – RollRoll
    Nov 22 '18 at 23:39











  • Why do you want to show ticks outside your data range?

    – Joooeey
    Nov 22 '18 at 23:57











  • If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

    – Joooeey
    Nov 22 '18 at 23:59
















2















I have this simple python code that generates a simple X date y decimal plot.



My goal was to set the ticks to be the one unit less than the minimum TO the one unit plus the maximum, see code and plot below.



I don't see why it would not shows the X tick properly, the Y tick shows just fine.



import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure(facecolor="#979899")
ax = plt.gca()
ax.set_facecolor("#d1d1d1")
ax.set_xlim(pd.to_datetime(["2018-11-18"]),pd.to_datetime(["2018-11-22"]))

plt.grid(True)
plt.title("This is a title",fontsize=16)

plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.xticks(pd.to_datetime(["2018-11-18","2018-11-20","2018-11-22"]))

x1 = pd.to_datetime(["2018-11-19","2018-11-20","2018-11-21"])
y1 = [0.18,0.32,0.21]


for i,item in enumerate(y1):
xP = x1[i]
yP = y1[i]
plt.text(xP,yP,str(item)+"%",fontsize=11)

plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.show()


enter image description hereenter image description here










share|improve this question




















  • 2





    The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

    – jez
    Nov 22 '18 at 23:38











  • oh I totally see that you are saying

    – RollRoll
    Nov 22 '18 at 23:39











  • Why do you want to show ticks outside your data range?

    – Joooeey
    Nov 22 '18 at 23:57











  • If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

    – Joooeey
    Nov 22 '18 at 23:59














2












2








2








I have this simple python code that generates a simple X date y decimal plot.



My goal was to set the ticks to be the one unit less than the minimum TO the one unit plus the maximum, see code and plot below.



I don't see why it would not shows the X tick properly, the Y tick shows just fine.



import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure(facecolor="#979899")
ax = plt.gca()
ax.set_facecolor("#d1d1d1")
ax.set_xlim(pd.to_datetime(["2018-11-18"]),pd.to_datetime(["2018-11-22"]))

plt.grid(True)
plt.title("This is a title",fontsize=16)

plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.xticks(pd.to_datetime(["2018-11-18","2018-11-20","2018-11-22"]))

x1 = pd.to_datetime(["2018-11-19","2018-11-20","2018-11-21"])
y1 = [0.18,0.32,0.21]


for i,item in enumerate(y1):
xP = x1[i]
yP = y1[i]
plt.text(xP,yP,str(item)+"%",fontsize=11)

plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.show()


enter image description hereenter image description here










share|improve this question
















I have this simple python code that generates a simple X date y decimal plot.



My goal was to set the ticks to be the one unit less than the minimum TO the one unit plus the maximum, see code and plot below.



I don't see why it would not shows the X tick properly, the Y tick shows just fine.



import matplotlib.pyplot as plt
import pandas as pd

fig = plt.figure(facecolor="#979899")
ax = plt.gca()
ax.set_facecolor("#d1d1d1")
ax.set_xlim(pd.to_datetime(["2018-11-18"]),pd.to_datetime(["2018-11-22"]))

plt.grid(True)
plt.title("This is a title",fontsize=16)

plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
plt.xticks(pd.to_datetime(["2018-11-18","2018-11-20","2018-11-22"]))

x1 = pd.to_datetime(["2018-11-19","2018-11-20","2018-11-21"])
y1 = [0.18,0.32,0.21]


for i,item in enumerate(y1):
xP = x1[i]
yP = y1[i]
plt.text(xP,yP,str(item)+"%",fontsize=11)

plt.scatter(x1,y1)
plt.plot(x1,y1)
plt.show()


enter image description hereenter image description here







python matplotlib






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 1:53







RollRoll

















asked Nov 22 '18 at 23:24









RollRollRollRoll

3,4211354113




3,4211354113








  • 2





    The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

    – jez
    Nov 22 '18 at 23:38











  • oh I totally see that you are saying

    – RollRoll
    Nov 22 '18 at 23:39











  • Why do you want to show ticks outside your data range?

    – Joooeey
    Nov 22 '18 at 23:57











  • If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

    – Joooeey
    Nov 22 '18 at 23:59














  • 2





    The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

    – jez
    Nov 22 '18 at 23:38











  • oh I totally see that you are saying

    – RollRoll
    Nov 22 '18 at 23:39











  • Why do you want to show ticks outside your data range?

    – Joooeey
    Nov 22 '18 at 23:57











  • If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

    – Joooeey
    Nov 22 '18 at 23:59








2




2





The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

– jez
Nov 22 '18 at 23:38





The answer to the "why" of it is: matplotlib simply does not know that you intend the x-axis numbers to be treated as datestamps. By default Python, and hence matplotlib, assumes that your 8-digit literals are decimal numbers in the neighborhood of twenty million. If your question is really "how do I plot with datestamps on the x axis?" then a google search for matplotlib date tick labels can get you to the following example code from the matplotlib project's own gallery: matplotlib.org/gallery/api/date.html

– jez
Nov 22 '18 at 23:38













oh I totally see that you are saying

– RollRoll
Nov 22 '18 at 23:39





oh I totally see that you are saying

– RollRoll
Nov 22 '18 at 23:39













Why do you want to show ticks outside your data range?

– Joooeey
Nov 22 '18 at 23:57





Why do you want to show ticks outside your data range?

– Joooeey
Nov 22 '18 at 23:57













If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

– Joooeey
Nov 22 '18 at 23:59





If you just want a quick and dirty graph, you could use strings on the x-axis x1 = ['20181120', '20181121', '20181122']. But that will only work if the dates are increasing and all one day apart.

– Joooeey
Nov 22 '18 at 23:59












2 Answers
2






active

oldest

votes


















0














You provide integers for the dates. So Matplotlib treats it like any number. And it tries to be smart about large numbers that are close together:




The default formatter identifies when the x-data being plotted is a
small range on top of a large off set. To reduce the chances that the
ticklabels overlap the ticks are labeled as deltas from a fixed
offset. For example:



ax.plot(np.arange(2000, 2010), range(10)) will have tick of 0-9 with
an offset of +2e3. If this is not desired turn off the use of the
offset on the default formatter:



ax.get_xaxis().get_major_formatter().set_useOffset(False) set the
rcParam axes.formatter.useoffset=False to turn it off globally, or set
a different formatter.
This is from the docs of the matplotlib.ticker module.







share|improve this answer































    0














    I solved the issue by converting to datetime object. Find solution below:



    import matplotlib.pyplot as plt
    import datetime

    fig = plt.figure(facecolor="#979899")
    ax = plt.gca()
    ax.set_facecolor("#d1d1d1")
    plt.grid(True)
    plt.title("This is a title",fontsize=16)

    plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
    plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])

    x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
    y1 = [0.18,0.32,0.21]

    for i,item in enumerate(y1):
    xP = x1[i]
    yP = y1[i]
    plt.text(xP,yP,str(item)+"%",fontsize=11)

    plt.plot(x1,y1)
    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',
      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%2f53439034%2fwhy-are-ticks-not-working-for-date-format%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









      0














      You provide integers for the dates. So Matplotlib treats it like any number. And it tries to be smart about large numbers that are close together:




      The default formatter identifies when the x-data being plotted is a
      small range on top of a large off set. To reduce the chances that the
      ticklabels overlap the ticks are labeled as deltas from a fixed
      offset. For example:



      ax.plot(np.arange(2000, 2010), range(10)) will have tick of 0-9 with
      an offset of +2e3. If this is not desired turn off the use of the
      offset on the default formatter:



      ax.get_xaxis().get_major_formatter().set_useOffset(False) set the
      rcParam axes.formatter.useoffset=False to turn it off globally, or set
      a different formatter.
      This is from the docs of the matplotlib.ticker module.







      share|improve this answer




























        0














        You provide integers for the dates. So Matplotlib treats it like any number. And it tries to be smart about large numbers that are close together:




        The default formatter identifies when the x-data being plotted is a
        small range on top of a large off set. To reduce the chances that the
        ticklabels overlap the ticks are labeled as deltas from a fixed
        offset. For example:



        ax.plot(np.arange(2000, 2010), range(10)) will have tick of 0-9 with
        an offset of +2e3. If this is not desired turn off the use of the
        offset on the default formatter:



        ax.get_xaxis().get_major_formatter().set_useOffset(False) set the
        rcParam axes.formatter.useoffset=False to turn it off globally, or set
        a different formatter.
        This is from the docs of the matplotlib.ticker module.







        share|improve this answer


























          0












          0








          0







          You provide integers for the dates. So Matplotlib treats it like any number. And it tries to be smart about large numbers that are close together:




          The default formatter identifies when the x-data being plotted is a
          small range on top of a large off set. To reduce the chances that the
          ticklabels overlap the ticks are labeled as deltas from a fixed
          offset. For example:



          ax.plot(np.arange(2000, 2010), range(10)) will have tick of 0-9 with
          an offset of +2e3. If this is not desired turn off the use of the
          offset on the default formatter:



          ax.get_xaxis().get_major_formatter().set_useOffset(False) set the
          rcParam axes.formatter.useoffset=False to turn it off globally, or set
          a different formatter.
          This is from the docs of the matplotlib.ticker module.







          share|improve this answer













          You provide integers for the dates. So Matplotlib treats it like any number. And it tries to be smart about large numbers that are close together:




          The default formatter identifies when the x-data being plotted is a
          small range on top of a large off set. To reduce the chances that the
          ticklabels overlap the ticks are labeled as deltas from a fixed
          offset. For example:



          ax.plot(np.arange(2000, 2010), range(10)) will have tick of 0-9 with
          an offset of +2e3. If this is not desired turn off the use of the
          offset on the default formatter:



          ax.get_xaxis().get_major_formatter().set_useOffset(False) set the
          rcParam axes.formatter.useoffset=False to turn it off globally, or set
          a different formatter.
          This is from the docs of the matplotlib.ticker module.








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 0:04









          JoooeeyJoooeey

          632818




          632818

























              0














              I solved the issue by converting to datetime object. Find solution below:



              import matplotlib.pyplot as plt
              import datetime

              fig = plt.figure(facecolor="#979899")
              ax = plt.gca()
              ax.set_facecolor("#d1d1d1")
              plt.grid(True)
              plt.title("This is a title",fontsize=16)

              plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
              plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])

              x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
              y1 = [0.18,0.32,0.21]

              for i,item in enumerate(y1):
              xP = x1[i]
              yP = y1[i]
              plt.text(xP,yP,str(item)+"%",fontsize=11)

              plt.plot(x1,y1)
              plt.show()





              share|improve this answer




























                0














                I solved the issue by converting to datetime object. Find solution below:



                import matplotlib.pyplot as plt
                import datetime

                fig = plt.figure(facecolor="#979899")
                ax = plt.gca()
                ax.set_facecolor("#d1d1d1")
                plt.grid(True)
                plt.title("This is a title",fontsize=16)

                plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
                plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])

                x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
                y1 = [0.18,0.32,0.21]

                for i,item in enumerate(y1):
                xP = x1[i]
                yP = y1[i]
                plt.text(xP,yP,str(item)+"%",fontsize=11)

                plt.plot(x1,y1)
                plt.show()





                share|improve this answer


























                  0












                  0








                  0







                  I solved the issue by converting to datetime object. Find solution below:



                  import matplotlib.pyplot as plt
                  import datetime

                  fig = plt.figure(facecolor="#979899")
                  ax = plt.gca()
                  ax.set_facecolor("#d1d1d1")
                  plt.grid(True)
                  plt.title("This is a title",fontsize=16)

                  plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
                  plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])

                  x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
                  y1 = [0.18,0.32,0.21]

                  for i,item in enumerate(y1):
                  xP = x1[i]
                  yP = y1[i]
                  plt.text(xP,yP,str(item)+"%",fontsize=11)

                  plt.plot(x1,y1)
                  plt.show()





                  share|improve this answer













                  I solved the issue by converting to datetime object. Find solution below:



                  import matplotlib.pyplot as plt
                  import datetime

                  fig = plt.figure(facecolor="#979899")
                  ax = plt.gca()
                  ax.set_facecolor("#d1d1d1")
                  plt.grid(True)
                  plt.title("This is a title",fontsize=16)

                  plt.xticks([datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)],["11/20","11/21","11/22"])
                  plt.yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0],["0.0","0.1","0.2","0.3","0.4","0.5","0.6","0.7","0.8","0.9","1.0"])

                  x1 = [datetime.date(2018,11,20),datetime.date(2018,11,21),datetime.date(2018,11,22)]
                  y1 = [0.18,0.32,0.21]

                  for i,item in enumerate(y1):
                  xP = x1[i]
                  yP = y1[i]
                  plt.text(xP,yP,str(item)+"%",fontsize=11)

                  plt.plot(x1,y1)
                  plt.show()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 3:02









                  RollRollRollRoll

                  3,4211354113




                  3,4211354113






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439034%2fwhy-are-ticks-not-working-for-date-format%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







                      這個網誌中的熱門文章

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Xamarin.form Move up view when keyboard appear

                      JBPM : POST request for execute process go wrong