简体   繁体   English

如何在 python 中使用 matplotlib 和 pandas 绘制 CSV 数据

[英]How to plot CSV data using matplotlib and pandas in python

I have a python code in which I read a csv file using pandas and store date and time in one column Datetime .我有一个 python 代码,我在其中使用 pandas 读取了一个 csv 文件,并将日期和时间存储在一列Datetime中。 Now i want to plot Sensor Value on y-axis and datatime on x-axis.现在我想在 y 轴上绘制传感器值,在 x 轴上绘制数据时间。 How can i achieve this?我怎样才能做到这一点? My code is below:我的代码如下:

import pandas as pd
import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Sensor Value','Date','Time']
df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV',parse_dates=     {"Datetime" : [1,2]},names=headers)
print (df)

Heres some rows from dataset:下面是数据集中的一些行:

                      Datetime  Sensor Value
0     2017/02/17  19:06:17.188             2
1     2017/02/17  19:06:22.360            72
2     2017/02/17  19:06:27.348            72
3     2017/02/17  19:06:32.482            72
4     2017/02/17  19:06:37.515            74
5     2017/02/17  19:06:42.580            70
6     2017/02/17  19:06:47.660            72

I have a python code in which I read a csv file using pandas and store date and time in one column Datetime .我有一个 python 代码,我在其中使用 pandas 读取一个 csv 文件并将日期和时间存储在一列Datetime 中 Now i want to plot Sensor Value on y-axis and datatime on x-axis.现在我想在 y 轴上绘制传感器值,在 x 轴上绘制数据时间。 How can i achieve this?我怎样才能做到这一点? My code is below:我的代码如下:

import pandas as pd
import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Sensor Value','Date','Time']
df = pd.read_csv('C:/Users\Lala Rushan\Downloads\DataLog.CSV',parse_dates=     {"Datetime" : [1,2]},names=headers)
print (df)

Heres some rows from dataset:这是数据集中的一些行:

                      Datetime  Sensor Value
0     2017/02/17  19:06:17.188             2
1     2017/02/17  19:06:22.360            72
2     2017/02/17  19:06:27.348            72
3     2017/02/17  19:06:32.482            72
4     2017/02/17  19:06:37.515            74
5     2017/02/17  19:06:42.580            70
6     2017/02/17  19:06:47.660            72

Updated solution for Python 3.9 with date in the format '2022-01-11 23:57' :更新了 Python 3.9 的解决方案,日期格式为 '2022-01-11 23:57' :

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')

df['DATE'] = pd.to_datetime(df['DATE'], format='%m/%d/%Y %H:%M')

x = df['DATE']
y = df['Sensor Value']

plt.plot(x,y)
# beautify the x-labels
plt.gcf().autofmt_xdate()

plt.show()

To get this code to work on the machine I'm currently coding on (MacOS 10.14) with Python 2.7.16, I needed to declare the row of the CSV file that the headers are on.为了让这段代码在我目前使用 Python 2.7.16 编码的机器(MacOS 10.14)上工作,我需要声明标题所在的 CSV 文件的行。 So this is a header=1 in the read_csv section, as is recommended on the official pandas read_csv page here .所以这是 read_csv 部分中的header=1 ,正如官方 pandas read_csv 页面在这里所推荐的那样。

My code is below:我的代码如下:

 import pandas as pd from datetime import datetime import csv import matplotlib.pyplot as plt import matplotlib.dates as mdates headers = ['sensor_data','Date'] df = pd.read_csv('output.csv',header=1,names=headers) df['Date']= pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S') df['Date']= df['Date'].map(lambda x: datetime.strptime(str(x), '%Y-%m-%d %H:%M:%S')) x = df['Date'] print(x) y = df['sensor_data'] # plot plt.plot(x,y) # beautify the x-labels plt.gcf().autofmt_xdate() plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM