0

I have a question how to correctly plot the date time of my pandas datafile:

headers = ['date', 'time', 'level']
dtypes = {'date': 'str', 'time': 'str','level': 'float'}

filen = 'level-11-12-2018.lvm'
df = pd.read_csv(filen, delimiter=' ', header=None, names=headers, dtype= dtypes, parse_dates=[['date', 'time']])
fig, ax = plt.subplots()
ax.plot_date(df['date_time'], df['level'],'o', label='level')

When I print the date_time column the output is correct, but in the matplotlib plot it is not correct the full day.

print(df):

             date_time     level
0  2018-11-12 00:11:54  261.0564
1  2018-11-12 00:42:03  262.8177
2  2018-11-12 01:12:13  263.9395
3  2018-11-12 01:42:23  264.9376
4  2018-11-12 02:12:32  267.2714
5  2018-11-12 02:42:42  268.3845
6  2018-11-12 03:12:52  269.3476
7  2018-11-12 03:43:02  270.1414
8  2018-11-12 04:13:12  270.9120
9  2018-11-12 04:43:22  271.6264
10 2018-11-12 05:13:32  272.4339
11 2018-11-12 05:43:42  273.3226
12 2018-11-12 06:13:51  274.1788
13 2018-11-12 06:44:01  274.9057
14 2018-11-12 07:14:10  275.6401
15 2018-11-12 07:44:20  276.3888
16 2018-11-12 08:14:29  277.1319
17 2018-11-12 08:44:40  277.8713
18 2018-11-12 09:14:49  278.6563
19 2018-11-12 09:44:59  279.5125
20 2018-11-12 10:15:09  280.5232
21 2018-11-12 10:45:19  281.6038
22 2018-11-12 11:15:29  282.5887
23 2018-11-12 11:45:39  283.5268
24 2018-11-12 12:15:49  284.4925
25 2018-11-12 12:45:59  285.5137
26 2018-11-12 13:16:09  286.5162
27 2018-11-12 13:46:19  287.5524
28 2018-11-12 14:16:28  288.5737
29 2018-11-12 14:46:38  289.6199
30 2018-11-12 15:16:48  290.6105
31 2018-11-12 15:46:58  291.5811
32 2018-11-12 16:17:07  292.5799
33 2018-11-12 16:47:17  295.1786
34 2018-11-12 17:17:29  296.1767
35 2018-11-12 17:47:39  297.1735
36 2018-11-12 18:17:49  298.1223
37 2018-11-12 18:47:59  298.9085
38 2018-11-12 19:18:10  299.9747
39 2018-11-12 19:48:19  300.8697
40 2018-11-12 20:18:29  301.7941
41 2018-11-12 20:48:38  302.7222
42 2018-11-12 21:18:48  303.6684
43 2018-11-12 21:48:58  304.6440
44 2018-11-12 22:19:07  305.6458
45 2018-11-12 22:49:17  306.6508
46 2018-11-12 23:19:26  307.7308
47 2018-11-12 23:49:36  308.7820

Can you help me how to get the right date_time format in the plot: Plot with matplotlib date_time axis not like the print output

So basically it should be 11th of December 2018 and then time series of a day.

4
  • 1
    The plot seems correct to me. What exactly is not "right"? Commented Dec 13, 2018 at 13:01
  • The data in the dataframe is year month day? In matplotlib it is reversed and seems normal to me. Do you want it reversed there as well? Commented Dec 13, 2018 at 13:02
  • The month and day is inversed here like 11th of December 2018 Commented Dec 13, 2018 at 16:32
  • The ISO standard on dates is YYYY-MM-DD. The pandas dataframe hence shows the 12th of november 2018. The matplotlib plot shows this date as well. If you want a different date, you need to question the way you produce the pandas dataframe. Commented Dec 13, 2018 at 21:06

1 Answer 1

0

The problem were french date formats like: 08/12/2018 for french dates like this you should use:

df = pd.read_csv(filen, delimiter=' ', header=None, names=headers, dtype= dtypes, parse_dates=[['date', 'time']], dayfirst=True)

with the option dayfirst=True like written for example here: Specifying date format when converting with pandas.to_datetime

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.