Wiz123
Wiz123

Reputation: 920

Quiver plot arrows using Matplotlib

I am trying to make a quiver plot using the list I4. In I4[0], (0,0) and (1,0) represent the starting position and the direction of the arrow respectively. However, the current output doesn't match the expected one. I attach both here.

import matplotlib.pyplot as plt
I4 = [[(0, 0), (1, 0)], [(0, 0), (0, -1)], [(1, 0), (1, -1)], [(0, -1), (1, -1)]]

fig, ax = plt.subplots(figsize = (10,10))
for i in range(0,len(I4)):
    ax.quiver(I4[i][0], I4[i][1])
plt.show()

The current output is

enter image description here

The expected output is

enter image description here

Upvotes: 0

Views: 596

Answers (2)

jylls
jylls

Reputation: 4705

Alternatively, you could use plt.arrow (doc here):

import matplotlib.pyplot as plt
I4 = [(0, 0),(1, 1),(0, 1), (0, 1)]
dI4=[(1,0),(0,-1),(1,0),(0,-1)]
texts=['(0,-1)','(1,-1)','(1,0)','(0,0)']
textxy=[(0,0),(1,0),(1,1),(0,1)]

fig, ax = plt.subplots(figsize = (10,10))
for i in range(len(I4)):
    ax.arrow(I4[i][0], I4[i][1],dI4[i][0],dI4[i][1],length_includes_head=True,head_width=0.05,color='k',lw=3)
    if i<2:
      dy=-0.1
    else:
      dy=0.1
    plt.text(textxy[i][0], textxy[i][1]+dy,texts[i],fontsize=20)

ax.set_aspect('equal')
ax.set_xlim([-0.5,1.5])
ax.set_ylim([-0.5,1.5])
plt.axis('off')
plt.show()

And the output gives:

enter image description here

Upvotes: 1

Davide_sd
Davide_sd

Reputation: 13185

Please, take a look at the documentation with help(ax.quiver): you'll see that you need to specify ax.quiver(x, y, u, v) where x, y are the starting coordinates, u, v represents the directions.

In practice, you need to unpack your lists, like this:

ax.quiver(*I4[i][0], *I4[i][1])

Upvotes: 0

Related Questions