[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib

Introduction

** Visualize the vector field using matplotlib. As an example, we plot the electrostatic and static magnetic fields learned in high school physics. ** **

Every program has a flow

  1. Create x, y mesh for drawing with numpy's meshgrid method,
  2. Define the vector function $ F (u (x, y), v (x, y)) $ on the generated (x, y) points
  3. Draw arrows (vectors) on the mesh with various options (https://matplotlib.org/devdocs/api/_as_gen/matplotlib.axes.Axes.quiver.html),

That is.

It will be easier to understand if you check the operation while looking at a concrete example.

Contents

(1) Visualization of the electrostatic field created by one point charge (2) Electrostatic field created by two point charges (electric dipole) (3) Static magnetic field created by steady-state current passing through the origin (4) Three-dimensional vector field (electrostatic field created by point charge at origin)


(1) Visualization of the electrostatic field created by one point charge


"""
(1)Electrostatic field created by one point charge at the origin
"""

import numpy as np
import matplotlib.pyplot as plt

plt.figure()


LX, LY=2.1,2.1   #Parameters for mesh
gridwidth=0.33 
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth))  #Mesh generation

R = np.sqrt(X**2+Y**2) #Distance from the origin

#Position coordinates of point charge and charge
X1,Y1=0,0   #Placement of charge to the origin
Q1=1       #Charge amount setting
R1=np.sqrt((X-X1)**2+(Y-Y1)**2) #Arbitrary point from this charge(X,Y)Distance to
plt.plot(X1,Y1,'o',color='blue') #Draw point charge

#Vector function F(U(x,y), V(x,y))Is defined. The expression of the electrostatic field is used.
U = Q1*(X-X1)/(R1**2)  
V = Q1*(Y-Y1)/(R1**2)

plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=5.0) #Plot vector field

plt.xlim([-LX,LX]) #Range of X to draw
plt.ylim([-LY,LY]) #Range of y to draw

#Graph drawing
plt.grid()
plt.draw()
plt.show()

Result (1)

t.png An electrostatic field due to a point charge placed at the origin.


(2) Electrostatic field created by two point charges (electric dipole)


"""
(2)Two point charges(Electric dipole)Electrostatic field created by
"""

import numpy as np
import matplotlib.pyplot as plt

plt.figure()


LX, LY=2,2

gridwidth=0.2 
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth)) 

R = np.sqrt(X**2+Y**2)

#Position coordinates and charge of two point charges
X1,Y1=1.1,0
Q1=1
R1=np.sqrt((X-X1)**2+(Y-Y1)**2)
plt.plot(X1,Y1,'o',color='blue')

X2,Y2=-1.1,0
Q2=-1
R2=np.sqrt((X-X2)**2+(Y-Y2)**2)
plt.plot(X2,Y2,'o',color='blue')
##

#Vector function settings. 2 charges.
U = Q1*(X-X1)/(R1**2)+Q2*(X-X2)/(R2**2)
V = Q1*(Y-Y1)/(R1**2)+Q2*(Y-Y2)/(R2**2)

plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=6.5)


plt.xlim([-LX,LX])
plt.ylim([-LY,LY])

#Graph drawing
plt.grid()
plt.draw()
plt.show()

Result (2): Electrostatic field created by two point charges (electric dipole)

tt.png

An electrostatic field created by two point charges (charges are 1 and -1 respectively) placed at (-1.1,0) and (1.1,0).

(3) Static magnetic field created by steady-state current passing through the origin

"""
(3)A static magnetic field created by a steady-state current passing through the origin
"""

import numpy as np
import matplotlib.pyplot as plt

plt.figure()


LX, LY=3,3

gridwidth=0.3 
X, Y= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth)) 

R = np.sqrt(X**2+Y**2)

#Position coordinates of point charge and charge
X1,Y1=0,0 
I=1  #Steady current value
R1=np.sqrt((X-X1)**2+(Y-Y1)**2)
plt.plot(X1,Y1,'o',color='blue')

#Vector function settings. Static magnetic field created by steady current F(U(x,y), V(x,y))
U = I*-1*(Y-Y1)/(R1)
V = I*(X-X1)/(R1)


plt.quiver(X,Y,U,V,color='red',angles='xy',scale_units='xy', scale=4.5)


plt.xlim([-LX,LX])
plt.ylim([-LY,LY])

#Graph drawing
plt.grid()
plt.draw()
plt.show()


Result (3) Static magnetic field created by steady-state current

t.png

A static magnetic field created by a steady-state uniform current I that runs perpendicular to the paper from the origin.


(4) Three-dimensional vector field (electrostatic field created by point charge at origin)

"""
(4)Illustration of 3D vector field
Electrostatic field created by point charge at origin
"""
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D #Import for 3D plots

import numpy as np

fig = plt.figure()
ax = Axes3D(fig)


LX, LY, LZ = 2,2,2  #xyz mesh parameters
gridwidth=0.9 # 
X, Y, Z= np.meshgrid(np.arange(-LX, LX, gridwidth), np.arange(-LY, LY,gridwidth),np.arange(-LZ, LZ, gridwidth) ) #Mesh generation

R = np.sqrt(X**2+Y**2+Z**2)

#Position coordinates of point charge and charge
X1,Y1, Z1=0,0,0
Q1=1
R1=np.sqrt((X-X1)**2+(Y-Y1)**2+(Z-Z1)**2)
ax.scatter3D(X1,Y1,Z1,"o", color='blue')

U = Q1*(X-X1)/(R1**2)
V = Q1*(Y-Y1)/(R1**2)
W= Q1*(Z-Z1)/(R1**2)


ax.quiver(X, Y, Z, U, V, W, color='red',length=1, normalize=False)

ax.set_xlim([-LX, LX])
ax.set_ylim([-LY, LY])
ax.set_zlim([-LZ, LZ])
plt.show()

Result (4) Three-dimensional vector field (electrostatic field created by point charge at origin)

tt.png

A three-dimensional plot of the electric field created by the point charge placed at the origin.

Recommended Posts

[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] Solving simultaneous linear equations, numerical calculation, numpy
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
Visualization memo by Python
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
Python application: data visualization # 2: matplotlib
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[Scientific / technical calculation by Python] Numerical solution of two-dimensional Laplace-Poisson equation by Jacobi method for electrostatic potential, elliptic partial differential equation, boundary value problem
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
[Scientific / technical calculation by Python] Solving one-dimensional Schrodinger equation in stationary state by shooting method (1), well-type potential, quantum mechanics