Issue
I have days of data in a csv file which is the date/time and the value of the data as shown below:
14/09/2021 23.25.07 -0.008762
14/09/2021 23.27.37 -0.008346
14/09/2021 23.30.07 -0.008188
14/09/2021 23.32.37 -0.007041
14/09/2021 23.35.07 -0.007720
14/09/2021 23.37.38 -0.008280
14/09/2021 23.40.08 -0.009916
14/09/2021 23.42.38 -0.008358
14/09/2021 23.45.08 -0.007290
14/09/2021 23.47.38 -0.008402
14/09/2021 23.50.08 -0.007332
14/09/2021 23.52.38 -0.008073
14/09/2021 23.55.08 -0.009283
14/09/2021 23.57.38 -0.008321
15/09/2021 00.00.08 -0.008364
15/09/2021 00.02.38 -0.007530
15/09/2021 00.05.08 -0.007127
15/09/2021 00.07.39 -0.006744
15/09/2021 00.10.09 -0.008065
15/09/2021 00.12.39 -0.008973
15/09/2021 00.15.09 -0.007410
15/09/2021 00.17.39 -0.008687
15/09/2021 00.20.09 -0.009036
15/09/2021 00.22.39 -0.010055
The time part is as you see is like dd/mm/yyyy hh.mm.ss and the data values are after one tab from the times. As you see the day changes from 14 to 15 as well.
How can this data be plotted in Python. (I use Spyder and have numpy, matplotlib and pandas libraries)
edit:
14/09/2021 16.39.53 0.081907 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.42.23 0.090114 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.44.53 0.083744 25 123 1234 3.29 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.47.23 0.086005 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.49.53 0.113348 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.52.23 0.106089 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.54.53 0.088008 25 123 1234 3.28 1.43 0.00 0.00 0.00 0.00 0.00 0.00
14/09/2021 16.57.23 0.078151 25 123 1234 3.28 1.42 0.00 0.00 0.00 0.00 0.00 0.00
Solution
The data in my .csv file looks like this
datetime value
0 14/09/2021 23.25.07 -0.008762
1 14/09/2021 23.27.37 -0.008346
2 14/09/2021 23.30.07 -0.008188
3 14/09/2021 23.32.37 -0.007041
4 14/09/2021 23.35.07 -0.007720
You can use the following snippet to plot datetime values
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Read .csv
df = pd.read_csv('data.csv')
# Convert str to datetime
df['datetime'] = pd.to_datetime(df['datetime'], format='%d/%m/%Y %H.%M.%S')
# Plot
fig, ax = plt.subplots()
ax.plot(df['datetime'],df['value'])
ax.set_xticks(df['datetime'])
# Format your axis as required
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
ax.xaxis.set_minor_formatter(mdates.DateFormatter("%H:%M"))
_=plt.xticks(rotation=90)
Answered By - Luke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.