Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConnectionPatch "axis fraction" failure when axesB contains a plot #2377

Closed
JamesPHoughton opened this issue Sep 4, 2013 · 5 comments
Closed
Milestone

Comments

@JamesPHoughton
Copy link

I'm having a bit of an obscure issue drawing lines from one subplot (using data coordinates) to another subplot (using axes fraction coordinates). When the destination subplot is empty, the code works fine:

from matplotlib.patches import ConnectionPatch
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8,8))

x=np.arange(0,10)
y=x**2

ax_1 = plt.subplot2grid([2,2], loc=[0,0])
plt.plot(x, y)

ax_2 = plt.subplot2grid([2,2], loc=[1,1])
#plt.plot(x, y)

ax_1.add_artist(ConnectionPatch(xyA=[5,25], xyB=[.5,1], 
                                coordsA="data", coordsB="axes fraction", 
                                axesA=ax_1, axesB=ax_2,
                                arrowstyle="-|>"))

image

but when the destination subplot contains a plot, the line is interpreted in data coordinates instead of the requested axes fraction coordinates:

from matplotlib.patches import ConnectionPatch
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8,8))

x=np.arange(0,10)
y=x**2

ax_1 = plt.subplot2grid([2,2], loc=[0,0])
plt.plot(x, y)

ax_2 = plt.subplot2grid([2,2], loc=[1,1])
plt.plot(x, y)

ax_1.add_artist(ConnectionPatch(xyA=[5,25], xyB=[.5,1], 
                                coordsA="data", coordsB="axes fraction", 
                                axesA=ax_1, axesB=ax_2,
                                arrowstyle="-|>"))

image

Am I being silly? Is there a better way to do this? Or is it a bug?

@dmcdougall
Copy link
Member

The arrow looks like it's pointing to (0.5, 1) in both cases. What happens if you change it to [5, 90] in the second case? If that works, then it looks like you're still in data coordinates, and that's a bug.

@JamesPHoughton
Copy link
Author

I showed it pointing to [.5,1] for both cases to show that the only difference between the two was commenting out the axis 2 plot command. In the first plot, it points to the 'axis fraction' interpretation of [.5,1], in the second it points to the 'data' coordinate interpretation.

Here's a plot with the coordinates [5,90]:

image

Which is pretty undoubtedly in data coordinates!

An ugly workaround for now has been to generate an additional 'dummy' axis collocated with the original, but without any data in it. You can use 'axis fraction' coordinates on that axis no problem.

@pelson
Copy link
Member

pelson commented Sep 17, 2013

@leejjoon - you're probably best suited to answer this one. Any ideas?

@leejjoon
Copy link
Contributor

This is a bug. The fix is simple and you may apply this by yourself (I will soon commit the fix). Otherwise, here is a workaround although not that elegant.

class MyConnectionPatch(ConnectionPatch):
    def get_path_in_displaycoord2(self):

        dpi_cor = self.get_dpi_cor()

        x, y = self.xy1
        posA = self._get_xy(x, y, self.coords1, self.axesA)

        x, y = self.xy2
        posB = self._get_xy(x, y, self.coords2, self.axesB)

        _path = self.get_connectionstyle()(posA, posB,
                                           patchA=self.patchA,
                                           patchB=self.patchB,
                                           shrinkA=self.shrinkA * dpi_cor,
                                           shrinkB=self.shrinkB * dpi_cor
                                          )

        _path, fillable = self.get_arrowstyle()(_path,
                                                self.get_mutation_scale(),
                                                self.get_linewidth() * dpi_cor,
                                                self.get_mutation_aspect()
                                               )

        return _path, fillable

ax_1.add_artist(MyConnectionPatch(xyA=[5,25], xyB=[.5,1],
                                  coordsA="data", coordsB="axes fraction",
                                  axesA=ax_1, axesB=ax_2,
                                  arrowstyle="-|>"))

```python

@mdboom
Copy link
Member

mdboom commented Sep 26, 2013

I've cherry-picked this back to the v1.3.x branch as c4a2135

@leejjoon: In the future, if simple bugfixes also apply to the maintenance branch, do them there -- it's too easy forget to backport the fixes otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants