This is the 23rd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Review and

Matplotlib is an open source project based on The Python language. Pyplot provides a series of methods for drawing 2D graphs. With the iteration of the version, the Matplotlib module also supports the mplot3D toolkit for drawing 3D graphics, and the Animation class for making dynamic graphs. The Interactive mode of Pyplot can also be used for drawing dynamic graphs, and the image class is provided for loading, scaling and displaying images.

  • Plot line chart method: Matplotlib plot line chart

  • Pyplot.ion () Interactive plot dynamic graph: Matplotlib plots dynamic graph

  • Matplotlib animation:

  • Image class for image processing: Matplotlib image processing

We have learned that Matplot Pyplot provides methods for plotting broken lines, bars, scatters, pies, squares, graphs, etc. Pyplot also provides methods for plotting special graphs, such as physical magnetic plots, box plots, fiddles, etc

In this installment, we’ll learn about matplotlib.pyplot.quiver() related method property learning, let’s go~

1. Overview of quantity field diagram

  • What is a quantity field diagram?

    • Quantity field diagram, also known as vibration diagram, quantity field diagram. Represented by a set of vector arrows
    • A scalar field diagram represents one vector to another
    • The quantity forming the field is vector, called vector field
  • Usage scenarios of measurement field diagrams

    • Quantity field diagrams are commonly used in physics such as electromagnetic field representation
    • Quantity field map is also used for geomagnetic map drawing according to the observation data of various magnetic stations
    • Commonly used vector fields in physics include wind field, gravitational field, electromagnetic field, water flow field and so on
  • Method of drawing quantity field map

    import matplotlib.pyplot as plt
    
    plt.quiver()
    Copy the code

2. Quantity field map attributes

  • Set the color

    • Vector color keyword: color or facecolor
    • When both facecolor and color are set, facecolor takes precedence
    • Value range
      • Words for colors: e.g. Red, “red”
      • Short for color words such as red “r”, yellow “Y”
      • RGB format: hexadecimal format, such as “# 88C999 “; (r,g,b) tuple form
      • You can also pass in a list of colors
  • Set transparency

    • Key words: alpha
    • Values of 0 ~ 1
  • Set the vector arrow size

    • Keyword: units
    • The default value is width
    • Value are: {‘ width ‘, ‘height’ and ‘dots’,’ inches’, ‘x’, ‘y’, ‘y’}
      • Width,height: indicates the width and height of the axis
      • Dots,inches: pixels or inches based on the graph DPI
      • X,y,xy:x,y or the square root of x^2+y^2
  • Sets the position of vector arrows in coordinates

    • Key word: Pivot
    • The default value is tail
    • Possible values: {‘tail’, ‘mid’, ‘middle’, ‘tip’}
  • Set the width of the vector arrow

    • Keyword: width
    • The default value is 0.005
    • The value can be: floating point

3. Steps of drawing quantity field map

We also need to use matplotlib.pyplot when plotting quantity fields, so let’s take a look at the steps for plotting quantity fields

  • Import matplotlib. Pyplot class
import matplotlib.pyplot as plt
Copy the code
  • Prepare x and y data using arange(),random(),sin(),cos() and other methods in the NUMpy library
x = np.arange(-10.10.1)
y = np.arange(-10.10.1)
Copy the code
  • Call the numpy.meshGrid () method to generate two-dimensional grid coordinates
u,v = np.meshgrid(x,y)
Copy the code
  • Call Pyplot.quiver (x,y,u,v,c) to plot the quantity field
plt.quiver(x,y,u,v,alpha=0.4)
Copy the code
parameter instructions
x A one-dimensional or two-dimensional array or sequence representing the x coordinate of the arrow position
y A one-dimensional or two-dimensional array or sequence representing the y-coordinate of the arrow position
u A one-dimensional or two-dimensional array or sequence representing the x component of the arrow vector
v A one-dimensional or two-dimensional array or sequence representing the y-component of the arrow vector
c A one-dimensional or two-dimensional array or sequence representing the arrow color
  • Call Pyplot.show () to render the display icon
plt.show()
Copy the code

4. Test the cat

We learn the method of drawing quantity field map above, we draw a high school physics scene electromagnetic field map

  • Call numpy.meshGrid () to generate a two-dimensional list of x and y data
  • Call numpy.gradient() to generate gradient data for u and v
a = np.arange(-2.2.0.2)
b = np.arange(-2.2.0.25)
x,y = np.meshgrid(a,b)
z = x*np.exp(-x**2-y**2)
v,u = np.gradient(z,0.2.0.2)

plt.quiver(x,y,u,v,color="pink",pivot="tip",units="inches")

plt.scatter(x,y,color="b",s=0.05)

plt.show()
Copy the code

conclusion

In this issue, we will learn about the properties of the matplotlib.pyplot method quiver(). Quantity field map is commonly used in geological research, electromagnetic field and other vector scenes.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time