Python Matplotlib - Spine coloring issue in a double-y axis plot











up vote
1
down vote

favorite












I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.



def func(ax, data, color, position):    # A function for plotting
ax.plot(data[0], data[1], color=color)
ax.spines[position].set_color(color)

fig = plt.figure()
data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]

ax = fig.add_subplot(111)
func(ax, data_1, 'r', 'left')

ax_1 = ax.twinx()
func(ax_1, data_2, 'b', 'right')

plt.show()


The expected plot should have both y axis colored, however, only right spine is colored, as shown below.
enter image description here



When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.



    def func(ax, data, color, position):    # A function for plotting
    ax.plot(data[0], data[1], color=color)
    ax.spines[position].set_color(color)

    fig = plt.figure()
    data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
    data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]

    ax = fig.add_subplot(111)
    func(ax, data_1, 'r', 'left')

    ax_1 = ax.twinx()
    func(ax_1, data_2, 'b', 'right')

    plt.show()


    The expected plot should have both y axis colored, however, only right spine is colored, as shown below.
    enter image description here



    When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.



      def func(ax, data, color, position):    # A function for plotting
      ax.plot(data[0], data[1], color=color)
      ax.spines[position].set_color(color)

      fig = plt.figure()
      data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
      data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]

      ax = fig.add_subplot(111)
      func(ax, data_1, 'r', 'left')

      ax_1 = ax.twinx()
      func(ax_1, data_2, 'b', 'right')

      plt.show()


      The expected plot should have both y axis colored, however, only right spine is colored, as shown below.
      enter image description here



      When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?










      share|improve this question













      I'm writing a script to draw plot via matplotlib, the following code is an analogy to my original script that reproduce the issue that I've encountered.



      def func(ax, data, color, position):    # A function for plotting
      ax.plot(data[0], data[1], color=color)
      ax.spines[position].set_color(color)

      fig = plt.figure()
      data_1 = [np.linspace(0, 1, 10), np.linspace(0, 10, 10)]
      data_2 = [np.linspace(0, 1, 10), np.linspace(10, 0, 10)]

      ax = fig.add_subplot(111)
      func(ax, data_1, 'r', 'left')

      ax_1 = ax.twinx()
      func(ax_1, data_2, 'b', 'right')

      plt.show()


      The expected plot should have both y axis colored, however, only right spine is colored, as shown below.
      enter image description here



      When zooming into left spine you can find a red shadow around y-axis, that means my colored spine is covered by another one, how to solve this issue by only modifying func()?







      python matplotlib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 15 hours ago









      nochenon

      336




      336
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.



          So:



          def func(ax, data, color, position):    # A function for plotting
          ax.plot(data[0], data[1], color=color)
          ax.spines[position].set_color(color)
          for pos in ['left', 'right']:
          if pos != position:
          ax.spines[pos].set_visible(False)





          share|improve this answer




























            up vote
            1
            down vote













            Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:



            def func(ax, data, color, position):    # A function for plotting
            ax.plot(data[0], data[1], color=color)
            ax.spines[position].set_color(color)

            if position == 'left':
            other = 'right'
            elif position == 'right':
            other = 'left'
            ax.spines[other].set_color('None')


            Result:



            enter image description here



            This can solve my problem, but I'm still open for other beautiful solutions.






            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',
              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%2f53183966%2fpython-matplotlib-spine-coloring-issue-in-a-double-y-axis-plot%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.



              So:



              def func(ax, data, color, position):    # A function for plotting
              ax.plot(data[0], data[1], color=color)
              ax.spines[position].set_color(color)
              for pos in ['left', 'right']:
              if pos != position:
              ax.spines[pos].set_visible(False)





              share|improve this answer

























                up vote
                1
                down vote



                accepted










                twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.



                So:



                def func(ax, data, color, position):    # A function for plotting
                ax.plot(data[0], data[1], color=color)
                ax.spines[position].set_color(color)
                for pos in ['left', 'right']:
                if pos != position:
                ax.spines[pos].set_visible(False)





                share|improve this answer























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.



                  So:



                  def func(ax, data, color, position):    # A function for plotting
                  ax.plot(data[0], data[1], color=color)
                  ax.spines[position].set_color(color)
                  for pos in ['left', 'right']:
                  if pos != position:
                  ax.spines[pos].set_visible(False)





                  share|improve this answer












                  twinx does not only "twin" the y axis spine but all other three spines, too. So the red spine on the left is basically overdrawn (like you recognized already in your own answer). Instead of setting their color to None, you can set_visible() their visibility to False, which seems to be the preferred way, compared to lines 18-22 here.



                  So:



                  def func(ax, data, color, position):    # A function for plotting
                  ax.plot(data[0], data[1], color=color)
                  ax.spines[position].set_color(color)
                  for pos in ['left', 'right']:
                  if pos != position:
                  ax.spines[pos].set_visible(False)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 11 hours ago









                  gehbiszumeis

                  782118




                  782118
























                      up vote
                      1
                      down vote













                      Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:



                      def func(ax, data, color, position):    # A function for plotting
                      ax.plot(data[0], data[1], color=color)
                      ax.spines[position].set_color(color)

                      if position == 'left':
                      other = 'right'
                      elif position == 'right':
                      other = 'left'
                      ax.spines[other].set_color('None')


                      Result:



                      enter image description here



                      This can solve my problem, but I'm still open for other beautiful solutions.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:



                        def func(ax, data, color, position):    # A function for plotting
                        ax.plot(data[0], data[1], color=color)
                        ax.spines[position].set_color(color)

                        if position == 'left':
                        other = 'right'
                        elif position == 'right':
                        other = 'left'
                        ax.spines[other].set_color('None')


                        Result:



                        enter image description here



                        This can solve my problem, but I'm still open for other beautiful solutions.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:



                          def func(ax, data, color, position):    # A function for plotting
                          ax.plot(data[0], data[1], color=color)
                          ax.spines[position].set_color(color)

                          if position == 'left':
                          other = 'right'
                          elif position == 'right':
                          other = 'left'
                          ax.spines[other].set_color('None')


                          Result:



                          enter image description here



                          This can solve my problem, but I'm still open for other beautiful solutions.






                          share|improve this answer












                          Seems like other spines would be created after executing this code: ax_1 = ax.twinx(), so I find a "dumb" solution:



                          def func(ax, data, color, position):    # A function for plotting
                          ax.plot(data[0], data[1], color=color)
                          ax.spines[position].set_color(color)

                          if position == 'left':
                          other = 'right'
                          elif position == 'right':
                          other = 'left'
                          ax.spines[other].set_color('None')


                          Result:



                          enter image description here



                          This can solve my problem, but I'm still open for other beautiful solutions.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 12 hours ago









                          nochenon

                          336




                          336






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53183966%2fpython-matplotlib-spine-coloring-issue-in-a-double-y-axis-plot%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              Popular posts from this blog

                              横浜市

                              Prokocim

                              Hungria