0

我正在尝试显示力与时间的关系图,并且测量频率可能会发生变化。例如,80 个样本/秒、100 个/秒等。此频率作为“速率”加载。我可以将绘图安排为以任何速率除以秒,但我无法以秒为单位标记 x 轴,只能以样本为单位。我被阻止发布输出图像,但这个测试文件的速率为 80/秒,X 轴标记为 80、160、240、320 等。我怎样才能让它在几秒钟内标记,即 1 , 2, 3, 4 等等?

import numpy as np
import pylab as p
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

x = read_csv_file('test2.csv')
fnum = int(x[0][0])    # run number
rate = int(x[0][1])    # data rate
unit = int(x[0][2])    # 0 = metric
span_r = int(x[0][3])  # calibration setting
span_w = int(x[0][4])  # calibration setting

load = np.loadtxt(x[1], delimiter=',', unpack=True)

xfmt = "1/%d sec"      # x axis label

if unit:
  span_r = span_r*454
  yfmt = "Pounds Force"
else:
  span_r = span_r*9.81
  yfmt = "Newtons"


fig = plt.figure()
ax = fig.add_subplot(111)
majorLocator = MultipleLocator(rate)
majorFormatter = FormatStrFormatter('%d')
ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(majorFormatter)
ax.plot(load * span_w / span_r)
plt.xlabel(xfmt % rate)
plt.ylabel(yfmt)
plt.grid(True)
plt.show()
4

2 回答 2

1

创建一个时间数组,并plot()使用 xaxis 数据和 yaxis 数据调用,例如pl.plot(x, y)

import pylab as pl
import numpy as np

period = 0.01
data = np.random.rand(1230)
t = np.arange(0, len(data)*period, period)
pl.plot(t, data)
pl.show()
于 2012-05-23T01:41:20.777 回答
1

请参阅如何更改 pyplot 轴的增量

跟随那个:

ax.set_xticks( np.arange(upper-x-bound) )
于 2012-05-23T01:48:00.943 回答