gpt4 book ai didi

来自 txt(csv) 文件的 Python 图表有 5 列,如何为 y 轴选择第三或第四列?

转载 作者:行者123 更新时间:2023-11-29 20:27:52 25 4
gpt4 key购买 nike

enter image description here我正在学习Python matplotlib。我有 txt 文件,其中包含 4 列。

但我想从 txt 中选择第三列或第四列。

我尝试并研究过,但我的编码中有很多错误。我是Python编程的初学者,所以自己处理起来太困难了。你能帮我一下吗?

Date  |  Time  |  distance  |  speed

2016/08/25 02:19:39 0.0006 0.6406
2016/08/25 02:19:40 0.0013 2.7856
2016/08/25 02:19:40 0.0019 2.4938
2016/08/25 02:19:42 0.0025 2.1624
2016/08/25 02:19:43 0.0031 1.7867
2016/08/25 02:19:45 0.0038 1.2161
2016/08/25 02:19:50 0.0044 0.4524
2016/08/25 02:19:51 0.0050 1.7881
2016/08/25 02:19:54 0.0057 0.7540
2016/08/25 02:19:55 0.0063 2.7822

我想制作一个x轴是日期和时间的图表,y 轴表示距离或速度。

我从互联网上找到了这个来源。源代码的底部适用于 test.txt。

Date  |  Time  |  distance  

2016/08/26 23:45:30 0.0088
2016/08/26 23:45:35 0.0094
2016/08/26 23:45:36 0.0101
2016/08/26 23:45:38 0.0107
2016/08/26 23:45:39 0.0113
2016/08/26 23:45:42 0.0119
2016/08/26 23:45:47 0.0126
2016/08/26 23:45:48 0.0132
2016/08/26 23:45:50 0.0138
2016/08/26 23:45:51 0.0145
2016/08/26 23:45:52 0.0151
2016/08/26 23:45:54 0.0157

代码:

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, '%Y/%m/%d %H:%M:%S'))

# Read data from 'file.dat'
dates, levels = np.genfromtxt('sss.txt', # 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('%Y/%m/%d %H:%M'))
ax.set_ylabel('y')
ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('How many km does my hamster runs?')
ax.set_ylabel('Distance (km)')
ax.grid(True)

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

fig.show()

最佳答案

这是一个如何做到这一点的工作示例。 data.txt 中的数据与第一个示例中的数据相同。我用np.loadtxt加载文本文件。我提出的可选参数是: 1) unpack=True,以便将数据放入不同的变量中,如我的示例所示; 2) skiprows=2 不读取文件头; 3) dtype='string' 将数据解析为字符串。将数据作为字符串加载会强制您将数据转换为 float 或日期时间对象。加载完成后,这是一种使用 matplotlib 绘制数据的简单方法。为了清楚起见,我使用 twinx 来共享 x 轴,因为 x 值是相同的。这能解决您的问题吗?

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
plt.close('all')

filepath = 'data.txt'
date, ttime, dist, vel = np.loadtxt(filepath, unpack=True, skiprows=2, dtype='string')

timestamps = [datetime.strptime(ddate+'-'+tt, '%Y/%m/%d-%H:%M:%S') for ddate, tt in zip(date, ttime)]
dist = map(float, dist)
vel = map(float, vel)

fig, ax_dist = plt.subplots()
ax_vel = ax_dist.twinx()
ax_dist.plot(timestamps, dist, 'r')
ax_vel.plot(timestamps, vel, 'g')
ax_dist.set_ylabel('Distance')
ax_vel.set_ylabel('Velocity')
ax_dist.set_xlabel('Time')

fig.show()

关于来自 txt(csv) 文件的 Python 图表有 5 列,如何为 y 轴选择第三或第四列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com