user1411442
user1411442

Reputation: 1

Pyplot - Changing the x axis numbering in pyplot

I am trying to display a plot of force against time, and the frequency of measurements can change. For example, 80 samples/sec, 100/sec, etc. This frequency is loaded as 'rate'. I can arrange the plot to be divided by seconds for any rate, but I can't get the x-axis labelled in seconds, only in samples. I'm prevented from posting an image of the output, but this test file has a rate of 80/sec and the X axis is labeled 80, 160, 240, 320 etc. How can I get it to label in seconds, ie 1, 2, 3, 4 etc?

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()

Upvotes: 0

Views: 4643

Answers (2)

HYRY
HYRY

Reputation: 97261

Create a time array, and call plot() with the xaxis data and yaxis data such as 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()

Upvotes: 1

jtmoulia
jtmoulia

Reputation: 660

See How to change the amount of increments in pyplot axis

Following that:

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

Upvotes: 1

Related Questions