Issue
So I have a plot which looks like this:
I'd like the numbers on the x-axis to go from 1 to 10, so I can write the label as "Time (microseconds)"
Is there a way to do this?
Thanks
There have been requests to see the code. I intentionally left it out because my issue is simply a case of presentation - altering the values on the x-axis without changing any of the data the plot is showing. The code I'm using is a bit too extensive considering the relatively simple thing I require, but for reference it's here:
def Asymmetry(Ch1,Ch2):
#Turning Ch1 and Ch2 data into bins of equal size
timesCh1, bins1 = numpy.histogram(Ch1,bins=200)
timesCh2, bins2 = numpy.histogram(Ch2,bins=200)
#Converting the int values in the time arrays to flaot to allow for division in next line
timesCh1 = timesCh1.astype(numpy.float)
timesCh2 = timesCh2.astype(numpy.float)
#Combining Ch1 and Ch2 data to find asymmetry
Asymmetry = (timesCh1-timesCh2)/(timesCh1+timesCh2)
A0times = numpy.linspace(0,10E-6,200)
P_Lerror = numpy.sqrt(timesCh1)
P_Rerror = numpy.sqrt(timesCh2)
A0error = 2.0*(numpy.sqrt((1.0/(numpy.power(timesCh1 + timesCh2,4.0)))*(numpy.power(timesCh1,2.0)*numpy.power(P_Lerror,2.0)+numpy.power(timesCh2,2.0)*numpy.power(P_Rerror,2.0))))
#Asymmetry function
def A0(t, B, beta): #t = time, B = magnetic field, beta = detector angle
gamma = 851.616E6
return (-1.0/3.0)*((numpy.sin(gamma*B*t - beta)-numpy.sin(gamma*B*t + beta))/(2.0*beta))
popt,pcov = curve_fit(A0, A0times, Asymmetry,p0=(0.007,0.754),sigma=A0error, absolute_sigma=True)
errors=numpy.sqrt(numpy.diag(pcov))
#Set x-axis
new_time = numpy.linspace(0,10E-6,1000)
new_A0=A0(new_time,*popt)
pyplot.plot(A0times,Asymmetry, label = 'Raw data')
pyplot.errorbar(A0times,Asymmetry,yerr=A0error, fmt=None, label='Error bars')
pyplot.plot(new_time, new_A0,"r-", label = "Fitted data")
pyplot.legend(loc=4)
pyplot.title('py15csa - Asymmetry Data')
pyplot.xlabel('Time (microseconds)')
pyplot.ylabel('Asymmetry')
pyplot.show()
Solution
Put your time data in microseconds, i.e., multiply the time data by 1,000,000. For example:
import matplotlib.pyplot as plt
import numpy as np
# Generate some data
x = np.arange(1000) / 1000 * 10e-6
y = np.sin(2 * np.pi * 1e6 * x)
plt.xkcd()
plt.plot(1e6 * x, y)
plt.xlabel('Time ($\mu$s)')
plt.ylabel('Velocity (furlongs/fortnight)')
plt.title('This is a Sample Plot')
plt.grid(True)
plt.show()
Produces a plot showing microseconds rather than seconds. Note that this does not alter your data, it only generates a copy times 1E6 for the plot.
Answered By - Steve Cohen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.