我正在编写一个脚本,该脚本可以从 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/