简体   繁体   English

使用文件中的数据绘制日期和时间(x 轴)与值(y 轴)

[英]Plot date and time (x axis) versus a value (y axis) using data from file

I have data in a file (.dat) in the format of我在文件 (.dat) 中有数据,格式为

%dd %mm %yyyy %HH %MM %SS value %dd %mm %yyyy %HH %MM %SS 值

separated by spaces.用空格隔开。 I would like to plot the day, month, year and time on the x-axis, and the value on the y-axis.我想在 x 轴上绘制日、月、年和时间,在 y 轴上绘制值。 It should always be reading it from the file, as I have many files that are very large to analyze.它应该始终从文件中读取它,因为我有许多非常大的文件需要分析。

My latest attempt:我最近的尝试:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from time import gmtime, strftime
date, time, level = np.loadtxt('my_file.txt', unpack=True, usecols = (0,1,2,3), converters={ 0,1: mdates.strpdate2num('%dd/%mm/%YY %HH:%MM')}) #read format of file
# then to plot 
plt.plot_date(x=date, y=level, fmt='%dd/%mm/%YY %HH:%MM') # fmt is changed from r- 
plt.title('title')
plt.ylabel('Waterlevel (m)')
plt.grid(True)
plt.show()

If I have understood your problem correctly, I believe this is a possible solution:如果我正确理解了您的问题,我相信这是一个可能的解决方案:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np    

# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d %m %Y %H %M %S'))

# Read data from 'file.dat'
dates, levels = np.genfromtxt('file.dat',    # Data to be read
                              delimiter=19,  # First column is 19 characters wide
                              converters={0: datefunc}, # Formatting of column 0
                              dtype=float,   # All values are floats
                              unpack=True)   # Unpack to several variables

fig = plt.figure()
ax = fig.add_subplot(111)

# Configure x-ticks
ax.set_xticks(dates) # Tickmark + label at every plotted point
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))

ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('title')
ax.set_ylabel('Waterlevel (m)')
ax.grid(True)

# Format the x-axis for dates (label formatting, rotation)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()

fig.show()

With file.dat being egfile.dat例如

01 06 2013 00 00 00 24.23
02 06 2013 01 00 00 22.23
03 06 2013 02 00 00 21.43
04 06 2013 03 00 00 24.32
04 06 2013 14 30 00 23.42
06 06 2013 03 00 00 24.32
07 06 2013 19 20 00 23.54
08 06 2013 03 00 00 26.23
08 06 2013 19 00 00 24.43
10 06 2013 12 40 00 23.22

the output becomes like this:输出变成这样:在此处输入图片说明

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

相关问题 使用txt文件(Python)中的数据绘制日期和时间(x轴)与值(y轴)的关系 - Plot date and time (x axis) versus a value (y axis) using data from txt file (Python) 使用txt文件(Python)中的数据绘制日期和时间(x轴)与值(y轴)(仅用空格分隔的列) - Plot date and time (x axis) versus a value (y axis) using data from txt file (Python) (columns separated only by spaces) 如何在X轴上相对于Y值绘制日期和时间(Python) - How to plot date and time in X axis against Y value (Python) 来自 .txt 文件的数据的 Plot 直方图,其中 x 轴上的名称和 y 轴上的数字 - Plot histogram for data from .txt file with names on x-axis and number on y axis 如何在 y 轴上一起绘制日期和时间 - How to plot date and time in the y axis together 如何在y轴上绘制日期,在x轴上绘制小时? - How to plot date on y-axis and hours on x-axis? 如何在Python中将日期列绘制为x轴并在y轴上绘制数据 - How to plot date column as x-axis and data on y axis in Python 获取matplotlib以绘制x和y轴数据 - Get matplotlib to plot x & y axis data 使用排序文件绘制 X 轴与原始文件中相应的 Y 值 - Using sorted file to plot X-axis with corresponding Y-values from the original file 使用日期索引时间序列数据绘制x轴上的经过时间 - Plot elapsed time on x axis using date indexed time-series data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM