Home > Blockchain >  how to plot figure with arrows
how to plot figure with arrows

Time:10-11

How can I add arrows on x and y axes, show only the values that are used and add label for coordinates My code on this moment:

x = [9, 8, 11, 11, 14, 13, 16, 14, 14]
y = [9, 16, 15, 11, 10, 11, 10, 8, 8]
fig = plt.figure(figsize=(7,7), dpi=300)
axes = fig.add_axes([0,1,1,1])

axes.set_xlim(0, 17)
axes.set_ylim(0, 17)

axes.invert_yaxis()

axes.scatter(x, y, color='green')
axes.vlines(x, 0, y, linestyle="dashed", color='green')
axes.hlines(y, 0, x, linestyle="dashed", color='green')
axes.spines.right.set_visible(False)
axes.spines.bottom.set_visible(False)

plt.show()

Visually:

enter image description here

CodePudding user response:

Adding

axes.plot(1, 0, ">k", transform=axes.get_yaxis_transform(), clip_on=False)  
axes.plot(0, 0, "vk", transform=axes.get_xaxis_transform(), clip_on=False)

Will do it for you. This is basically just a cheat plot of markers.


There are also

from mpl_toolkits.axisartist.axislines.AxesZero which allow

for direction in ["xzero", "yzero"]:
    # adds arrows at the ends of each axis
    ax.axis[direction].set_axisline_style("-|>")

but they can't handle the reversed y axis in your case with default settings.

  • Related