4.5 C
London
Thursday, December 19, 2024
HomePythonMatplotlib in PythonHow to Create a Quiver Plot in Matplotlib (With Examples)

How to Create a Quiver Plot in Matplotlib (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

quiver plot is a type of plot that displays arrows with directional components U and V at the Cartesian coordinates specified by X and Y.

We can easily create a quiver plot in Matplotlib by using the quiver() function, which uses the following syntax:

quiver(x, y, u, v)

where:

  • x: The x-coordinates of the arrow locations
  • y: The y-coordinates of the arrow locations
  • u: The x components of the arrow vectors
  • v: The y components of the arrow vectors

This tutorial provides several examples of how to use this function in practice.

Example 1: Quiver Plot with One Arrow

The following code shows how to display a quiver plot with just one arrow:

import matplotlib.pyplot as plt

#define plots
fig, ax = plt.subplots()

#define coordinates and directions
x = 0
y = 0
u = 15
v = 3

#create quiver plot
ax.quiver(x, y, u, v)

#display quiver plot
plt.show()

Quiver plot in matplotlib

Example 2: Quiver Plot with Two Arrows

The following code shows how to display a quiver plot with two arrows:

import matplotlib.pyplot as plt

#define plots
fig, ax = plt.subplots()

#define coordinates and directions
x = [0, 0]
y = [0, 0]
u = [0, 1]
v = [-2, 0]
#create quiver plot
ax.quiver(x, y, u, v, scale = 10)

#display quiver plot
plt.show()

Quiver plot in Python with two arrows

Note that the scale argument scales the arrows to be longer, which makes them easier to view on the plot.

Example 3: Quiver Plot with a Mesh Grid

The following code shows how to display a quiver plot using a mesh grid:

import matplotlib.pyplot as plt
import numpy as np

#define plots
fig, ax = plt.subplots()

#define coordinates and directions
x,y = np.meshgrid(np.arange(-2, 2, .1), np.arange(-2, 2, .1))
z = x*np.exp(-x**2 - y**2)
v, u = np.gradient(z, .1, .1)

#create quiver plot
ax.quiver(x, y, u, v)

#display quiver plot
plt.show()

Matplotlib quiver

You can find the complete documentation for the quiver() function here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories