How does matplotlib quiver work in Python ? Stepwise Answer

How does matplotlib quiver work in Python

Matplotlib quiver is an arrow based plots. quiver plots the vectors which have magnitudes and directions. quiver() is the part of matplotlib.pyplot module.

What is quiver plotting?

We all know about vectors. In the graphical representation, it looks like an arrow. Which has magnitude and some direction. Also when we plot any vector or arrow. We need two points. First for the origin and second for the tip of the arrow. We can also choose the colors for the quiver arrow.

 

Matplotlib quiver plotting Implementation-

Step 1: Importing Packages and Data Initialization-

In this step, we will import the required packages. We will also initialize the coordinates of the arrow.

import numpy as np 
import matplotlib.pyplot as plt
x_origin = 1
y_origin = 1
x_tip = 8
y_tip = 8

Step 2- Plotting Matplotlib quiver

In the above step, We have already initialized the data. Let’s invoke the function quiver(). Here is the syntax for that.

gph.quiver(x_origin, y_origin, x_tip, y_tip) 

Let’s fit the syntax with the above example.

fig, gph = plt.subplots(figsize = (5, 5)) 
gph.quiver(x_origin, y_origin, x_tip, y_tip) 
gph.set_title('Single arrow chart') 
plt.show() 

Here set_title() is optional. It is just to set the title of the plot.

Complete Code-

Let’s merge the code from both steps.

import numpy as np 
import matplotlib.pyplot as plt
x_origin = 1
y_origin = 1
x_tip = 8
y_tip = 8
fig, gph = plt.subplots(figsize = (5, 5)) 
gph.quiver(x_origin, y_origin, x_tip, y_tip) 
gph.set_title('Single arrow chart') 
plt.show() 

Now let’s run the code. We get the below arrow plot.

"<yoastmark

 

How to create Matplotlib quiver plotting with multiple arrows?

In the above section, We have plotted with a single arrow. In this example, We will see multiple quiver plotting. The only change is at data initialization after matplotlib import. It will be a list as coordinate in place of single values.

import numpy as np 
import matplotlib.pyplot as plt
x_origin = [1, 1] 
y_origin = [1, 1] 
x_tip = [15, 1] 
y_tip =[15, -15] 
fig, gph = plt.subplots(figsize = (5, 5)) 
gph.quiver(x_origin, y_origin, x_tip, y_tip) 
gph.set_title('Single arrow chart') 
plt.show() 

Here in the x_origin, We have used values in the list. In the same way, We have used the list as coordinates. Rest part is the same for the single arrow plot and multiple arrow plot.

multiple quiver python
multiple quiver python

Conclusion –

In this article, we have seen quiver plotting with single and multiple arrows. I hope each step is self-explanatory. Still of you have any doubt, please comment below in the comment box.

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner