Python Forum
Plot time series data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot time series data
#1
I have a long list of data looking like this:

   

Minute and Second are always zero since the data is the 1 hour average of a measurement, which is the last column. I would like to plot this data. If there was only two columns, date and measurement, then I would simply do:

import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv('data.csv')
plt.plot(df)
So I am considering to rewrite the dataframe, however, I am unsure if this approach will work. Appreciate any feedback.
Reply
#2
Hi,

if you combine the first six columns to one column holding a datetime object, plotting should be easy.

Regards, noisefloor
schniefen likes this post
Reply
#3
(Mar-04-2023, 03:56 PM)noisefloor Wrote: Hi,

if you combine the first six columns to one column holding a datetime object, plotting should be easy.

Regards, noisefloor

Here is my current solution, which I'd like to shorten if possible:

import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv('data.csv')
df['Date']=pd.to_datetime(df[['Year', 'Month', 'Day', 'Hour', 'Minute','Second']])
df=df.drop(['Year', 'Month', 'Day', 'Hour', 'Minute','Second'], axis=1)
df=df.dropna()
plt.plot(df['Date'],df['HYY_META.VOC_M137_1250'])
Reply
#4
Hi,

what to you mean by "shorten"? The code is already pretty short... If you like to get rid of one line, you can skip line 5 in your code and skip deleting the year, month, ... columns, as there not a real need for it.

You could also shorten the code if the input csv would have a column for datetime already instead of six columns. In case you have an impact on how the csv file is structured.

Regards, noisefloor
schniefen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 3,557 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  Fit straight line to pandas time series data with semilog plot schniefen 2 1,542 Mar-10-2023, 01:08 PM
Last Post: jefsummers
  Help on Time Series problem Kishore_Bill 1 4,821 Feb-27-2020, 09:07 AM
Last Post: Kishore_Bill
  How to plot date series in matplotlib? StrybolData 2 8,375 Jan-25-2018, 07:13 PM
Last Post: StrybolData
  Visualisation of gaps in time series data ulrich48155 11 19,337 Jul-04-2017, 11:47 PM
Last Post: zivoni
  Removing data in a plot ulrich48155 3 3,807 Jun-19-2017, 06:31 PM
Last Post: zivoni
  10fold cross-validation on time series ulrich48155 5 9,214 May-08-2017, 04:36 PM
Last Post: ulrich48155

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020