Dates shown on plot x axis












0















I am working a dataset that is quite big. It's a file that has 8 columns (y1-y8) with number data (e.g. 21.3456) and 1 column (x) with date data (format: m/d/Y).



I want to contrast the 8 columns over time and a plot diagram seemed the best option for that.



This is my code:



import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.cbook as cbook
import matplotlib.dates as mdates
import datetime
from matplotlib.dates import DateFormatter

%matplotlib inline

df = pd.read_excel('Masterfile_allSTW.xlsx', 'Sheet1')

#data
#df.TS_TIMESTAMP = df.TS_TIMESTAMP.astype(str)
x = df['dates']
y1 = df['Rtemp_EG']
y2 = df['Rtemp_EG+1.OG']
y3 = df['Rtemp_1.OG']
y4 = df['Rtemp_2.OG']
y5 = df['Rtemp_3.OG']
y6 = df['Rtemp_4.OG']
y7 = df['Rtemp_5.OG']
y8 = df['Rtemp_6.OG']

#plot data
fig, ax = plt.subplots(figsize=(15,7))
df.plot(ax=ax)

#set ticks every week
ax.xaxis.set_major_locator(mdates.DayLocator())
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))


Running the script the plot looks like this:



enter image description here



I am quite satisfied with the look of the plot diagram, the only thing that is missing are the date descriptions on the x axis (which should come from the columns ('dates'). I have a feeling the problem lies within the df.plot(ax=ax) line, but I'm not sure.



Any help is much appreciated!










share|improve this question

























  • Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

    – ImportanceOfBeingErnest
    Nov 15 '18 at 19:10











  • No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

    – Lis011235
    Nov 21 '18 at 8:32













  • Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

    – ImportanceOfBeingErnest
    Nov 21 '18 at 16:11
















0















I am working a dataset that is quite big. It's a file that has 8 columns (y1-y8) with number data (e.g. 21.3456) and 1 column (x) with date data (format: m/d/Y).



I want to contrast the 8 columns over time and a plot diagram seemed the best option for that.



This is my code:



import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.cbook as cbook
import matplotlib.dates as mdates
import datetime
from matplotlib.dates import DateFormatter

%matplotlib inline

df = pd.read_excel('Masterfile_allSTW.xlsx', 'Sheet1')

#data
#df.TS_TIMESTAMP = df.TS_TIMESTAMP.astype(str)
x = df['dates']
y1 = df['Rtemp_EG']
y2 = df['Rtemp_EG+1.OG']
y3 = df['Rtemp_1.OG']
y4 = df['Rtemp_2.OG']
y5 = df['Rtemp_3.OG']
y6 = df['Rtemp_4.OG']
y7 = df['Rtemp_5.OG']
y8 = df['Rtemp_6.OG']

#plot data
fig, ax = plt.subplots(figsize=(15,7))
df.plot(ax=ax)

#set ticks every week
ax.xaxis.set_major_locator(mdates.DayLocator())
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))


Running the script the plot looks like this:



enter image description here



I am quite satisfied with the look of the plot diagram, the only thing that is missing are the date descriptions on the x axis (which should come from the columns ('dates'). I have a feeling the problem lies within the df.plot(ax=ax) line, but I'm not sure.



Any help is much appreciated!










share|improve this question

























  • Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

    – ImportanceOfBeingErnest
    Nov 15 '18 at 19:10











  • No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

    – Lis011235
    Nov 21 '18 at 8:32













  • Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

    – ImportanceOfBeingErnest
    Nov 21 '18 at 16:11














0












0








0








I am working a dataset that is quite big. It's a file that has 8 columns (y1-y8) with number data (e.g. 21.3456) and 1 column (x) with date data (format: m/d/Y).



I want to contrast the 8 columns over time and a plot diagram seemed the best option for that.



This is my code:



import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.cbook as cbook
import matplotlib.dates as mdates
import datetime
from matplotlib.dates import DateFormatter

%matplotlib inline

df = pd.read_excel('Masterfile_allSTW.xlsx', 'Sheet1')

#data
#df.TS_TIMESTAMP = df.TS_TIMESTAMP.astype(str)
x = df['dates']
y1 = df['Rtemp_EG']
y2 = df['Rtemp_EG+1.OG']
y3 = df['Rtemp_1.OG']
y4 = df['Rtemp_2.OG']
y5 = df['Rtemp_3.OG']
y6 = df['Rtemp_4.OG']
y7 = df['Rtemp_5.OG']
y8 = df['Rtemp_6.OG']

#plot data
fig, ax = plt.subplots(figsize=(15,7))
df.plot(ax=ax)

#set ticks every week
ax.xaxis.set_major_locator(mdates.DayLocator())
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))


