Home > Net >  How to put arrows on Matplotlib graph?
How to put arrows on Matplotlib graph?

Time:11-19

I'm looking to build a graph in Matplotlib with the origin centered and arrows on both sides of the x- and y-axes, and have been mostly successful in doing so with the following code:

fig, ax = plt.subplots(1)

plt.xlim([-100, 100])
plt.ylim([-100, 100])

ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)

ax.set_aspect('equal')

for axis in ['top','bottom','left','right']:
    ax.spines[axis].set_linewidth(3)

# This part adds the arrows
ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)

plt.show()

enter image description here

The problem is that I can't figure out how to add arrows to the left and bottom parts of the graph. Any ideas?

CodePudding user response:

You can try to add two lines:

ax.plot(0, 0, "<k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 0, "vk", transform=ax.get_xaxis_transform(), clip_on=False)

Alternatively, you can use arrows

  • Related