Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Matplotlib's quiver plotting

I'm trying to understand how plt.quiver() works. My issue is as follows:

I plot a simple vector (1,1) as such:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1, units = 'xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()

I would expect the arrow to go from (0,0) to (1,1), but the result is slightly off from that:vector 1,1

Similarly, I try and plot an arrow for vector (0,3) and the resulting arrow seems to be for vector (0,3.5)...

enter image description here

My assumption is that this has something to do with the kwargs 'units', 'scale', 'angles', & 'scale_units'. I've read the docs on them but don't fully understand how they work. A sunday school explanation would be greatly appreciated!

like image 554
RSHAP Avatar asked Apr 10 '16 19:04

RSHAP


People also ask

What does a quiver plot show?

Quiver plot is basically a type of 2D plot which shows vector lines as arrows. This type of plots are useful in Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.

How does quiver work in Matlab?

quiver( U , V ) plots arrows with directional components specified by U and V at equally spaced points. If U and V are vectors, then the x-coordinates of the arrows range from 1 to the number of elements in U and V , and the y-coordinates are all 1.

How do I save a quiver plot in Python?

The Quiver object does not have a savefig method and so fig. savefig will not work. However, you should just be able to swap the the fig. savefig command with plt.

How do you plot vector points in Python?

Create a matrix of 2×3 dimension. Create an origin point, from where vecors could be originated. Plot a 3D fields of arrows using quiver() method with origin, data, colors and scale=15.


2 Answers

If you adjust the aspect ratio of the figure to 1, the vectors are displayed to proper scale:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.quiver((0,0), (0,0), (1,0), (1,3), units = 'xy', scale = 1)
plt.axis('equal')
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.show()

enter image description here

like image 135
Reblochon Masque Avatar answered Oct 20 '22 11:10

Reblochon Masque


You can try this code.

import matplotlib.pyplot as plt

fig = plt.figure(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1,angles='xy', scale_units='xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.draw()
plt.show()

This is the output

Just remember that the first two arguments of quiver are the x and y coordinates of the tail of the vector, the next two are the lengths of the vector along x and y direction respectively. angle='xy' makes the arrow point from tail of the vector to its tip.

You can find out more about matplotlib.quiver at http://matplotlib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.quiver

like image 41
kanayamalakar Avatar answered Oct 20 '22 12:10

kanayamalakar