我如何绘制下面的傅立叶级数方程,使用for循环,我可以改变n,而不是写出cos(x) + cos(2x) + cos(3x)等等?
我的脚本如下:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
pi = np.pi
x = np.linspace(-pi,pi,100)
ao = (1/(2*pi))
y = ao + (1/pi)*(np.cos(x)+np.cos(2*x)+np.cos(3*x) + np.cos(4*x)+np.cos(5*x)+np.cos(6*x) + np.cos(7*x) + np.cos(8*x) + np.cos(9*x)+np.cos(10*x))
plt.plot(x,y)
plt.show()
Run Code Online (Sandbox Code Playgroud) 这是一个基本问题,但我想遍历数据框列表,并为每个数据框将索引设置为数据框中的列之一。下面代码的问题在于它没有使用新索引保存数据框。如何格式化此 For 循环,以便在 for 循环之外永久更改数据帧?谢谢。
dflist = [df_1, df_2, df_3]
for i in dflist:
i = i.set_index('column_2')
Run Code Online (Sandbox Code Playgroud) 我希望遍历M行XN列值数组并将它们打印到屏幕以用于以后的脚本,但是我遇到了循环和NR的问题.这是我到目前为止所拥有的:
#!/bin/bash
cat temp_file | wc -l > num_rows
i="0"
while [ $i -le `cat num_rows` ]; do
echo $i
awk 'NR==$i{print $1, $2}' temp_file
awk 'NR==$i{print $3, $4}' temp_file
((i++))
done
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我希望首先使用每行来查找行数,num_rows和循环数,然后使用AWK的NR函数遍历每一行,首先打印第一列和第二列,然后是第三列和第四列.上述脚本运行时会出现以下错误:
0
awk: illegal field $(), name "i"
input record number 1, file temp_file
source line number 1
awk: illegal field $(), name "i"
input record number 1, file temp_file
source line number 1
1
awk: illegal field $(), name "i"
input record number 1, file temp_file
source …Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,其中包含多个条目,如下所示:
# 2018 11 21 17 47 37.708756 -34.390213 116.803673 2.6972 0.442474 3.324627 2.840390 0.885880 890
LM01 0.836408 1.00 P
LM01 1.035398 1.00 S
LM03 3.987074 1.00 S
# 2018 11 22 11 58 25.550581 -34.439400 116.750832 2.8513 0.288144 3.306790 2.576028 0.771026 891
LM01 1.664419 1.00 P
LM01 2.471786 1.00 S
LM03 3.536432 1.00 P
# 2018 11 22 14 38 7.190175 -34.447819 116.788727 3.1661 0.577347 2.063253 2.132511 0.608057 892
LM01 1.629825 1.00 P
LM02 3.059825 1.00 P
LM03 3.284825 …Run Code Online (Sandbox Code Playgroud) 给出以下脚本来读取纬度,经度和幅度数据:
#!/usr/bin/env python
# Read in latitudes and longitudes
eq_data = open('lat_long')
lats, lons = [], []
for index, line in enumerate(eq_data.readlines()):
if index > 0:
lats.append(float(line.split(',')[0]))
lons.append(float(line.split(',')[1]))
#Build the basemap
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
antmap = Basemap(projection='spstere', boundinglat=-20, lon_0=-60, resolution='f')
antmap.drawcoastlines(color='0.50', linewidth=0.25)
antmap.fillcontinents(color='0.95')
x,y = antmap(lons, lats)
antmap.plot(x,y, 'r^', markersize=4)
plt.show()
Run Code Online (Sandbox Code Playgroud)
尝试阅读纬度,经度和幅度时,我收到以下错误:
Traceback (most recent call last):
File "./basic_eqplot.py", line 10, in <module>
lats.append(float(line.split(',')[0]))
ValueError: invalid literal for float(): -18.381 -172.320 …Run Code Online (Sandbox Code Playgroud)