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
CodePudding user response:
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)
CodePudding user response:
To plot values, just convert dates and hours in timestamp (X) and plot values (Y) as Y = fct(X)
Here is the module associated:
Output:
timestamps = [datetime.datetime(2021, 9, 14, 23, 25, 7), datetime.datetime(2021, 9, 14, 23, 27, 37), datetime.datetime(2021, 9, 14, 23, 30, 7), datetime.datetime(2021, 9, 14, 23, 32, 37), datetime.datetime(2021, 9, 14, 23, 35, 7), datetime.datetime(2021, 9, 14, 23, 37, 38), datetime.datetime(2021, 9, 14, 23, 40, 8), datetime.datetime(2021, 9, 14, 23, 42, 38), datetime.datetime(2021, 9, 14, 23, 45, 8), datetime.datetime(2021, 9, 14, 23, 47, 38), datetime.datetime(2021, 9, 14, 23, 50, 8), datetime.datetime(2021, 9, 14, 23, 52, 38), datetime.datetime(2021, 9, 14, 23, 55, 8), datetime.datetime(2021, 9, 14, 23, 57, 38), datetime.datetime(2021, 9, 15, 0, 0, 8), datetime.datetime(2021, 9, 15, 0, 2, 38), datetime.datetime(2021, 9, 15, 0, 5, 8), datetime.datetime(2021, 9, 15, 0, 7, 39), datetime.datetime(2021, 9, 15, 0, 10, 9), datetime.datetime(2021, 9, 15, 0, 12, 39), datetime.datetime(2021, 9, 15, 0, 15, 9), datetime.datetime(2021, 9, 15, 0, 17, 39), datetime.datetime(2021, 9, 15, 0, 20, 9), datetime.datetime(2021, 9, 15, 0, 22, 39)]
values = [-0.008762, -0.008346, -0.008188, -0.007041, -0.00772, -0.00828, -0.009916, -0.008358, -0.00729, -0.008402, -0.007332, -0.008073, -0.009283, -0.008321, -0.008364, -0.00753, -0.007127, -0.006744, -0.008065, -0.008973, -0.00741, -0.008687, -0.009036, -0.010055]
This way is better to use as template is you have a large scale of data format to plot because it is simple to change for addapt. Then is it easy to get the datetime format to correspond to your needs. Indeed matplotlib get a good integration of this library as you can see on X axis of this graph.