我有一个日志,它描述了我的家庭ADSL速度。 日志条目采用以下格式,其中字段为datetime;数量减速;提高速度;测试主机:
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轴的作用
多亏了@Parfait,我希望我能正确地理解事情。以下是工作代码:
其中:
我仍然不明白的是: a) 为什么x轴标签似乎只尊重颜色而不尊重字符串值:( b) 为什么df图中的ylim=(n,m)参数不能很好地工作,而我必须使用ax.set_ylim构造
首先,分配y1和y2对象是不必要的,因为以后您将永远不会使用它们。另外,
legend=True
是默认值根据matplotlib.pyplot.subplots文档,
ax
的返回为:对于pandas.DataFrame.plot,
ax
参数:因此,首先要初始化轴对象数组(默认为一个项目
nrow=1
和nrow=2
),然后根据图分配它/它们。现在,通常情况下,您将用ax=ax
覆盖ax的赋值,但由于您使用了一个辅助y轴,因此相互重叠打印:要说明如何扩展轴对象,请参见下面的多个(非重叠)绘图
使用
nrows=2
的多个子批次示例:使用
ncols=2
的多个绘图示例:甚至可以在将日期/时间字段设置为索引后使用
subplots=True
:相关问题 更多 >
编程相关推荐