2017-05-02 14 views
10

(Câu hỏi này có thể được đọc một mình, nhưng là một phần tiếp theo: Timeseries from CSV data (Timestamp and events))chuỗi thời gian cốt truyện từ dữ liệu CSV (Timestamp và các sự kiện): x-nhãn liên tục

Tôi muốn hiển thị dữ liệu CSV (từ 2 files) như được hiển thị dưới đây, bởi một đại diện timeseries, sử dụng mô-đun gấu trúc của python (xem các liên kết dưới đây).

dữ liệu Mẫu df1:

   TIMESTAMP eventid 
0 2017-03-20 02:38:24  1 
1 2017-03-21 05:59:41  1 
2 2017-03-23 12:59:58  1 
3 2017-03-24 01:00:07  1 
4 2017-03-27 03:00:13  1 

Cột 'EventID' luôn chứa các giá trị là 1, và tôi đang cố gắng để hiển thị tổng của các sự kiện cho mỗi ngày trong tập dữ liệu. Bộ dữ liệu 2, df0, có cấu trúc tương tự nhưng chỉ chứa số không:

dữ liệu Mẫu df0:

   TIMESTAMP eventid 
0 2017-03-21 01:38:24  0 
1 2017-03-21 03:59:41  0 
2 2017-03-22 11:59:58  0 
3 2017-03-24 01:03:07  0 
4 2017-03-26 03:50:13  0 

Nhãn trục x chỉ hiển thị ngày tháng giống nhau, và câu hỏi của tôi là: Làm thế nào có thể các ngày khác nhau được hiển thị? (Điều gì gây ra cùng ngày để được hiển thị nhiều lần trên nhãn x?)

kịch bản cho đến nay:

import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

df1 = pd.read_csv('timestamp01.csv', parse_dates=True, index_col='TIMESTAMP') 
df0 = pd.read_csv('timestamp00.csv', parse_dates=True, index_col='TIMESTAMP') 

f, (ax1, ax2) = plt.subplots(1, 2) 
ax1.plot(df0.resample('D').size()) 

ax1.set_xlim([pd.to_datetime('2017-01-27'), pd.to_datetime('2017-04-30')]) 

ax1.xaxis.set_major_formatter(ticker.FixedFormatter 
(df0.index.strftime('%Y-%m-%d'))) 

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=15) 

ax2.plot(df1.resample('D').size()) 
ax2.set_xlim([pd.to_datetime('2017-03-22'), pd.to_datetime('2017-04-29')]) 
ax2.xaxis.set_major_formatter(ticker.FixedFormatter(df1.index.strftime 
('%Y-%m-%d'))) 
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=15) 
plt.show() 

Output: (https://www.dropbox.com/s/z21koflkzglm6c3/figure_1.png?dl=0) enter image description here

Liên kết tôi đã cố gắng làm theo:

Bất kỳ giúp đỡ được nhiều đánh giá cao.

Trả lời

7

Làm ví dụ tái sản xuất, chúng ta có thể tạo ra các tập tin văn bản sau đây (data/timestamp01.csv):

TIMESTAMP;eventid 
2017-03-20 02:38:24;1 
2017-03-21 05:59:41;1 
2017-03-23 12:59:58;1 
2017-03-24 01:00:07;1 
2017-03-27 03:00:13;1 

(tương tự cho data/timestamp00.csv). Sau đó chúng tôi có thể đọc chúng trong

import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

df1 = pd.read_csv('data/timestamp01.csv', parse_dates=True, index_col='TIMESTAMP', sep=";") 
df0 = pd.read_csv('data/timestamp00.csv', parse_dates=True, index_col='TIMESTAMP', sep=";") 

Vẽ họ

f, (ax1, ax2) = plt.subplots(1, 2) 

ax1.plot(df0.resample('D').size()) 
ax2.plot(df1.resample('D').size()) 

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=30, ha="right") 
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=30, ha="right") 
plt.show() 

kết quả trong

enter image description here

đó là cốt truyện như mong muốn.

Các vấn đề liên quan