Difference in plotting with different matplotlib versions











up vote
6
down vote

favorite
3












A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to do with the version of Matplotlib.



The script that does the plotting of the data is quite short:



import matplotlib.pyplot as plt
import csv
import os
from dateutil import parser

def plot(outputDir,plotsDir,FS):
allfiles = os.listdir(outputDir)
flist =
for f in allfiles:
if 'csv' in f.lower(): flist.append(f)
for f in flist:
with open(outputDir + '/' + f, 'rt') as ff:
data = list(csv.reader(ff,delimiter=FS))
values = [i[2] for i in data[1::]]
values = ['NaN' if v is '' else v for v in values]
time = [parser.parse(i[1]) for i in data[1::]]
plt.xlabel('Time_[UTC]')
plt.plot(time, values)
plt.xticks(rotation=40)
if os.path.isdir(plotsDir) != 1:
os.mkdir(plotsDir, 777)
plt.savefig('{}/{}_Data.png'.format(plotsDir, f[:-4]), bbox_inches='tight', dpi=160)
plt.clf()


outputdir = 'C:/Users/matthijsk/Documents/Test'
plotsdir = outputdir + '/plots'
fs = ','
plot(outputdir, plotsdir, fs)


When I run it using Matplotlib version 2.1.0, my image looks like this:
Matplotlib version 2.1.0
When I run it using Matplotlib version 2.0.2, it looks the way it is supposed to:
Matplotlib version 2.0.2



The file the script is reading looks like this:



stationNo,dtg(UTC),TT_[°C],source_TT,quality_TT
10381,2017-01-01 00:00:00,3.0,ob,na
10381,2017-01-01 01:00:00,3.0,ob,na
10381,2017-01-01 02:00:00,2.4,ob,na
10381,2017-01-01 03:00:00,2.5,ob,na
10381,2017-01-01 04:00:00,2.5,ob,na
10381,2017-01-01 05:00:00,2.3,ob,na
10381,2017-01-01 06:00:00,1.9,ob,na
10381,2017-01-01 07:00:00,1.0,ob,na
10381,2017-01-01 08:00:00,0.1,ob,na
10381,2017-01-01 09:00:00,0.9,ob,na


Can anyone explain me what was changed in Matplotlib that caused this? And apparently I'm doing something wrong with the plotting that is causing this. Can anyone notice a mistake?
I've already tried using



values = [float(value) if value.isnumeric() else None for value in values]


But that didn't solve it.
Note: I'd rather not use any non-standard packages (like Pandas) since it's quite a hassle to get approvement to install such packages.










