“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

preface

Matplotlib is Python’s drawing library, which provides a full set of matlab-like command apis to generate the publish-quality graphics you need. We’ve learned a series of graphs to illustrate the basic relationship between the two variables and how to highly customize the presentation style of the statistics, but using these graphs alone is not enough for all scenarios. For example, we need to visualize the distribution of rainfall across regions. Therefore, we need more practical graphics to express complex relationships in the real world.

Visualize the contents of a two-dimensional array

Let’s start with the simplest scenario. Suppose we have a two-dimensional array — the famous fractal shape Mandelbrot — that we want to visualize. You need to create a two-dimensional array and visualize it by calling plt.imshow().

import numpy as np
import matplotlib.cm as cm
from matplotlib import pyplot as plt
def iter_count(c, max_iter) :
    x = c
    for n in range(max_iter):
        if abs(x) > 2.:
            return n
        x = x ** 2 + c
    return max_iter
n = 512
max_iter = 64
xmin, xmax, ymin, ymax = -2.2.8., -1.5.1.5
x = np.linspace(xmin, xmax, n)
y = np.linspace(ymin, ymax, n)
z = np.empty((n, n))
for i, y_i in enumerate(y):
    for j, x_j in enumerate(x):
        z[i, j] = iter_count(complex(x_j, y_i), max_iter)
plt.imshow(z, cmap = cm.Spectral)
Copy the code

Tips: Imshow () takes a 2D array as an argument to render the image, where each pixel represents a value extracted from the 2D array. The color of the pixel is selected from the colormap. The data in a 2D array can also be from a file or other source, for example, we can draw the read image into a graph.

# fetch image
img = plt.imread('img.png')
# draw a picture
plt.imshow(img)
Copy the code

We can also use different color maps to see the effect, just by changing the value of the optional cmap parameter of plt.imshow().

plt.imshow(z, cmap = cm.binary, extent=(xmin, xmax, ymin, ymax))
Copy the code

Tips: The optional extent parameter of plt.imshow() specifies the coordinate system for storing data in a two-dimensional array -- a tuple of four values representing the minimum and maximum range on the horizontal and vertical axes, respectively.

Next, reduce the size of the array from 512×512 to 32×32 and see what happens:

n = 64
Copy the code

Tips: When using a 32x32 array to represent the Mandelbrot set, the resulting images are not reduced in size, but are still significantly different from the images generated by a 512x512 array. This is because plt.imshow() will perform interpolation if the input data is smaller or larger than a given image size. The default interpolation is linear interpolation, and you can see that the results are not always ideal. You can specify the interpolation type to use with the optional parameter Interpolation of the imshow() function.

Using the bicubic interpolation algorithm (interpolation = ‘bicubic’)

Visualization of two-dimensional scalar fields

You can use the numpy.meshgrid() function to generate samples from 2D functions. Then, use plt.pcolormesh() to display the function graph:

n = 256
x = np.linspace(-3..3., n)
y = np.linspace(-3..3., n)
x_list, y_list = np.meshgrid(x, y)
z_list = x_list * np.cos(x_list ** 2 + y_list ** 2)
plt.pcolormesh(x_list, y_list, z_list, cmap = cm.Spectral)
cb = plt.colorbar(orientation='horizontal', shrink=75.)
Copy the code

Tips: Using color maps can help us quickly determine the symbol and size of the corresponding points.

The np.meshGrid () function takes two coordinate lists and builds a grid of coordinates. Because both coordinate lists are NUMPY arrays, we can treat them as a single variable, which makes the process of calculating scalar fields concise and readable. Finally, call the function plt.pcolormesh() to render the image.

Visualization of contour lines

Contour lines connect all points with the same value, making it easier to see the distribution characteristics of the data.

def iter_count(c, max_iter) :
    x = c
    for n in range(max_iter):
        if abs(x) > 2.:
            return n
        x = x ** 2 + 0.98 * c
    return max_iter
