How can I draw axis lines inside a plot in Matplotlib?










3















When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2 and -2 < y < 2, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.










share|improve this question






















  • When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

    – cel
    Sep 16 '15 at 10:47






  • 2





    @cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

    – Hannes Ovrén
    Sep 16 '15 at 10:50















3















When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2 and -2 < y < 2, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.










share|improve this question






















  • When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

    – cel
    Sep 16 '15 at 10:47






  • 2





    @cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

    – Hannes Ovrén
    Sep 16 '15 at 10:50













3












3








3








When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2 and -2 < y < 2, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.










share|improve this question














When I plot data using Matplotlib, the axes are always plotted by default as a box framing the plot. Let's say I am plotting data within axis limits -2 < x < 2 and -2 < y < 2, but I would like to draw axis lines inside this plot area through the origin, preferably with ticks and tick labels along these axis lines - not along the outer frame.







python matplotlib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 16 '15 at 10:32









Thomas ArildsenThomas Arildsen

339518




339518












  • When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

    – cel
    Sep 16 '15 at 10:47






  • 2





    @cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

    – Hannes Ovrén
    Sep 16 '15 at 10:50

















  • When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

    – cel
    Sep 16 '15 at 10:47






  • 2





    @cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

    – Hannes Ovrén
    Sep 16 '15 at 10:50
















When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

– cel
Sep 16 '15 at 10:47





When asking us to solve a problem that requires writing code, it is best to first give some example code, include the plot and tell us what exactly you would like to change. Telling us what you have already tried and did not work would be nice, but not necessary.

– cel
Sep 16 '15 at 10:47




2




2





@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

– Hannes Ovrén
Sep 16 '15 at 10:50





@cel: As someone who regularly deals with matplotlib, I think the question is quite clear.

– Hannes Ovrén
Sep 16 '15 at 10:50












2 Answers
2






active

oldest

votes


















6














This is well documented in the spines example (old link) / spine placement demo (new link).



You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).



import numpy as np
import matplotlib.pyplot as plt


fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2*np.sin(x)

