0
我想用日期數據繪製細分線作爲x軸,並且細分線不應相互重疊。但是,結果圖中有一些重疊。用matplotlib繪製細分線
下面是數據文件命名爲data.csv
event_start,event_end,event_summary,posture
07:30,07:35,setting up desk,2
07:35,07:47,"fill water bottle, wash mug -> toilet",3
07:47,10:20,work(computer work + work discussion with office mate at desk),2
10:20,10:25,toilet,3
10:25,10:42,work,2
11:42,11:44,go find supervisor ,3
11:44,13:00,work (work discussion with supervisor at desk + computer work),2
13:00,13:30,toilet --> get lunch,2
13:30,14:00,Eat lunch,2
14:00,14:05,clean up,3
14:06,14:51,work (computer work + skype),2
14:51,14:59,toilet -> kitchen to fill ater bottle -> printing,3
14:59,16:31,work,2
16:31,16:42,toilet,3
16:42,17:15,work,2
這裏是繪製
import pandas as pd
import matplotlib.pyplot as plt
# plt.style.use('ggplot')
def plot_event(file_name, y_min=0, y_max=5):
df = pd.read_csv(file_name)
df['event_start'] = pd.to_datetime(df['event_start'])
df['event_end'] = pd.to_datetime(df['event_end'])
xs = zip(df['event_start'], df['event_end'])
ys = zip(df['posture'], df['posture'])
plt.ylim(y_min, y_max)
for x, y in zip(xs, ys):
plt.plot(x, y, linewidth=linewidth)
plt.show()
if __name__ == '__main__':
file_name = 'data.csv'
y_min = 1
y_max = 5
linewidth = 8
plot_event(file_name, y_min, y_max)
在這裏,代碼是該圖中,並有不同的段之間的重疊。它是有線的,因爲日期不重疊。