我正在尝试使用matplotlib从csv文件中绘制数据。时间戳有1列:
26-08-2016 00:01 0.062964691
26-08-2016 00:11 0.047209214
26-08-2016 00:21 0.047237823
我只能使用下面的代码仅使用整数创建一个简单的绘图,当y数据是时间戳时,这不起作用。我需要添加什么? 这可能看起来很简单,但我时间紧迫:/ 谢谢!
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import datetime as dt
x,y = np.loadtxt('I112-1.csv',
unpack=True,
delimiter = ',')
plt.plot(x,y)
plt.title('Title')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
答案 0 :(得分:0)
以下是我解决此问题的示例:
import pandas as pd
from io import StringIO
from datetime import datetime
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
data_file = StringIO("""
time,value
26-08-2016 00:01,0.062964691
26-08-2016 00:11,0.047209214
26-08-2016 00:21,0.047237823""")
df = pd.read_table(data_file,delimiter=",")
df['datetime']= df.time.map(lambda l: datetime.strptime(l, '%d-%m-%Y %H:%M'))
ax = df.set_index("datetime",drop=False)[['value','datetime']].plot(title="Title",yticks=df.value)