Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 206
user40551 Avatar asked Oct 17 '25 02:10

user40551


2 Answers

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

like image 116
ngoldbaum Avatar answered Oct 19 '25 16:10

ngoldbaum


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.

like image 42
VBB Avatar answered Oct 19 '25 16:10

VBB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!