简体   繁体   English

在Python中绘图仅从CSV中提取特定列

[英]Plotting in Python extracting only specific columns from a CSV

EDIT: as suggested shortening the question: 编辑:建议缩短问题:

Quite new to python and programming, and I would like to plot the 1st and 4th column into a log(x) log(y) graph. 对于python和编程来说是相当新的东西,我想将第1列和第4列绘制到log(x)log(y)图中。 And honestly I don't knot how to extract only the two columns i need from this. 老实说,我不知道如何从中仅提取我需要的两列。

16:58:58 | 2.090 | 26.88  | 1.2945E-9  |   45.8
16:59:00 | 2.031 | 27.00  | 1.3526E-9  |  132.1
16:59:02 | 2.039 | 26.90  | 1.3843E-9  |  178.5
16:59:04 | 2.031 | 26.98  | 1.4628E-9  |  228.9
16:59:06 | 2.031 | 27.04  | 1.5263E-9  |  259.8
16:59:08 | 2.027 | 26.84  | 1.6010E-9  |  271.8

Using pandas: 使用熊猫:

import pandas as pd
df = pd.read_csv("data.txt", delimiter="\s[|]\s+", header=None, index_col=0)
df.plot(y=4)

在此处输入图片说明

(Note that this ignores the logarithmic scaling because it's not clear what the logarithm of a time should be) (请注意,这忽略了对数缩放,因为不清楚时间的对数应该是多少)

If you want to not use the excellent pandas , here is a steam approach. 如果您不想使用优质的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()

剧情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM