# 5 Matplotlib Methods That You Never Knew Existed

## 1. quiver

## 1. quiver

The `quiver`

method is used to plot vector fields in `matplotlib`

.

A vector field is a mathematical model of the magnitude and direction of different vectors in a 2D or 3D space.

These are used to represent different physical quantities like:

- Electromagnetic fields
- Fluid dynamics, and more

## How To Use?

quiver([X, Y], U, V, [C], **kwargs)

The parameters passed to this method are as follows:

`X, Y`

: define the x and y coordinates of the arrow locations`U, V`

: define the x and y direction components of the arrow vectors/directions`C`

: sets the colour of the arrows

## Example Plot

```
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 20)
y = np.linspace(0, 2*np.pi, 20)
# Starting positions of the vectors in the vector field
X, Y = np.meshgrid(x, y)
# Horizontal component of the vectors
U = np.cos(X)
# Vertical component of the vectors
V = np.sin(Y)
plt.quiver(X, Y, U, V)
plt.show()
```

## Documentation

## 2. contour / contourf

`contour`

method is used to create a Contour plot that visualises 3D data in two dimensions using contour lines.

## How To Use?

contour([X, Y,] Z, [levels], **kwargs)

The parameters passed to this method are as follows:

`X, Y`

: define the coordinates of the values in`Z`

`Z`

: define the height values over which the contour is drawn`levels`

: defines the number and positions of the contour lines/regions

## Example Plot

```
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
plt.contour(X, Y, Z)
plt.show()
```

The `contourf`

method creates filled contour plots that provide a visual representation of continuous data.

```
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
#Z is a Gaussian function
Z = np.exp(-(X**2 + Y**2))
plt.contourf(X, Y, Z, levels = 20)
plt.colorbar()
plt.show()
```

## Documentation

## 3. cohere

The `cohere`

method is used to plot the coherence/ correlation between two signals in the frequency domain.

Mathematically, Coherence is the normalized cross-spectral density as shown below:

## How To Use?

`plt.cohere(x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, window=mlab.window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None)`

The parameters passed to this method are as follows:

`x`

,`y`

: Input signals to be analyzed`NFFT`

: The number of data points used in each block for the FFT (Fast Fourier Transform)`Fs`

: The sampling frequency of the input signals`Fc`

: The centre frequency of`x`

`detrend`

: The function applied to each segment before FFT`window`

and`noverlap`

control the computation specifics for FFT

## Example Plot

```
import matplotlib.pyplot as plt
import numpy as np
# Generate two sample signals
t = np.arange(0.0, 5.0, 0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.sin(2 * np.pi * t + np.pi / 2)
plt.cohere(s1, s2, NFFT=64, Fs=1. / 0.01)
plt.xlabel('Frequency')
plt.ylabel('Coherence')
plt.show()
```

## Documentation

## 4. triplot

The `triplot`

method is used to create a Triangular grid or Triangulations that are subdivisions of a geometric object into triangles.

## How To Use?

```
triplot(triangulation, ...)
#or
triplot(x, y, [triangles], *, [mask=mask], ...)
```

The parameter to the methods can either be passed a `Triangulat`

ion object or can be given the points to define a triangular grid (`x`

, `y`

, `triangles`

and a `mask`

).

## Example Plot

```
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.tri as tri
points = np.random.rand(20, 2)
# Delaunay triangulation on the random points
triangulation = tri.Triangulation(points[:, 0], points[:, 1])
plt.triplot(triangulation, 'bo-')
plt.show()
```

## Documentation

## 5. matshow

The `matshow`

method is used to display matrices (2D arrays) as colour-coded grids.

## How To Use?

`matshow(A, fignum=None, **kwargs)`

The parameters passed to the method are as follows:

`A`

: the matrix to be displayed`fignum`

: specifies the figure that should be used for the plot

## Example Plot

```
import matplotlib.pyplot as plt
import numpy as np
# Sample matrix
data = np.random.rand(10, 10)
plt.matshow(data)
plt.colorbar()
plt.show()
```

## Documentation

*If you found the article valuable and wish to offer a gesture of encouragement:*

*Clap 50 times for this article**Leave a comment telling me what you think**Highlight the parts in this article that you resonate with*