波形の簡単なプロット
モジュールのインポート
import numpy as np
import matplotlib . pyplot as plt
import scienceplots
#Warning : As of version 2.0.0, you need to add import scienceplots before setting the style (plt.style.use('science')).
plt . style . use ([ 'science' , 'notebook' ])
カラーコードの自作モジュールの関数のインポートして,
import color_html as clr
ユーザ関数
電圧波形は,
def funcv ( t , zc ): # 関数の定義
return np . sin ( t - zc )
計算値の出力のために,
def table ( sh , sv , h , v , f ): # 数表の作成(フォーマットで整頓した出力)
print ( f "{sh:>6s}" , end = ' ' )
nh = len ( h )
nv = len ( v )
[ print ( f "{h[j]:6.3f}" , end = ' ' ) for j in range ( nh )]
print ( '' )
print ( f "{sv:>6s}" , end = ' ' )
print ( '' )
for i in range ( nv ):
print ( f '{v[i]:6.3f}' , end = ' ' )
[ print ( f "{f[i,j]:6.3f}" , end = ' ' ) for j in range ( nh )]
print ( '' )
return
時間変化
時間 による変化を求めると,
pi2 = 2.0 * np . pi
t_min , t_max , nt = 0.0 , 4.0 * pi2 , 401
zc_min , zc_max , nz = 0.0 , 4.0 * pi2 , 17
tt = np . linspace ( t_min , t_max , nt )
zz = np . linspace ( zc_min , zc_max , nz )
yy2 = np . empty ( 0 )
for i in range ( nz ): # 0,1,2,...,n-1
alpha = tt - zz [ i ]
x1 = alpha >= 0.0
x2 = alpha <= pi2
x3 = np . logical_and ( x1 , x2 )
yy = np . where ( x3 , funcv ( tt , zz [ i ]), 0.0 )
yy2 = np . append ( yy2 , yy )
yyy = np . reshape ( yy2 ,[ nz , nt ])
計算値を表として出力すると,
nstep = 25 # 出力データの刻み
yyyt = np . transpose ( yyy )
yyy2 = yyyt [ 0 : nt : nstep ,:]
tt2 = tt [ 0 : nt : nstep ]
print ( "sin (t - z/c)" )
table ( "z/c/pi" , "t/pi" , zz / np . pi , tt2 / np . pi , yyy2 ) # 表の出力
sin (t - z/c)
z/c/pi 0.000 0.500 1.000 1.500 2.000 2.500 3.000 3.500 4.000 4.500 5.000 5.500 6.000 6.500 7.000 7.500 8.000
t/pi
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.500 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1.500 -1.000 0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
2.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
2.500 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
3.000 0.000 0.000 -0.000 -1.000 0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
3.500 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
4.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
4.500 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
5.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
5.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000 0.000 0.000
6.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -0.000 -1.000 0.000 1.000 0.000 0.000 0.000 0.000 0.000
6.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000 0.000
7.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 -0.000 1.000 0.000 0.000 0.000
7.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -1.000 0.000 1.000 0.000 0.000
8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 -0.000 -1.000 0.000 1.000 0.000
カラーコードの自作モジュールの関数を使用して,
colorh = clr . color_code ( "green" )
colorh
['GreenYellow',
'Chartreuse',
'LawnGreen',
'Lime',
'LimeGreen',
'PaleGreen',
'LightGreen',
'MediumSpringGreen',
'SpringGreen',
'MediumSeaGreen',
'SeaGreen',
'ForestGreen',
'Green',
'DarkGreen',
'YellowGreen',
'OliveDrab',
'Olive',
'DarkOliveGreen',
'MediumAquamarine',
'DarkSeaGreen',
'LightSeaGreen',
'DarkCyan',
'Teal']
プロットすると,
fig = plt . figure () # グラフ領域の作成
plt . text ( 3.0 , 2.25 , "$z/c$" , size = "large" )
plt . xlim (- 1 , 27 ) # x軸範囲の設定
plt . ylim (- 1.5 , 2.5 ) # y軸範囲の設定
plt . xlabel ( "$t$ [s]" ) # x軸のラベル設定
plt . ylabel ( "$V(t-z/c) / V_{max}$" ) # y軸のラベル設定
for i in range ( nz ):
plt . plot ( tt , yyy [ i ,:], color = colorh [ i ], label = f "{zz[i]:.2f}" )
# fontsize: int or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
plt . legend ( ncol = 4 , loc = 'upper right' , fancybox = False , frameon = True , fontsize = "large" )
fig . savefig ( 'p3_tlt_s8_1.pdf' )
距離による変化
距離 による変化を求めると,
t_min , t_max , nt = 0.0 , 4.0 * pi2 , 17
zc_min , zc_max , nz = 0.0 , 4.0 * pi2 , 401
tt = np . linspace ( t_min , t_max , nt )
zz = np . linspace ( zc_min , zc_max , nz )
yy2 = np . empty ( 0 )
for i in range ( nt ): # 0,1,2,...,n-1
alpha = tt [ i ]- zz
x1 = alpha >= 0.0
x2 = alpha <= pi2
x3 = np . logical_and ( x1 , x2 )
yy = np . where ( x3 , funcv ( tt [ i ], zz ), 0.0 )
yy2 = np . append ( yy2 , yy )
yyy = np . reshape ( yy2 ,[ nt , nz ])
計算値を表として出力すると,
nstep = 25 # 出力データの刻み
yyyt = np . transpose ( yyy )
yyy2 = yyyt [ 0 : nz : nstep ,:]
zz2 = zz [ 0 : nz : nstep ]
print ( "sin (t - z/c)" )
table ( "t/pi" , "z/c/pi" , tt / np . pi , zz2 / np . pi , yyy2 ) # 表の出力
sin (t - z/c)
t/pi 0.000 0.500 1.000 1.500 2.000 2.500 3.000 3.500 4.000 4.500 5.000 5.500 6.000 6.500 7.000 7.500 8.000
z/c/pi
0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.500 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1.500 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
2.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
2.500 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
3.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000 0.000
3.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000 0.000
4.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000 0.000
4.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000 0.000
5.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000 0.000
5.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000 0.000
6.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000 -0.000
6.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000 -1.000
7.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 0.000
7.500 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000
8.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
カラーコードの自作モジュールの関数を使用して,
colorh = clr . color_code ( "brown" )
colorh
['Cornsilk',
'BlanchedAlmond',
'Bisque',
'NavajoWhite',
'Wheat',
'BurlyWood',
'Tan',
'RosyBrown',
'SandyBrown',
'Goldenrod',
'DarkGoldenrod',
'Peru',
'Chocolate',
'SaddleBrown',
'Sienna',
'Brown',
'Maroon']
プロットすると,
fig = plt . figure () # グラフ領域の作成
plt . text ( 3.5 , 2.28 , "$ t$" , size = "large" )
plt . xlim (- 1 , 27 ) # x軸範囲の設定
plt . ylim (- 1.5 , 2.5 ) # y軸範囲の設定
plt . xlabel ( "$z/c$ [s]" ) # x軸のラベル設定
plt . ylabel ( "$V(t-z/c) / V_{max}$" ) # y軸のラベル設定
for i in range ( nt ):
plt . plot ( zz , yyy [ i ,:], color = colorh [ i ], label = f "{tt[i]:.2f}" )
# fontsize: int or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
plt . legend ( ncol = 4 , loc = 'upper right' , fancybox = False , frameon = True , fontsize = "large" )
fig . savefig ( 'p3_tlt_s8_2.pdf' )
前のページに戻る
「目次」のページに戻る