# Python Matplotlib drawing streamline diagram

2022-02-01 16:26:03 This is my participation 11 The fourth of the yuegengwen challenge 28 God , Check out the activity details ：2021 One last more challenge

# Review

stay Python About drawing ,Mlab Provide open source matplotlib modular , You can not only draw a line chart 、 Histogram 、 Scatter diagram and other conventional diagrams , It also supports drawing quantity field diagram 、 spectrum 、 Picture of violin 、 Special drawings such as box drawing , Examples of previous articles can be found at .

We often pay attention to the weather forecast in our daily life , When the season changes , The announcer will explain the air flow . During the weather forecast , Meteorological experts will draw the air flow according to the streamline map , To predict the local weather . In this issue , We will learn matplotlib.pyplot.streamplot() Method related attribute learning ,let's go~

# 1. Streamline diagram Overview

• ## What is a flow chart ？

• Streamline diagram is drawn by a combination of streamline and arrow , To represent the operation of the streamline in a certain period of time 、
• The arrow on the streamline chart indicates the flow direction , The shape on the streamline indicates the flow intensity
• Streamline diagram can be divided into airflow diagram 、 Isoanemometry 、 Elevation map, etc
• The flow lines in the flow line diagram can be merged 、 Convergence 、 Split cross , But it can't cross
• ## Streamline diagram application scenario

• Streamline diagrams are usually used to study wind speed in meteorology 、 airflow 、 The flow direction of the ocean current
• Flow chart is an important chart for wind field analysis , The dilute density of the streamline is directly proportional to the wind speed
• ## Method of obtaining streamline diagram

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

# 2. Streamline properties

• ## Set streamline density

• keyword ：density
• The default value is ：1
• The value type is ： Floating point or tuple
• Control streamline density , When density=1 when , The grid will be divided into 30*30 grid
• For setting the density in each direction , Tuples can be used (x,y)
• ## Set the streamline width

• keyword ：linewidth
• The value type is ： Floating point or two-dimensional array
• Use a two-dimensional array , You can change the line width of the streamline on the mesh
• The shape of the array must match u、v identical
• ## Set streamline color

• keyword ：color
• Values can be ：
• English words for color ： Like green "g"
• Abbreviations of words indicating color, 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 when , You need to color Set as a two-dimensional array , Otherwise it will not work
• ## Set streamline scaling

• keyword ：norm
• The default is to stretch the streamline to （0,1）
• Use... Only when the color is an array
• ## Set streamline color system

• keyword ：cmap
• The value form is ： color table _r
• Available values commonly used are ：'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. To draw a streamline diagram

• Import matplotlib.pyplot class
``````import matplotlib.pyplot as plt
Copy code ``````
• call numpy library arange()、random()、randint() Wait until you're ready x,y,u,v data

• x,y: One dimensional array / Two dimensional array
• u,v: Two dimensional array
• When it is a two-dimensional array , Can pass np.meshgrid(x,y),np.mgrid() establish
``````x = np.arange(1,10)
y = np.arange(1,10)

u,v = np.meshgrid(np.sin(x),np.sin(y))
Copy code ``````
• call pyplot.streamplot() Draw a streamline diagram

``````plt.streamplot(x,y,u,v,density=[0.5,1])
Copy code ``````
• call pyplot.show() The rendering shows a streamline
``````plt.show()
Copy code `````` • Set up linewidth、color、cmap Property to draw a flow chart
``````plt.streamplot(x,y,u,v,density=[0.5,1],color=u,cmap="Accent_r",linewidth=3)
Copy code `````` # 4. A profound

We learned about the attributes related to drawing streamline diagram , Let's practice the starting point data of the control flow line

• call np.mgrid[] Definition x,y Two dimensional data
• call pyplot.streamplot() Method to draw a streamline diagram
• call pyplot.plot() Methods draw a line chart , Use marker Attribute tags
``````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,seed_points,"^",color="b")

plt.show()
Copy code `````` # summary

In this issue , We are right. matplotlib.pyplot Provide streamplot() Methods to draw streamline graph and learn related attributes . Flowcharts are usually used in meteorology , Study the change of air flow .

The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~