【发布时间】:2020-04-27 04:37:17
【问题描述】:
我有一个描述我家 ADSL 速度的日志。 日志条目的格式如下,其中字段为 datetime;level;downspeed;upspeed;testhost:
2020-01-06 18:09:45;INFO;211.5;29.1;0;host:spd-pub-rm-01-01.fastwebnet.it
2020-01-06 18:14:39;WARNING;209.9;28.1;0;host:spd-pub-rm-01-01.fastwebnet.it
2020-01-08 10:51:27;INFO;211.6;29.4;0;host:spd-pub-rm-01-01.fastwebnet.it
(获取完整示例文件 -> https://www.dropbox.com/s/tfmj9ozxe5millx/test.log?dl=0 供您下载以下代码)
我希望在左轴上绘制一个 matplot 图形,其中下载速度、上传速度(值的范围越来越小)并且在 x 刻度线下可能有 45 度角的缩短日期时间。
"""Plots the adsl-log generated log."""
import matplotlib.pyplot as plt
# import matplotlib.dates as mdates
import pandas as pd
# set field delimiter and set column names which will also cause reading from row 1
data = pd.read_csv("test.log", sep=';', names=[
'datetime', 'severity', 'down', 'up', 'loss', 'server'])
# we need to filter out ERROR records (with 0 speeds)
indexNames = data[data['severity'] == 'ERROR'].index
data.drop(indexNames, inplace=True)
# convert datetime pandas objecti to datetime64
data['datetime'] = pd.to_datetime(data['datetime'])
# use a dataframe with just the data I need; cleaner
speeds_df = data[['datetime', 'down', 'up']]
speeds_df.info() # this shows datetime column is really a datetime64 value now
# now let's plot
fig, ax = plt.subplots()
y1 = speeds_df.plot(ax=ax, x='datetime', y='down', grid=True, label="DL", legend=True, linewidth=2,ylim=(100,225))
y2 = speeds_df.plot(ax=ax, x='datetime', y='up', secondary_y=True, label="UL", legend=True, linewidth=2, ylim=(100,225))
plt.show()
我现在正在获取我需要的绘图,但希望能对上述代码中的 ax、y1 和 y2 轴的作用进行一些澄清。
【问题讨论】:
-
幸运的中风 ;) 通过在我的地块之前添加一个 fig, ax = plt.subplots() 行并在两者中使用 ax=ax 参数,设法几乎到达那里。仍然对我的代码生成的 ax 和 y1、y2 的语义感到困惑。
标签: python pandas datetime matplotlib