Help needed in developing a test to solve an issue that twiny() deslocate the axes more that it should

I was trying to write a test to fix the issue I was working on, but it still don’t work. The issue is that: #5904, so I would thank any kind of help.

  • This is the test that is into lib/matplotlib/tests:

    def test_twinning():
      # plot the axis
      ax1 = plt.axes()
      # get the currently position
      pos1 = ax1.get_position()
      # pos2 takes pos1 coords, and, alter the height by multiplying it by 0.6
      pos2 = [pos1.x0, pos1.y0,  pos1.width, pos1.height * 0.6]
      # twiny() ax1
      ax2 = ax1.twiny()
      pos3 = ax2.get_position()
      pos4 = [pos3.x0, pos3.y0,  pos3.width, pos3.height * 0.6]
      # set a new position
      # Assert that the positions are the same, and the twinning don't change them
      assert ax2.get_position() == pos3
  • And this is the code that I changed, based on the patch, already available in the issue post:

    def axes_locator(_, renderer, bounds, transform=None):
      self_locator = _TransformedBoundsLocator(bounds, transform)
          if self_locator is not None:
              return self_locator(self, renderer)

The main problem is, the last line of the test did not work. I don’t know why, I am a beginner in matplotlib. Other problem is to add the enhancements that @jklymak proposed, these are his recommendations:

@lucasricci, I think you missed @anntzer other comment in that thread about using _InsetLocator instead of this exact patch.

I’m pretty leery about this, but maybe it will work. In addition to your basic test, try resizing manually the whole figure, and please test that the figure pickles and unpickles. I know this was marked as Good First Issue, but the locators can have subtle bugs.


I would thank any kind of help!