user40551
user40551

Reputation: 365

how to stop pyplot from reversing the values in my x-axis

Signal is 2048 long integer array. The plot is shown below.

enter image description here

See that the velocity axis has negative values on left and positive values on the right. When I created the velocity array it starts with positive numbers and ends with negative numbers, why does the x axis reverse the plot? how do I get it to plot left to right instead of right to left? (positive to negative instead of negative to positive)

thanks


import numpy.polynomial.polynomial as poly
from scipy import asarray as ar,exp, optimize
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

signal = np.genfromtxt("rawdata/g924310/r_g924310_b1.txt")

for i in range(1,2048,1):
    vel[i] = (1024.5 - i)* 0.137177
signal = np.genfromtxt("rawdata/g924310/r_g924310_b1.txt")

plt.plot(vel,signal,'b+:',label='scan')
plt.legend()
plt.title('1667 MHz (92.4, -31.0) - Antenna Temp v Vel')
plt.xlabel('Velocity')
plt.ylabel('Antenna Temp (K)')
plt.show()

Upvotes: 4

Views: 3876

Answers (2)

VBB
VBB

Reputation: 1325

You can also say plt.xlim(vel[0], vel[-1]) to directly force your array first and last elements to be the start and end points of your plot.

Upvotes: 1

ngoldbaum
ngoldbaum

Reputation: 5580

By default matplotlib will order axes from smallest to largest. You can override that default for the x-axis by calling invert_xaxis on the Axes object:

from matplotlib import pyplot as plt
import numpy as np
x = np.array([3, 2, 1])
y = np.array([4, 5, 6])
plt.plot(x, y)
ax = plt.gca()
ax.invert_xaxis()
plt.show()

enter image description here

Upvotes: 4

Related Questions