1

I am trying to plot information against dates. I have a list of dates in the format 2013-10-15 18:59:45

What I want is to plot without vlines, just a line join every point.

I converted the datetime in epoch but I want to show it in the original format and in vertical labels

I was trying to do something like this: Creating graph with date and time in axis labels with matplotlib

This is the first time I use matlibplot and I spend 3 days trying to do this. I'm in a hurry, can you help me?

Community
  • 1
  • 1

1 Answers1

0

Supposing you have a file named "mydata.log" containing the following data:

2015-11-16 10:25:11 1.5
2015-11-16 10:25:16 2.2
2015-11-16 10:25:21 3.7

You could plot it as follows:

#!/usr/bin/python

import matplotlib.pyplot as plt
from matplotlib import dates
from datetime import datetime

with open("mydata.log") as f:
    data = f.readlines()

x = [row.split(' ')[0:2] for row in data]
x = [datetime.strptime(' '.join(row), '%Y-%m-%d %H:%M:%S') for row in x]
y = [row.split(' ')[2] for row in data]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("title")
ax.set_xlabel('time')
ax.set_ylabel('mydata')

hfmt = dates.DateFormatter('%y/%m/%d %H:%M:%S')
ax.xaxis.set_major_formatter(hfmt)

ax.plot(x,y, c='r', marker='+', label='mydata')

plt.xticks(rotation='vertical')
plt.subplots_adjust(bottom=.3)
plt.grid()
plt.show()
cripton
  • 505
  • 5
  • 9