Running the script the plot looks like this:



enter image description here



I am quite satisfied with the look of the plot diagram, the only thing that is missing are the date descriptions on the x axis (which should come from the columns ('dates'). I have a feeling the problem lies within the df.plot(ax=ax) line, but I'm not sure.



Any help is much appreciated!










share|improve this question
















I am working a dataset that is quite big. It's a file that has 8 columns (y1-y8) with number data (e.g. 21.3456) and 1 column (x) with date data (format: m/d/Y).



I want to contrast the 8 columns over time and a plot diagram seemed the best option for that.



This is my code:



import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.cbook as cbook
import matplotlib.dates as mdates
import datetime
from matplotlib.dates import DateFormatter

%matplotlib inline

df = pd.read_excel('Masterfile_allSTW.xlsx', 'Sheet1')

#data
#df.TS_TIMESTAMP = df.TS_TIMESTAMP.astype(str)
x = df['dates']
y1 = df['Rtemp_EG']
y2 = df['Rtemp_EG+1.OG']
y3 = df['Rtemp_1.OG']
y4 = df['Rtemp_2.OG']
y5 = df['Rtemp_3.OG']
y6 = df['Rtemp_4.OG']
y7 = df['Rtemp_5.OG']
y8 = df['Rtemp_6.OG']

#plot data
fig, ax = plt.subplots(figsize=(15,7))
df.plot(ax=ax)

#set ticks every week
ax.xaxis.set_major_locator(mdates.DayLocator())
#set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))


Running the script the plot looks like this:



enter image description here



I am quite satisfied with the look of the plot diagram, the only thing that is missing are the date descriptions on the x axis (which should come from the columns ('dates'). I have a feeling the problem lies within the df.plot(ax=ax) line, but I'm not sure.



Any help is much appreciated!







python matplotlib plot dataformat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:16









ImportanceOfBeingErnest

138k13158236




138k13158236










asked Nov 15 '18 at 15:59









Lis011235Lis011235

36




36













  • Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

    – ImportanceOfBeingErnest
    Nov 15 '18 at 19:10











  • No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

    – Lis011235
    Nov 21 '18 at 8:32













  • Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

    – ImportanceOfBeingErnest
    Nov 21 '18 at 16:11



















  • Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

    – ImportanceOfBeingErnest
    Nov 15 '18 at 19:10











  • No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

    – Lis011235
    Nov 21 '18 at 8:32













  • Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

    – ImportanceOfBeingErnest
    Nov 21 '18 at 16:11

















Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

– ImportanceOfBeingErnest
Nov 15 '18 at 19:10





Is the dates a column or the index? What's the dtype of the dates? Which version of pandas and matplotlib are in use?

– ImportanceOfBeingErnest
Nov 15 '18 at 19:10













No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

– Lis011235
Nov 21 '18 at 8:32







No, the 'dates' column is the first column after the index and the dtype is datetime64[ns]. I'm not sure which version of pandas and matplotlib I am using - how can I find out? But I'm using Python 3.7.0. on the Jupyter Notebook if this is useful information.

– Lis011235
Nov 21 '18 at 8:32















Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

– ImportanceOfBeingErnest
Nov 21 '18 at 16:11





Does the answer below help in any way? Usually find out versions via print(matplotlib.__version__).

– ImportanceOfBeingErnest
Nov 21 '18 at 16:11












2 Answers
2






active

oldest

votes


















0














Indeed the problem is with df.plot(ax=ax). Try replacing it withdf.plot(x='dates') to plot against a single specific column.






share|improve this answer
























  • Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

    – Lis011235
    Nov 22 '18 at 8:06













  • Glad it helped. Please consider accepting the answer.

    – b-fg
    Nov 22 '18 at 8:10



















0














If the other answer does not help here, I would suggest making the dates column the index for plotting. There seem to be problems if the dates are a column in some cases, but I haven't understood the mechanism of why that would be the case.



df.set_index("dates").plot(ax=ax, x_compat=True)





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%2f53323310%2fdates-shown-on-plot-x-axis%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














    Indeed the problem is with df.plot(ax=ax). Try replacing it withdf.plot(x='dates') to plot against a single specific column.






    share|improve this answer
























    • Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

      – Lis011235
      Nov 22 '18 at 8:06













    • Glad it helped. Please consider accepting the answer.

      – b-fg
      Nov 22 '18 at 8:10
















    0














    Indeed the problem is with df.plot(ax=ax). Try replacing it withdf.plot(x='dates') to plot against a single specific column.






    share|improve this answer
























    • Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

      – Lis011235
      Nov 22 '18 at 8:06













    • Glad it helped. Please consider accepting the answer.

      – b-fg
      Nov 22 '18 at 8:10














    0












    0








    0







    Indeed the problem is with df.plot(ax=ax). Try replacing it withdf.plot(x='dates') to plot against a single specific column.






    share|improve this answer













    Indeed the problem is with df.plot(ax=ax). Try replacing it withdf.plot(x='dates') to plot against a single specific column.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 16:46









    b-fgb-fg

    1,98411624




    1,98411624













    • Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

      – Lis011235
      Nov 22 '18 at 8:06













    • Glad it helped. Please consider accepting the answer.

      – b-fg
      Nov 22 '18 at 8:10



















    • Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

      – Lis011235
      Nov 22 '18 at 8:06













    • Glad it helped. Please consider accepting the answer.

      – b-fg
      Nov 22 '18 at 8:10

















    Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

    – Lis011235
    Nov 22 '18 at 8:06







    Thank you!, Now I there are dates visible on the x-axis, however not in the format that I would like and formulated. ax.xaxis.set_major_formatter(mdates.DateFormatter('%m %d'))

    – Lis011235
    Nov 22 '18 at 8:06















    Glad it helped. Please consider accepting the answer.

    – b-fg
    Nov 22 '18 at 8:10





    Glad it helped. Please consider accepting the answer.

    – b-fg
    Nov 22 '18 at 8:10













    0














    If the other answer does not help here, I would suggest making the dates column the index for plotting. There seem to be problems if the dates are a column in some cases, but I haven't understood the mechanism of why that would be the case.



    df.set_index("dates").plot(ax=ax, x_compat=True)





    share|improve this answer




























      0














      If the other answer does not help here, I would suggest making the dates column the index for plotting. There seem to be problems if the dates are a column in some cases, but I haven't understood the mechanism of why that would be the case.



      df.set_index("dates").plot(ax=ax, x_compat=True)





      share|improve this answer


























        0












        0








        0







        If the other answer does not help here, I would suggest making the dates column the index for plotting. There seem to be problems if the dates are a column in some cases, but I haven't understood the mechanism of why that would be the case.



        df.set_index("dates").plot(ax=ax, x_compat=True)





        share|improve this answer













        If the other answer does not help here, I would suggest making the dates column the index for plotting. There seem to be problems if the dates are a column in some cases, but I haven't understood the mechanism of why that would be the case.



        df.set_index("dates").plot(ax=ax, x_compat=True)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 16:13









        ImportanceOfBeingErnestImportanceOfBeingErnest

        138k13158236




        138k13158236






























            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%2f53323310%2fdates-shown-on-plot-x-axis%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values