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

Review and

In Python about drawing, Mlab provides open source matplotlib module, which can not only draw line graph, bar graph, scatter graph and other conventional graphs, but also support to draw quantity field graph, spectrum graph, violin graph, box graph and other special graphs. Examples can be found in previous articles to view details.

  • The specgram() method plots the spectrum for the amplitude spectrum: Matplotlib plots the spectrum

  • The boxplot() method plots boxes for data distribution: Matplotlib plots boxes

  • Quiver () method to draw quantity field map for electromagnetic field analysis: Matplotlib to draw quantity field map

  • Shows data distribution and probability: Matplotlib plots the violinplot

In our daily life, we often pay attention to the weather forecast. When the season changes, the announcer will explain the flow of air. In weather forecasting, meteorologists make predictions about local weather conditions based on the flow of air drawn on a flow chart.

In this issue, we will learn matplotlib. Pyplot. Streamplot relevant properties () method of learning, let ‘s go ~

1. Overview of the flow chart

  • What is a flow chart?

    • The flow chart is drawn by a combination of flow lines and arrows to represent the operation of flow lines in a certain period of time.
    • The arrow on the flow chart indicates the direction of flow and the shape on the flow line indicates the flow intensity
    • Flow chart can be divided into flow chart, isovelocity line, elevation chart, etc
    • Streamlines in a flow chart can be merged, confluent or intersected, but not crossed
  • Application scenario of the flow diagram

    • Flow charts are commonly used in meteorology to study the direction of wind speed, air currents and ocean currents
    • Flow chart is an important chart for wind field analysis. The thin density of streamlines is proportional to the size of wind speed
  • Method of obtaining flow diagram

    import matplotlib.pyplot as plt 
    plt.streamplot(x,y,u,v)
    Copy the code

2. Flow chart attributes

  • Set the density of the flow chart

    • Keywords: Density
    • The default value is 1
    • The value can be floating point or tuple
    • Control the density of the flow chart. When density=1, the grid will be divided into 30*30 grids
    • To set the density in each direction, use the tuple (x,y)
  • Set the streamline width

    • Keyword: linewidth
    • The value can be floating point or two-dimensional array
    • Using a two-dimensional array, you can change the width of the streamlines on the grid
    • The array shape must be the same as u and V
  • Set the streamline color

    • Key words: color
    • The value can be:
      • Words for color: green “G”
      • Short for color words such as red “r”, yellow “Y”
      • RGB format: hexadecimal format, such as “# 88C999 “; (r,g,b) tuple form
      • You can go to the color list
    • When using CMAP, color needs to be set to a two-dimensional array, otherwise it will not work
  • Set streamline scaling

    • Key word: Norm
    • The default is to stretch the streamline to (0,1)
    • Only used when the color is an array
  • Set the streamline color scheme

    • Keyword: CMAP
    • The value is in the format of color table _r
    • The values are as follows: ‘Accent’, ‘Accent_r’, ‘Blues’, ‘Blues_r’, ‘BrBG’, ‘BrBG_r’, ‘BuGn’, ‘BuGn_r’, ‘BuPu’, ‘BuPu_r’, ‘CMRmap’, ‘CMRmap_r’, ‘Dark2’, ‘Dark2_r’, ‘GnBu’, ‘GnBu_r’, ‘Greens’

3. Draw a flow diagram

  • Import matplotlib. Pyplot class
import matplotlib.pyplot as plt
Copy the code
  • Call numpy libraries arange(), Random (), randint() and so on to prepare x,y,u,v data

    • X, y: one-dimensional array/two-dimensional array
    • U,v: two-dimensional array
    • Meshgrid (x,y),np.mgrid()
    x = np.arange(1.10)
    y = np.arange(1.10)
    
    u,v = np.meshgrid(np.sin(x),np.sin(y))
    Copy the code
  • Call Pyplot.streamplot () to draw a flow plot

plt.streamplot(x,y,u,v,density=[0.5.1])
Copy the code
  • A call to Pyplot.show () renders the flow diagram
plt.show()
Copy the code

  • Set the lineWidth, color, and CMAP properties to draw a flow diagram
plt.streamplot(x,y,u,v,density=[0.5.1],color=u,cmap="Accent_r",linewidth=3)
Copy the code

4. Test the cat

Now that we have learned about the properties associated with drawing a flow line, let’s practice with the starting point of the control flow line

  • Call Np.mgrid [] to define x, Y 2d data
  • Call the Pyplot.streamplot () method to draw a flow plot
  • Call the Pyplot.plot () method to draw a line chart marked by marker attributes
y,x= np.mgrid[-3:3:100j, -3:3:100j]
u = -1-x**2+y
v = 1+x-y**2

seed_points = np.array([[-2, -1.0.1.2, -1], [...2, -1.0.1.2.2]])
plt.streamplot(x,y,u,v,density=0.6,color=u,cmap="autumn",linewidth=1,start_points=seed_points.T)
plt.plot(seed_points[0],seed_points[1]."^",color="b")

plt.show()
Copy the code

conclusion

In this installment, we will learn about properties related to the streamplot() method provided by Matplotlib.pyplot to plot streamplots. Flow charts are commonly used in meteorology to study changes in air currents.

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