Python - Matplotlib - 设置 X 轴范围 - 绘制每秒数据包

标签 python matplotlib

我正在编写一个脚本,该脚本可以从 csv 文件中绘制 pps 计数与时间的关系。到目前为止一切正常,但我似乎不知道如何更改 X 轴上刻度/刻度标 checkout 现的时间间隔,我希望有 60 个时间戳/刻度而不是默认值。这就是我所在的位置:

import matplotlib
matplotlib.use('Agg')                   
from matplotlib.mlab import csv2rec     
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from pylab import *

data = csv2rec('tpm_counter.log', names=['packets', 'time']) # reads in the data from the csv as column 1 = tweets column 2 = time
rcParams['figure.figsize'] = 12, 4                           # this sets the ddimensions of the graph to be made
rcParams['font.size'] = 8

fig = plt.figure()
plt.plot(data['time'], data['packets'])                      # this sets the fields to be graphed
plt.xlabel("Time(minutes)")                                  # this sets the x label
plt.ylabel("Packets")                                        # this sets the y label
plt.title("Packets Capture Log: Packets Per Minute")         # this sets the title

#plt.xticks(range(60)) --- nothing shows on the graph if I use this

fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')

plt.savefig('tpm.png')                                      # this sets the output file name

我尝试过 plt.xticks(range(60)) 但当绘图生成时,它上面什么也没有。

最佳答案

bmu 的上述答案有效。但对于其他人来说,看到一种更通用的重新缩放图中 xticks 和 xlabel 的方法可能会有所帮助。我生成了一些示例数据,而不是使用 csv 文件。

import matplotlib            
import matplotlib.pyplot as plt
from pylab import *

time=range(5000) #just as an example
data=range(5000) # just as an example

fig = plt.figure()
plt.plot(time,data)                      # this sets the fields to be graphed
plt.xlabel("Every 60th point")           # this sets the x label
plt.ylabel("Data")                       # this sets the y label
plt.title("Rescaling axes")              # this sets the title

#Slice the data into every 60th point. We want ticks at these points
tickpos=data[::60] 
#Now create a list of labels for each point...
ticklabels=[]
for point in tickpos:
    ticklabels.append(str(point/60))  

plt.xticks(tickpos,ticklabels) # set the xtick positions and labels

plt.savefig('tpm.png')                                     

关于Python - Matplotlib - 设置 X 轴范围 - 绘制每秒数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10661106/

相关文章:

python - Word2Vec + Regression - 数值评分方法

python - pytorch 数据集中每个类的实例数

python - SQLAlchemy 临时表痛苦的解决方案?

python - 在 matplotlib 3d 中旋转 STL 文件

python - 如何使用 seaborn 创建具有连接点的多个系列散点图?

python - Matplotlib 3D 绘图使用颜色图

python - 傅里叶空间滤波

python - 反转给定索引之间的数组

python - 将发散颜色居中至零

python - 如何在 Python 中将折线图拆分为子图?