如果你不想使用優秀的pandas
,這裏是一種蒸汽方式。
import matplotlib.pyplot as plt
import math
import datetime as dt
test = """16:58:58 | 2.090 | 26.88 | 1.2945E-9 | 45.8\n
16:59:00 | 2.031 | 27.00 | 1.3526E-9 | 132.1\n
16:59:02 | 2.039 | 26.90 | 1.3843E-9 | 178.5\n
16:59:04 | 2.031 | 26.98 | 1.4628E-9 | 228.9\n
16:59:06 | 2.031 | 27.04 | 1.5263E-9 | 259.8\n
16:59:08 | 2.027 | 26.84 | 1.6010E-9 | 271.8\n"""
lines = [line for line in test.splitlines() if line != ""]
# Here is the real code
subset = []
for line in lines:
parts = line.split('|')
ts = dt.datetime.strptime(parts[0].strip(), "%H:%M:%S")
num = math.log(float(parts[3].strip()))
subset.append((ts, num))
# now there is a list of tuples with your datapoints, looking like
# [(datetime.datetime(1900, 1, 1, 16, 58, 58), 1.2945E-9), (datetime.datetime(1900, 1, 1, 16, 59), ...]
# I made this list intentionally so that you can see how one can gather everything in a tidy way from the
# raw string data.
# Now lets separate things for plotting
times = [elem[0] for elem in subset]
values = [elem[1] for elem in subset]
# now to plot, I'm going to use the matplotlib plot_date function.
plt.figure()
plt.plot_date(times, values)
# do some formatting on the date axis
plt.gcf().autofmt_xdate()
plt.show()
使用'pandas'庫將大大簡化你的生活! – nbeuchat
歡迎來到SO:請參加[旅遊]。你應該把這兩個問題分成兩個單獨的問題。首先,也許你想讓數據保持正確的形狀。那麼你想擔心繪製它。 –
@nbeuchat現在看看它現在看起來不錯。 –