Plot CSV of time series data with unixtime value in Python (matplotlib)

↓ Suppose you want to plot the data of such a CSV file.

$ cat 1.csv
"_time",count
1604847600,0
1604848200,2
1604848800,0
1604849400,2
1604850000,0
1604850600,3
1604851200,3
1604851800,0
1604852400,3
1604853000,1

1604847600 is a notation called UNIX TIME, which actually means as follows.

2020-11-09 00:00:00,0
2020-11-09 00:10:00,2
2020-11-09 00:20:00,0

Consider a program that converts this. .. ..

     1from datetime import datetime
     2
     3import matplotlib.pyplot as plt
     4import matplotlib.dates as md
     5import dateutil
     6
     7import sys
     8args = sys.argv
     9
    10x = []
    11y = []
    12
    13f = open(args[1], 'r')
    14
    15line = f.readline()
    16line = f.readline()
    17
    18while line:
    19    tmp = line.split(",")
    20    dt_object = datetime.fromtimestamp(int(tmp[0]))
    21
    22    x.append(str(dt_object))
    23    y.append(int(str(str(tmp[1]).replace('\n',''))))
    24
    25    line = f.readline()
    26f.close()
    27
    28counter = 0
    29for i in x:
    30    print(x[counter] + "," + str(y[counter]))
    31    counter = counter + 1

https://github.com/RuoAndo/qiita/blob/master/python-timeseries-plot/1.py

Make a list to store the time stamp and value on lines 10 and 11.

    10 x = []
    11 y = []

Convert UNIX TIME to a date on line 20.

    20    dt_object = datetime.fromtimestamp(int(tmp[0]))

Try to run it. .. ..

$ python 1.py 1.csv | more
2020-11-09 00:00:00,0
2020-11-09 00:10:00,2
2020-11-09 00:20:00,0
2020-11-09 00:30:00,2
2020-11-09 00:40:00,0
2020-11-09 00:50:00,3
2020-11-09 01:00:00,3
2020-11-09 01:10:00,0
2020-11-09 01:20:00,3
2020-11-09 01:30:00,1
2020-11-09 01:40:00,0
2020-11-09 01:50:00,1
2020-11-09 02:00:00,2
2020-11-09 02:10:00,2

(`) B

After that, make a program to plot this.

Take a look at the code. ..

     1from datetime import datetime
     2
     3import matplotlib.pyplot as plt
     4import matplotlib.dates as md
     5import dateutil
     6
     7x = []
     8y = []
     9
    10f = open('1.csv', 'r')
    11
    12line = f.readline()
    13line = f.readline()
    14
    15while line:
    16    tmp = line.split(",")
    17    dt_object = datetime.fromtimestamp(int(tmp[0]))
    18
    19    x.append(str(dt_object))
    20    y.append(int(str(str(tmp[1]).replace('\n',''))))
    21
    22    line = f.readline()
    23f.close()
    24
    25counter = 0
    26for i in x:
    27    print(x[counter] + "," + str(y[counter]))
    28    counter = counter + 1
    29
    30dates = [dateutil.parser.parse(s) for s in x]
    31
    32plt.subplots_adjust(bottom=0.2)
    33plt.xticks( rotation= 80 )
    34ax=plt.gca()
    35xfmt = md.DateFormatter('%Y-%m-%d %H:%M')
    36ax.xaxis.set_major_formatter(xfmt)
    37plt.plot(dates,y)
    38plt.show()

https://github.com/RuoAndo/qiita/blob/master/python-timeseries-plot/5.py

Plot the list x (timestamp), y (value) on lines 30-38.

    30dates = [dateutil.parser.parse(s) for s in x]
    31
    32plt.subplots_adjust(bottom=0.2)
    33plt.xticks( rotation= 80 )
    34ax=plt.gca()
    35xfmt = md.DateFormatter('%Y-%m-%d %H:%M')
    36ax.xaxis.set_major_formatter(xfmt)
    37plt.plot(dates,y)
    38plt.show()

(`) B

Recommended Posts

Plot CSV of time series data with unixtime value in Python (matplotlib)
[Python] Plot time series data
Reformat the timeline of the pandas time series plot with matplotlib
Graph time series data in Python using pandas and matplotlib
[Python] Accelerates loading of time series CSV
Get time series data from k-db.com in Python
Read Python csv data with Pandas ⇒ Graph with Matplotlib
View details of time series data with Remotte
Time series plot / Matplotlib
Format and display time series data with different scales and units with Python or Matplotlib
Full-width and half-width processing of CSV data in Python
Consolidate a large number of CSV files in folders with python (data without header)
Implementation of clustering k-shape method for time series data [Unsupervised learning with python Chapter 13]
Try scraping the data of COVID-19 in Tokyo with Python
How to generate exponential pulse time series data in python
[Homology] Count the number of holes in data with Python
Python: Time Series Analysis: Preprocessing Time Series Data
Division of timedelta in Python 2.7 series
"Measurement Time Series Analysis of Economic and Finance Data" Solving Chapter End Problems with Python
How to calculate the sum or average of time series csv data in an instant
Time series plot started ~ python edition ~
Heatmap with Dendrogram in Python + matplotlib
Differentiation of time series data (discrete)
Time series analysis 3 Preprocessing of time series data
Lognormal probability plot with Python, matplotlib
Forecasting time series data with Simplex Projection
Get additional data in LDAP with python
Data input / output in Python (CSV, JSON)
Predict time series data with neural network
Try working with binary data in Python
Conversion of time data in 25 o'clock notation
Recommendation of Altair! Data visualization with Python
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
Power of forecasting methods in time series data analysis Semi-optimization (SARIMA) [Memo]
Plot multiple maps and data at the same time with Python's matplotlib
Rewrite the field creation node of SPSS Modeler with Python. Feature extraction from time series sensor data
Convenient time series aggregation with TimeGrouper in pandas
Visualize corona infection data in Tokyo with matplotlib
Summary of how to read numerical data with python [CSV, NetCDF, Fortran binary]
[Python] limit axis of 3D graph with Matplotlib
Acquisition of time series data (daily) of stock prices
Read table data in PDF file with Python
Real-time visualization of thermography AMG8833 data in Python
Smoothing of time series and waveform data 3 methods (smoothing)
Compare the sum of each element in two lists with the specified value in Python
CSV output of pulse data with Raspberry Pi (CSV output)
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
The story of reading HSPICE data in Python
How to read time series data in PyTorch
Write CSV data to AWS-S3 with AWS-Lambda + Python
Csv in python
Draw a line / scatter plot on the CSV file (2 columns) with python matplotlib
Create 3D scatter plot with SciPy + matplotlib (Python)
Create an animated time series map of coronavirus infection status with python + plotly
A well-prepared record of data analysis in Python
Speed evaluation of CSV file output in Python
Example of reading and writing CSV with Python
Predicting the goal time of a full marathon with machine learning-③: Visualizing data with Python-
Get the value of a specific key in a list from the dictionary type in the list with Python
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file