2

I want create a plot in maptplotlib with arrows pointing to the startpoint of a line and pointing away from the endpoint of a line to visualize where a line start and where it ends (see figure). The upper arrow on the right side (YYArrow) looks right, but should point away from the endpoint. The Arrow shall be drawn in Figure space, not in data space.

from matplotlib import pyplot as plt

def testplot():
    plt.plot([0, 1], [0, 1], 'r')
    plt.plot([0, 1], [0.9, 0.2], 'b-')
    plt.gcf().set_facecolor('w')
    plt.annotate("",
                xy=(0, 0.9), xycoords='data',
                xytext=(-25, 7), textcoords='offset points',
                size=20, va="center", ha="center",
                arrowprops=dict(ec='None',fc='k',
                                width=1, frac=0.5, headwidth=5,
                                ),
                )

    plt.annotate("",
                xy=(1, 0.2), xycoords='data',
                xytext=(25, -7), textcoords='offset points',
                size=20, va="center", ha="center",
                arrowprops=dict(ec='None',fc='k',
                                width=1, frac=0.5, headwidth=5,
                                ),
                )

    plt.annotate("",
                xy=(1, 0.1), xycoords='data',
                xytext=(25, -7), textcoords='offset points',
                size=20, va="center", ha="center",
                arrowprops=dict(arrowstyle="<|-,head_length=0.5,head_width=0.1",
                                connectionstyle="arc3,rad=-0.0",
                                fc='k'
                                ),
                )
    plt.show()

if __name__ == '__main__':
    testplot()

Problem: I want to specify a data point and a screen/figure offset, so that the arrow will always look the same, mo matter how the figure is scaled. Therefore it is necessary to use textcoords='offset points'. However, the better working YYArrow cannot be reversed (at least I do not know how).

arrowstyle="<|-" is working (reversing the arrow), but it is inferior (rounded corners instead of miter, much slower in interactive mode for > 50 arrows), maybe because it has to draw face and edge.

So I am searching for either a way to "flip" the YYArrow or be able to calculate (transformations?) the data point xy=... to which the arrow points given the offset in pixels, but I am not sure how.

Sample for YYArrows

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.