Pyplot - 更改pyplot中的x轴编号

时间:2012-05-23 01:25:26

标签: python matplotlib

我试图显示力与时间的关系图,测量频率可能会发生变化。例如,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()

2 个答案:

答案 0 :(得分:1)

创建一个时间数组,并使用xaxis数据和yaxis数据调用plot(),例如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()

答案 1 :(得分:1)

请参阅How to change the amount of increments in pyplot axis

之后:

ax.set_xticks( np.arange(upper-x-bound) )