xlab的更改值(刻度)

2024-09-29 23:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个情节是这样的:

Plot

我希望x轴上的数字从1到10,这样我就可以把标签写为“时间(微秒)”

有办法吗?在

谢谢

有人要求查看代码。我故意把它排除在外,因为我的问题只是一个表示的例子——改变x轴上的值而不改变绘图显示的任何数据。考虑到我所需要的相对简单的东西,我使用的代码有点太过广泛,但作为参考,这里是:

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

Tags: numpynewdatatimesqrta0betapower
2条回答

把你的时间数据以微秒为单位,也就是说,把时间数据乘以1000000。例如:

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

生成显示微秒而不是秒的绘图。请注意,这不会改变您的数据,它只会为绘图生成1E6倍的副本。在

如果要使用pylab导入matplotlib:

import pylab
pylab.xlim([0,10])

或者:

^{2}$

你必须分享你正在使用的代码。在

如果要设置x轴记号,可以执行以下操作:

plt.xticks([0, 2, 4, 6, 8, 10])

相关问题 更多 >

    热门问题