n = 512
max_iter = 80
xmin, xmax, ymin, ymax = -0.32, -0.22.0.8.0.9
x = np.linspace(xmin, xmax, n)
y = np.linspace(ymin, ymax, n)
z = np.empty((n, n))
for i, y_i in enumerate(y):
    for j, x_j in enumerate(x):
        z[j, i] = iter_count(complex(x_j, y_i), max_iter)
plt.imshow(z, cmap = cm.Spectral, 
    interpolation = 'bicubic',
    origin = 'lower',
    extent=(xmin, xmax, ymin, ymax))
levels = [8.12.16.20]
ct = plt.contour(x, y, z, levels, cmap = cm.binary)
plt.clabel(ct, fmt='%d')
Copy the code

Tips: Pyplot.contour () function retrieves the coordinate lists X and y of the sample grid and the values stored in the matrix Z. The function then renders the contours corresponding to the values specified in the "level" list, either using the optional parameter cmap to apply color mapping, or using the optional parameter color to specify a unique color for all the contours.

Each contour can be displayed with a color bar or directly on a graph. The plt.contour() function returns a contour instance. The pyplot.clabel() function takes the Contour instance and an optional format string to render the label for each contour line.

Tips: By default, the fill contour is not anti-aliased. Optional antialiased arguments can be used to obtain more satisfactory results.

ct = plt.contour(x, y, z, levels, cmap = cm.binary, antialiased = True)
Copy the code

Visualization of two-dimensional vector fields

Vector fields associate a two-dimensional vector with every point on a two-dimensional plane and are common in physics. In this case, we rely on the SymPy package for symbolic calculations, which is only used to keep the code short. If this package is not installed, it can be installed using the PIP Install sympy command. We don’t have to calculate the vector field in relation to each other, remember that the main purpose of this article is visualization, so we only need to worry about how to display the vector field — using the Pyplot.quiver () function.

import sympy
from sympy.abc import x, y
def cylinder_stream_function(u = 1, r = 1) :
    radius = sympy.sqrt(x ** 2 + y ** 2)
    theta = sympy.atan2(y, x)
    return u * (radius - r ** 2 / r) * sympy.sin(theta)
def velocity_field(psi) :
    u = sympy.lambdify((x, y), psi.diff(y), 'numpy')
    v = sympy.lambdify((x, y), -psi.diff(x), 'numpy')
    return u, v
u_func, v_func = velocity_field(cylinder_stream_function() )
xmin, xmax, ymin, ymax = -2.5.2.5, -2.5.2.5
y, x = np.ogrid[ymin:ymax:16j, xmin:xmax:16j]
u, v = u_func(x, y), v_func(x, y)
m = (x ** 2 + y ** 2) < 1.
u = np.ma.masked_array(u, mask = m)
v = np.ma.masked_array(v, mask = m)
shape = patches.Circle((0.0), radius = 1., lw = 2., fc = 'w', ec = 'c', zorder = 0)
plt.gca().add_patch(shape)
plt.quiver(x, y, u, v, color='c', zorder = 1)
plt.axes().set_aspect('equal')
Copy the code

Tips: The vector field is stored in the matrices U and V, the coordinates of each vector we sample from the vector field; The matrices x and y represent sample locations. The matrices x, y, u, and v are passed to Pyplot.quiver () to render the vector field.

Series of links

Matplotlib common statistical graph drawing

Matplotlib uses custom colors to draw statistics

Matplotlib controls line style and line width

Matplotlib custom style to draw beautiful statistics

Matplotlib adds text instructions to the graph

Matplotlib adds comments to the graph

Matplotlib adds auxiliary grids and auxiliary lines to the graph

Matplotlib adds custom shapes

Matplotlib controls the scale spacing and labeling of the coordinate axes

Matplotlib uses logarithmic scales and polar coordinates

Matplotlib draws subgraphs

Matplotlib custom graph scale

Matplotlib graph output and save