Matplotlib: categorical plot without strings and inversion of axes












1















Let's take this snippet of Python:



import matplotlib.pyplot as plt

x = [5,4,3,2,1,0]
x_strings = ['5','4','3','2','1','0']
y = [0,1,2,3,4,5]

plt.figure()

plt.subplot(311)
plt.plot(x, y, marker='o')

plt.subplot(312)
plt.plot(x_strings, y, marker='^', color='red')

plt.subplot(313)
plt.plot(x, y, marker='^', color='red')
plt.gca().invert_xaxis()

plt.show()


Which produces these three subplots:



enter image description here



In the top subplot the x values are automatically sorted increasingly despite their order in the given list. If I want to plot x vs. y exactly in the given order of x, then I have two possibilities:



1) Convert x values to strings and have a categorical plot -- that's the middle subplot.



2) Invert the x-axis -- that's the bottom subplot.



Question: is there any other way to do a sort of categorical plot, but without conversion of numbers into strings and without the inversion of the x-axis?



ADD-ON:



If I use set_xticklabels(list), then for some unclear reason the first element in the list is skipped (no matter if I refer to the x or to the x_strings list), and the resulting plot is also totally strange:



import matplotlib.pyplot as plt

x = [5,4,3,2,1,0]
x_strings = ['5','4','3','2','1','0']
y = [0,1,2,3,4,5]

fig, ax = plt.subplots()

ax.set_xticklabels(x)
ax.plot(x, y, marker='^', color='red')

plt.show()


enter image description here










