This article is a sequel to Draw contour lines in textbooks (Fortan + gnuplot).
The author is studying fluid dynamics learned in the engineering department of the university, but at the beginning of fluid dynamics (perfect fluid theory), physical quantities such as velocity potential $ \ phi $ and stream function $ \ psi $ appear, and these quantities There are some typical flow examples using (double side. 87% BA% E3% 81% 97) etc.). The two-dimensional contour lines of these quantities are shown as figures in the textbook, but I feel that if I can draw them myself, my understanding will deepen. However, it is often difficult to draw contour lines of a physical quantity on a two-dimensional plane with a graph software service that can easily draw the function $ y = f (x) $. In the previous article, I tried drawing with Fortran + gnuplot, but I would like to try it with a popular programming language, so this time I will draw contour lines using Python.
Since Python is multi-platform, install it according to each environment. There are various ways to install Python, so please check it yourself and choose the one that looks good. For those who find it difficult to install or do not want to install various things on their PC, we recommend using Google Colaboratory.
In addition to Python itself, numpy and matplotlib are required as modules. If you want to use Python etc. that is originally included in the OS, install it additionally.
The previous article is targeted for the same balloon / suction. Assuming that the distance from the origin is $ r $, the velocity potential $ \ phi $ in three dimensions can be expressed as follows.
Python's matplotlib module can draw various things, so the work can be solved only with Python.
Create a Python script in the flow. I don't do advanced things, so I don't consider object orientation.
An example script is shown below.
contour.py
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
xmin = -10.0
xmax = 10.0
ymin = -10.0
ymax = 10.0
delta = 0.1
x = np.arange(xmin,xmax,delta)
y = np.arange(xmin,xmax,delta)
m = 1.0
X, Y = np.meshgrid(x,y)
r = np.sqrt(X**2 + Y**2)
Z = -m/r
fig, ax = plt.subplots(figsize=(6,6))
levels = np.arange(-1.0,0.0,0.2)
cont = ax.contour(X,Y,Z,levels)
cont.clabel(fmt='%1.1f', fontsize=12)
plt.show()
First, the coordinate values are put in $ x $ and $ y $, and $ X $ and $ Y $ are converted into grid points. There are two styles of graph drawing in matplotlib, but the method using the Artist object is used. It is one of the advantages of matplotlib that you can put numerical values in the contour lines, so I put it in.
The above script is executed on the terminal as follows, and the contour map appears in a separate window.
python contour.py
If you want to save the image, click the Save button in this drawing window to save it, or change the plot.show ()
at the end of the script to the following.
plt.savefig('c2.png')
We introduced how to easily draw contour lines using the function of Python's matplotlib. This time, only the minimum necessary settings for matplotlib are set, but matplotlib can be set in detail to every corner of the graph, so if you are particular about it, you can draw the figures in the textbook correctly and neatly. ..
Recommended Posts