ax = fig.add_subplot(111)
ax.set_title('zeroed spines')
ax.plot(x, y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')

# remove the ticks from the top and right edges
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')


enter image description here






share|improve this answer
































    3














    I can at least give a half-complete answer.
    Yes, you can easily draw the axis lines. It is as simple as



    plt.axvline(0)
    plt.axhline(0)


    The original axes will remain, but can be turned off with plt.axis('off').
    It will also not give you any tick marks.






    share|improve this answer























    • Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

      – Thomas Arildsen
      Sep 16 '15 at 11:00






    • 1





      I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

      – Hannes Ovrén
      Sep 16 '15 at 11:03










    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%2f32606155%2fhow-can-i-draw-axis-lines-inside-a-plot-in-matplotlib%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









    6














    This is well documented in the spines example (old link) / spine placement demo (new link).



    You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).



    import numpy as np
    import matplotlib.pyplot as plt


    fig = plt.figure()
    x = np.linspace(-np.pi, np.pi, 100)
    y = 2*np.sin(x)

    ax = fig.add_subplot(111)
    ax.set_title('zeroed spines')
    ax.plot(x, y)
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')

    # remove the ticks from the top and right edges
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')


    enter image description here






    share|improve this answer





























      6














      This is well documented in the spines example (old link) / spine placement demo (new link).



      You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).



      import numpy as np
      import matplotlib.pyplot as plt


      fig = plt.figure()
      x = np.linspace(-np.pi, np.pi, 100)
      y = 2*np.sin(x)

      ax = fig.add_subplot(111)
      ax.set_title('zeroed spines')
      ax.plot(x, y)
      ax.spines['left'].set_position('zero')
      ax.spines['right'].set_color('none')
      ax.spines['bottom'].set_position('zero')
      ax.spines['top'].set_color('none')

      # remove the ticks from the top and right edges
      ax.xaxis.set_ticks_position('bottom')
      ax.yaxis.set_ticks_position('left')


      enter image description here






      share|improve this answer



























        6












        6








        6







        This is well documented in the spines example (old link) / spine placement demo (new link).



        You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).



        import numpy as np
        import matplotlib.pyplot as plt


        fig = plt.figure()
        x = np.linspace(-np.pi, np.pi, 100)
        y = 2*np.sin(x)

        ax = fig.add_subplot(111)
        ax.set_title('zeroed spines')
        ax.plot(x, y)
        ax.spines['left'].set_position('zero')
        ax.spines['right'].set_color('none')
        ax.spines['bottom'].set_position('zero')
        ax.spines['top'].set_color('none')

        # remove the ticks from the top and right edges
        ax.xaxis.set_ticks_position('bottom')
        ax.yaxis.set_ticks_position('left')


        enter image description here






        share|improve this answer















        This is well documented in the spines example (old link) / spine placement demo (new link).



        You are going to turn off the right and top spines (e.g. spines['right'].set_color('none')), and move the left and bottom spines to the zero position (e.g. spines['left'].set_position('zero')).



        import numpy as np
        import matplotlib.pyplot as plt


        fig = plt.figure()
        x = np.linspace(-np.pi, np.pi, 100)
        y = 2*np.sin(x)

        ax = fig.add_subplot(111)
        ax.set_title('zeroed spines')
        ax.plot(x, y)
        ax.spines['left'].set_position('zero')
        ax.spines['right'].set_color('none')
        ax.spines['bottom'].set_position('zero')
        ax.spines['top'].set_color('none')

        # remove the ticks from the top and right edges
        ax.xaxis.set_ticks_position('bottom')
        ax.yaxis.set_ticks_position('left')


        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 2:15









        ImportanceOfBeingErnest

        135k13151228




        135k13151228










        answered Sep 16 '15 at 11:02









        tmdavisontmdavison

        29k66880




        29k66880























            3














            I can at least give a half-complete answer.
            Yes, you can easily draw the axis lines. It is as simple as



            plt.axvline(0)
            plt.axhline(0)


            The original axes will remain, but can be turned off with plt.axis('off').
            It will also not give you any tick marks.






            share|improve this answer























            • Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

              – Thomas Arildsen
              Sep 16 '15 at 11:00






            • 1





              I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

              – Hannes Ovrén
              Sep 16 '15 at 11:03















            3














            I can at least give a half-complete answer.
            Yes, you can easily draw the axis lines. It is as simple as



            plt.axvline(0)
            plt.axhline(0)


            The original axes will remain, but can be turned off with plt.axis('off').
            It will also not give you any tick marks.






            share|improve this answer























            • Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

              – Thomas Arildsen
              Sep 16 '15 at 11:00






            • 1





              I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

              – Hannes Ovrén
              Sep 16 '15 at 11:03













            3












            3








            3







            I can at least give a half-complete answer.
            Yes, you can easily draw the axis lines. It is as simple as



            plt.axvline(0)
            plt.axhline(0)


            The original axes will remain, but can be turned off with plt.axis('off').
            It will also not give you any tick marks.






            share|improve this answer













            I can at least give a half-complete answer.
            Yes, you can easily draw the axis lines. It is as simple as



            plt.axvline(0)
            plt.axhline(0)


            The original axes will remain, but can be turned off with plt.axis('off').
            It will also not give you any tick marks.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 16 '15 at 10:48









            Hannes OvrénHannes Ovrén

            13.4k54863




            13.4k54863












            • Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

              – Thomas Arildsen
              Sep 16 '15 at 11:00






            • 1





              I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

              – Hannes Ovrén
              Sep 16 '15 at 11:03

















            • Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

              – Thomas Arildsen
              Sep 16 '15 at 11:00






            • 1





              I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

              – Hannes Ovrén
              Sep 16 '15 at 11:03
















            Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

            – Thomas Arildsen
            Sep 16 '15 at 11:00





            Thanks, that at least takes me some of the way. I will try to see if I can find a way of decorating those with ticks and labels.

            – Thomas Arildsen
            Sep 16 '15 at 11:00




            1




            1





            I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

            – Hannes Ovrén
            Sep 16 '15 at 11:03





            I would suggest looking through the matplotlib documentation. There is quite a lot of things possible with matplotlib if you look past the matplotlib.pyplot module, and instead check the "real" API. Drawing the coordinate axes is something that I have wanted to do myself several times, but never had the time to completely solve.

            – Hannes Ovrén
            Sep 16 '15 at 11:03

















            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%2f32606155%2fhow-can-i-draw-axis-lines-inside-a-plot-in-matplotlib%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3