share|improve this question





























    1















    Let's take this snippet of Python:



    import matplotlib.pyplot as plt

    x = [5,4,3,2,1,0]
    x_strings = ['5','4','3','2','1','0']
    y = [0,1,2,3,4,5]

    plt.figure()

    plt.subplot(311)
    plt.plot(x, y, marker='o')

    plt.subplot(312)
    plt.plot(x_strings, y, marker='^', color='red')

    plt.subplot(313)
    plt.plot(x, y, marker='^', color='red')
    plt.gca().invert_xaxis()

    plt.show()


    Which produces these three subplots:



    enter image description here



    In the top subplot the x values are automatically sorted increasingly despite their order in the given list. If I want to plot x vs. y exactly in the given order of x, then I have two possibilities:



    1) Convert x values to strings and have a categorical plot -- that's the middle subplot.



    2) Invert the x-axis -- that's the bottom subplot.



    Question: is there any other way to do a sort of categorical plot, but without conversion of numbers into strings and without the inversion of the x-axis?



    ADD-ON:



    If I use set_xticklabels(list), then for some unclear reason the first element in the list is skipped (no matter if I refer to the x or to the x_strings list), and the resulting plot is also totally strange:



    import matplotlib.pyplot as plt

    x = [5,4,3,2,1,0]
    x_strings = ['5','4','3','2','1','0']
    y = [0,1,2,3,4,5]

    fig, ax = plt.subplots()

    ax.set_xticklabels(x)
    ax.plot(x, y, marker='^', color='red')

    plt.show()


    enter image description here










    share|improve this question



























      1












      1








      1








      Let's take this snippet of Python:



      import matplotlib.pyplot as plt

      x = [5,4,3,2,1,0]
      x_strings = ['5','4','3','2','1','0']
      y = [0,1,2,3,4,5]

      plt.figure()

      plt.subplot(311)
      plt.plot(x, y, marker='o')

      plt.subplot(312)
      plt.plot(x_strings, y, marker='^', color='red')

      plt.subplot(313)
      plt.plot(x, y, marker='^', color='red')
      plt.gca().invert_xaxis()

      plt.show()


      Which produces these three subplots:



      enter image description here



      In the top subplot the x values are automatically sorted increasingly despite their order in the given list. If I want to plot x vs. y exactly in the given order of x, then I have two possibilities:



      1) Convert x values to strings and have a categorical plot -- that's the middle subplot.



      2) Invert the x-axis -- that's the bottom subplot.



      Question: is there any other way to do a sort of categorical plot, but without conversion of numbers into strings and without the inversion of the x-axis?



      ADD-ON:



      If I use set_xticklabels(list), then for some unclear reason the first element in the list is skipped (no matter if I refer to the x or to the x_strings list), and the resulting plot is also totally strange:



      import matplotlib.pyplot as plt

      x = [5,4,3,2,1,0]
      x_strings = ['5','4','3','2','1','0']
      y = [0,1,2,3,4,5]

      fig, ax = plt.subplots()

      ax.set_xticklabels(x)
      ax.plot(x, y, marker='^', color='red')

      plt.show()


      enter image description here










      share|improve this question
















      Let's take this snippet of Python:



      import matplotlib.pyplot as plt

      x = [5,4,3,2,1,0]
      x_strings = ['5','4','3','2','1','0']
      y = [0,1,2,3,4,5]

      plt.figure()

      plt.subplot(311)
      plt.plot(x, y, marker='o')

      plt.subplot(312)
      plt.plot(x_strings, y, marker='^', color='red')

      plt.subplot(313)
      plt.plot(x, y, marker='^', color='red')
      plt.gca().invert_xaxis()

      plt.show()


      Which produces these three subplots:



      enter image description here



      In the top subplot the x values are automatically sorted increasingly despite their order in the given list. If I want to plot x vs. y exactly in the given order of x, then I have two possibilities:



      1) Convert x values to strings and have a categorical plot -- that's the middle subplot.



      2) Invert the x-axis -- that's the bottom subplot.



      Question: is there any other way to do a sort of categorical plot, but without conversion of numbers into strings and without the inversion of the x-axis?



      ADD-ON:



      If I use set_xticklabels(list), then for some unclear reason the first element in the list is skipped (no matter if I refer to the x or to the x_strings list), and the resulting plot is also totally strange:



      import matplotlib.pyplot as plt

      x = [5,4,3,2,1,0]
      x_strings = ['5','4','3','2','1','0']
      y = [0,1,2,3,4,5]

      fig, ax = plt.subplots()

      ax.set_xticklabels(x)
      ax.plot(x, y, marker='^', color='red')

      plt.show()


      enter image description here







      python matplotlib type-conversion categorical-data invert






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 13:53







      sergiuspro

















      asked Nov 25 '18 at 13:11









      sergiusprosergiuspro

      205




      205
























          2 Answers
          2






          active

          oldest

          votes


















          -1














          Both attempted solutions seem possible. Alternatively, you can always mimic categorical plots by plotting integer numbers and setting the ticklabels to your liking.



          import matplotlib.pyplot as plt

          x = [5,4,3,2,1,0]
          y = [0,1,2,3,4,5]

          fig, ax = plt.subplots()

          ax.plot(range(len(y)), y, marker='^', color='red')

          ax.set_xticks(range(len(y)))
          ax.set_xticklabels(x)

          plt.show()


          enter image description here






          share|improve this answer
























          • Thank you! My implementation of set_xticklabels() was not fully correct.

            – sergiuspro
            Nov 25 '18 at 14:00



















          0














          I have found another way to do it, without being anyhow categorical and without x-axis inversion!



          ax = plt.subplot()
          ax.set_xlim(x[0],x[-1], auto=True) # this line plays the trick
          plt.plot(x, y, marker='^', color='red')


          enter image description here






          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%2f53467797%2fmatplotlib-categorical-plot-without-strings-and-inversion-of-axes%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














            Both attempted solutions seem possible. Alternatively, you can always mimic categorical plots by plotting integer numbers and setting the ticklabels to your liking.



            import matplotlib.pyplot as plt

            x = [5,4,3,2,1,0]
            y = [0,1,2,3,4,5]

            fig, ax = plt.subplots()

            ax.plot(range(len(y)), y, marker='^', color='red')

            ax.set_xticks(range(len(y)))
            ax.set_xticklabels(x)

            plt.show()


            enter image description here






            share|improve this answer
























            • Thank you! My implementation of set_xticklabels() was not fully correct.

              – sergiuspro
              Nov 25 '18 at 14:00
















            -1














            Both attempted solutions seem possible. Alternatively, you can always mimic categorical plots by plotting integer numbers and setting the ticklabels to your liking.



            import matplotlib.pyplot as plt

            x = [5,4,3,2,1,0]
            y = [0,1,2,3,4,5]

            fig, ax = plt.subplots()

            ax.plot(range(len(y)), y, marker='^', color='red')

            ax.set_xticks(range(len(y)))
            ax.set_xticklabels(x)

            plt.show()


            enter image description here






            share|improve this answer
























            • Thank you! My implementation of set_xticklabels() was not fully correct.

              – sergiuspro
              Nov 25 '18 at 14:00














            -1












            -1








            -1







            Both attempted solutions seem possible. Alternatively, you can always mimic categorical plots by plotting integer numbers and setting the ticklabels to your liking.



            import matplotlib.pyplot as plt

            x = [5,4,3,2,1,0]
            y = [0,1,2,3,4,5]

            fig, ax = plt.subplots()

            ax.plot(range(len(y)), y, marker='^', color='red')

            ax.set_xticks(range(len(y)))
            ax.set_xticklabels(x)

            plt.show()


            enter image description here






            share|improve this answer













            Both attempted solutions seem possible. Alternatively, you can always mimic categorical plots by plotting integer numbers and setting the ticklabels to your liking.



            import matplotlib.pyplot as plt

            x = [5,4,3,2,1,0]
            y = [0,1,2,3,4,5]

            fig, ax = plt.subplots()

            ax.plot(range(len(y)), y, marker='^', color='red')

            ax.set_xticks(range(len(y)))
            ax.set_xticklabels(x)

            plt.show()


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 13:50









            ImportanceOfBeingErnestImportanceOfBeingErnest

            135k13151226




            135k13151226













            • Thank you! My implementation of set_xticklabels() was not fully correct.

              – sergiuspro
              Nov 25 '18 at 14:00



















            • Thank you! My implementation of set_xticklabels() was not fully correct.

              – sergiuspro
              Nov 25 '18 at 14:00

















            Thank you! My implementation of set_xticklabels() was not fully correct.

            – sergiuspro
            Nov 25 '18 at 14:00





            Thank you! My implementation of set_xticklabels() was not fully correct.

            – sergiuspro
            Nov 25 '18 at 14:00













            0














            I have found another way to do it, without being anyhow categorical and without x-axis inversion!



            ax = plt.subplot()
            ax.set_xlim(x[0],x[-1], auto=True) # this line plays the trick
            plt.plot(x, y, marker='^', color='red')


            enter image description here






            share|improve this answer




























              0














              I have found another way to do it, without being anyhow categorical and without x-axis inversion!



              ax = plt.subplot()
              ax.set_xlim(x[0],x[-1], auto=True) # this line plays the trick
              plt.plot(x, y, marker='^', color='red')


              enter image description here






              share|improve this answer


























                0












                0








                0







                I have found another way to do it, without being anyhow categorical and without x-axis inversion!



                ax = plt.subplot()
                ax.set_xlim(x[0],x[-1], auto=True) # this line plays the trick
                plt.plot(x, y, marker='^', color='red')


                enter image description here






                share|improve this answer













                I have found another way to do it, without being anyhow categorical and without x-axis inversion!



                ax = plt.subplot()
                ax.set_xlim(x[0],x[-1], auto=True) # this line plays the trick
                plt.plot(x, y, marker='^', color='red')


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 15:28









                sergiusprosergiuspro

                205




                205






























                    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%2f53467797%2fmatplotlib-categorical-plot-without-strings-and-inversion-of-axes%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

                    Refactoring coordinates for Minecraft Pi buildings written in Python