Question

With the help of sankey-toolbox in Matplotlib, we can plot sankey-diagrams automaticly: The position of a sankey-object is automaticly calculated based on the position of its prior-object and cannot be given manually; and when a sankey-diagram is initialized, the position of the first sankey-object will be assigned with the input of an axis. (the (0,0)-point will be the center-point of this object)

And Here is the situation: i want to draw two sankey diagrams with a given y-offset, and several ports of the two diagrams should be connected. Therefore are two coordinate systems with y-offset in the same subplot required. I have tried the 'add_axes' method, but with this method a new subplot is created and there will be a graphic scaling problem.

Now this is the question: Is it possible to create a new coordinate system with a given y-offset, without creating subplot?

Was it helpful?

Solution

Not really a complete answer, but it might help you to find the solution: sankey.add() supports a transform keyword argument. Depending on your figure, create a new transform instance, and use it for the shifted part of your figure:

offset = transforms.ScaledTranslation(0.0, 0.5, fig.dpi_scale_trans)
new_transform = ax.transData + offset
sankey.add(flows=[-0.25, 0.15, 0.1], fc='#37c959', label='two',
           orientations=[-1, -1, -1], prior=0, connect=(0, 0),
           transform = new_transform)

In order to create the new transform, the helper function matplotlib.transforms.offset_copy() may also be useful:

new_transform = offset_copy(ax.transData, fig=fig,
                            x = 0.05, y=0.10, units='inches')

OTHER TIPS

Problem solved. I wrote a subclass of Affine2DBase-class to do the coordinate translation based on a given transform object, for example, a ax.transData object. And it works well for my program... hope it helps for other people.

class coordinateTranslation(Affine2DBase):
    def __init__(self, dx, dy, transform, **kwargs):
        Affine2DBase.__init__(self,**kwargs)
        self._dx = dx
        self._dy = dy
        self._transform = transform
        self._mtx = None
        self._inverted = None
    def __repr__(self):
        return "coordinateTranslation(%r, %r)" % (self._dx, self._dy)
    def get_matrix(self):
        dx, dy = self._transform.transform((self._dx, self._dy))
        dx_zero, dy_zero = self._transform.transform((0, 0))
        dx -= dx_zero; dy -= dy_zero
        self._mtx = np.array([[1.0, 0.0, dx],
                              [0.0, 1.0, dy],
                              [0.0, 0.0, 1.0]], np.float_)
        self._inverted = None
        return self._mtx
    get_matrix.__doc__ = Affine2DBase.get_matrix.__doc__
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top