share|improve this question


























    up vote
    6
    down vote

    favorite
    3












    A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to do with the version of Matplotlib.



    The script that does the plotting of the data is quite short:



    import matplotlib.pyplot as plt
    import csv
    import os
    from dateutil import parser

    def plot(outputDir,plotsDir,FS):
    allfiles = os.listdir(outputDir)
    flist =
    for f in allfiles:
    if 'csv' in f.lower(): flist.append(f)
    for f in flist:
    with open(outputDir + '/' + f, 'rt') as ff:
    data = list(csv.reader(ff,delimiter=FS))
    values = [i[2] for i in data[1::]]
    values = ['NaN' if v is '' else v for v in values]
    time = [parser.parse(i[1]) for i in data[1::]]
    plt.xlabel('Time_[UTC]')
    plt.plot(time, values)
    plt.xticks(rotation=40)
    if os.path.isdir(plotsDir) != 1:
    os.mkdir(plotsDir, 777)
    plt.savefig('{}/{}_Data.png'.format(plotsDir, f[:-4]), bbox_inches='tight', dpi=160)
    plt.clf()


    outputdir = 'C:/Users/matthijsk/Documents/Test'
    plotsdir = outputdir + '/plots'
    fs = ','
    plot(outputdir, plotsdir, fs)


    When I run it using Matplotlib version 2.1.0, my image looks like this:
    Matplotlib version 2.1.0
    When I run it using Matplotlib version 2.0.2, it looks the way it is supposed to:
    Matplotlib version 2.0.2



    The file the script is reading looks like this:



    stationNo,dtg(UTC),TT_[°C],source_TT,quality_TT
    10381,2017-01-01 00:00:00,3.0,ob,na
    10381,2017-01-01 01:00:00,3.0,ob,na
    10381,2017-01-01 02:00:00,2.4,ob,na
    10381,2017-01-01 03:00:00,2.5,ob,na
    10381,2017-01-01 04:00:00,2.5,ob,na
    10381,2017-01-01 05:00:00,2.3,ob,na
    10381,2017-01-01 06:00:00,1.9,ob,na
    10381,2017-01-01 07:00:00,1.0,ob,na
    10381,2017-01-01 08:00:00,0.1,ob,na
    10381,2017-01-01 09:00:00,0.9,ob,na


    Can anyone explain me what was changed in Matplotlib that caused this? And apparently I'm doing something wrong with the plotting that is causing this. Can anyone notice a mistake?
    I've already tried using



    values = [float(value) if value.isnumeric() else None for value in values]


    But that didn't solve it.
    Note: I'd rather not use any non-standard packages (like Pandas) since it's quite a hassle to get approvement to install such packages.










    share|improve this question
























      up vote
      6
      down vote

      favorite
      3









      up vote
      6
      down vote

      favorite
      3






      3





      A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to do with the version of Matplotlib.



      The script that does the plotting of the data is quite short:



      import matplotlib.pyplot as plt
      import csv
      import os
      from dateutil import parser

      def plot(outputDir,plotsDir,FS):
      allfiles = os.listdir(outputDir)
      flist =
      for f in allfiles:
      if 'csv' in f.lower(): flist.append(f)
      for f in flist:
      with open(outputDir + '/' + f, 'rt') as ff:
      data = list(csv.reader(ff,delimiter=FS))
      values = [i[2] for i in data[1::]]
      values = ['NaN' if v is '' else v for v in values]
      time = [parser.parse(i[1]) for i in data[1::]]
      plt.xlabel('Time_[UTC]')
      plt.plot(time, values)
      plt.xticks(rotation=40)
      if os.path.isdir(plotsDir) != 1:
      os.mkdir(plotsDir, 777)
      plt.savefig('{}/{}_Data.png'.format(plotsDir, f[:-4]), bbox_inches='tight', dpi=160)
      plt.clf()


      outputdir = 'C:/Users/matthijsk/Documents/Test'
      plotsdir = outputdir + '/plots'
      fs = ','
      plot(outputdir, plotsdir, fs)


      When I run it using Matplotlib version 2.1.0, my image looks like this:
      Matplotlib version 2.1.0
      When I run it using Matplotlib version 2.0.2, it looks the way it is supposed to:
      Matplotlib version 2.0.2



      The file the script is reading looks like this:



      stationNo,dtg(UTC),TT_[°C],source_TT,quality_TT
      10381,2017-01-01 00:00:00,3.0,ob,na
      10381,2017-01-01 01:00:00,3.0,ob,na
      10381,2017-01-01 02:00:00,2.4,ob,na
      10381,2017-01-01 03:00:00,2.5,ob,na
      10381,2017-01-01 04:00:00,2.5,ob,na
      10381,2017-01-01 05:00:00,2.3,ob,na
      10381,2017-01-01 06:00:00,1.9,ob,na
      10381,2017-01-01 07:00:00,1.0,ob,na
      10381,2017-01-01 08:00:00,0.1,ob,na
      10381,2017-01-01 09:00:00,0.9,ob,na


      Can anyone explain me what was changed in Matplotlib that caused this? And apparently I'm doing something wrong with the plotting that is causing this. Can anyone notice a mistake?
      I've already tried using



      values = [float(value) if value.isnumeric() else None for value in values]


      But that didn't solve it.
      Note: I'd rather not use any non-standard packages (like Pandas) since it's quite a hassle to get approvement to install such packages.










      share|improve this question













      A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to do with the version of Matplotlib.



      The script that does the plotting of the data is quite short:



      import matplotlib.pyplot as plt
      import csv
      import os
      from dateutil import parser

      def plot(outputDir,plotsDir,FS):
      allfiles = os.listdir(outputDir)
      flist =
      for f in allfiles:
      if 'csv' in f.lower(): flist.append(f)
      for f in flist:
      with open(outputDir + '/' + f, 'rt') as ff:
      data = list(csv.reader(ff,delimiter=FS))
      values = [i[2] for i in data[1::]]
      values = ['NaN' if v is '' else v for v in values]
      time = [parser.parse(i[1]) for i in data[1::]]
      plt.xlabel('Time_[UTC]')
      plt.plot(time, values)
      plt.xticks(rotation=40)
      if os.path.isdir(plotsDir) != 1:
      os.mkdir(plotsDir, 777)
      plt.savefig('{}/{}_Data.png'.format(plotsDir, f[:-4]), bbox_inches='tight', dpi=160)
      plt.clf()


      outputdir = 'C:/Users/matthijsk/Documents/Test'
      plotsdir = outputdir + '/plots'
      fs = ','
      plot(outputdir, plotsdir, fs)


      When I run it using Matplotlib version 2.1.0, my image looks like this:
      Matplotlib version 2.1.0
      When I run it using Matplotlib version 2.0.2, it looks the way it is supposed to:
      Matplotlib version 2.0.2



      The file the script is reading looks like this:



      stationNo,dtg(UTC),TT_[°C],source_TT,quality_TT
      10381,2017-01-01 00:00:00,3.0,ob,na
      10381,2017-01-01 01:00:00,3.0,ob,na
      10381,2017-01-01 02:00:00,2.4,ob,na
      10381,2017-01-01 03:00:00,2.5,ob,na
      10381,2017-01-01 04:00:00,2.5,ob,na
      10381,2017-01-01 05:00:00,2.3,ob,na
      10381,2017-01-01 06:00:00,1.9,ob,na
      10381,2017-01-01 07:00:00,1.0,ob,na
      10381,2017-01-01 08:00:00,0.1,ob,na
      10381,2017-01-01 09:00:00,0.9,ob,na


      Can anyone explain me what was changed in Matplotlib that caused this? And apparently I'm doing something wrong with the plotting that is causing this. Can anyone notice a mistake?
      I've already tried using



      values = [float(value) if value.isnumeric() else None for value in values]


      But that didn't solve it.
      Note: I'd rather not use any non-standard packages (like Pandas) since it's quite a hassle to get approvement to install such packages.







      python matplotlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 '17 at 10:27









      Matthijs Kramer

      707




      707
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.



          In matplotlib 2.1, categorical plots have been introduced. This now allows for something like



          plt.plot(["apple", "banana", "cherry"], [2,1,3])


          While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.



          In this case you would want to do this conversion like



          values = [None if v is '' else float(v) for v in values]





          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%2f47155569%2fdifference-in-plotting-with-different-matplotlib-versions%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
            10
            down vote



            accepted










            The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.



            In matplotlib 2.1, categorical plots have been introduced. This now allows for something like



            plt.plot(["apple", "banana", "cherry"], [2,1,3])


            While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.



            In this case you would want to do this conversion like



            values = [None if v is '' else float(v) for v in values]





            share|improve this answer



























              up vote
              10
              down vote



              accepted










              The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.



              In matplotlib 2.1, categorical plots have been introduced. This now allows for something like



              plt.plot(["apple", "banana", "cherry"], [2,1,3])


              While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.



              In this case you would want to do this conversion like



              values = [None if v is '' else float(v) for v in values]





              share|improve this answer

























                up vote
                10
                down vote



                accepted







                up vote
                10
                down vote



                accepted






                The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.



                In matplotlib 2.1, categorical plots have been introduced. This now allows for something like



                plt.plot(["apple", "banana", "cherry"], [2,1,3])


                While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.



                In this case you would want to do this conversion like



                values = [None if v is '' else float(v) for v in values]





                share|improve this answer














                The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.



                In matplotlib 2.1, categorical plots have been introduced. This now allows for something like



                plt.plot(["apple", "banana", "cherry"], [2,1,3])


                While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.



                In this case you would want to do this conversion like



                values = [None if v is '' else float(v) for v in values]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 13 at 16:07









                Mr. T

                4,18991535




                4,18991535










                answered Nov 7 '17 at 11:51









                ImportanceOfBeingErnest

                124k10127203




                124k10127203






























                    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%2f47155569%2fdifference-in-plotting-with-different-matplotlib-versions%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'