Draw contour lines that appear in textbooks (Python)

This article is a sequel to Draw contour lines in textbooks (Fortan + gnuplot).

Introduction

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.

Preparation: Install Python and required modules

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.

Target flow field

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. $ \phi = -\frac{m}{r},\, r = \sqrt{x^2+y^2+z^2} \tag{1} $ Here, $ m $ is a coefficient called the strength of the callout. I want to draw contour lines in two dimensions, so set $ z = 0 $ $ \phi = -\frac{m}{r},\, r = \sqrt{x^2+y^2} \tag{2} $ Try to draw.

Work policy

Python's matplotlib module can draw various things, so the work can be solved only with Python.

  1. Create two-dimensional grid points (equally spaced squares) with the function of numpy, and calculate the value of $ \ phi (= \ phi_ {i, j}) $ at each grid point.
  2. Draw contour lines with the function of matplotlib.

Create a Python script in the flow. I don't do advanced things, so I don't consider object orientation.

grid.png

Python script

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

c2.png

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')

Summary

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

Draw contour lines that appear in textbooks (Python)
Draw graph in python
Formulas that appear in Doing Math with Python
Draw mp3 waveform in Python
Draw Poincare's disk in Python
Draw "Draw Ferns Programmatically" in Python
Draw implicit function in python
Make python segfault in 2 lines
Draw a heart in Python
Python install in 2 lines @Windows
Draw Sine Waves in Blender Python
Draw knots interactively in Plotly (Python)
Draw a scatterplot matrix in python
Make python segfault in three lines
Draw a CNN diagram in Python
Line graphs and scale lines in python
Draw Nozomi Sasaki in Excel with python
Draw a heart in Python Part 2 (SymPy)
Draw a tree in Python 3 using graphviz
One liner that outputs multiplication tables in Python
Draw a graph of a quadratic function in Python
A memo that I wrote a quicksort in Python
Read a file containing garbled lines in Python
Building an environment that uses Python in Eclipse
A program that removes duplicate statements in Python
Static type checking that starts loosely in Python
Draw graphs in Julia ... Leave the graphs to Python
Testing methods that return random values in Python
[Python] How to draw a histogram in Matplotlib
The one that displays the progress bar in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.