matplotlib pyplot 2在同一个figu中使用不同的轴绘制

2024-09-29 23:24:38 发布

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

我有个小问题matplotlib.pyplot我希望有人以前见过。在

我有包含X,Y,e值的数据,这些值是变量的X,Y测量值,e是Y中测量值的误差。我需要用对数标度来绘制它们。在

我用错误栏函数来绘制它们,然后将yscale和xscale设置为log,这样可以正常工作。但是我还需要在同一个图上画一条线,这个图必须是线性的。在

我可以有单独完成的情节很好,但我希望他们在同一个图像,如果可能的话。你有什么想法吗?我把我现在所做的事都贴出来了。在

干杯, 基蒙

    tdlist = np.array([0.01,0.02,0.05,0.1,0.2,0.3,0.4,0.5,0.8,1,2,5,10,15,20,25,30,40,60,80,100,150,200,250,300,400])
    freqlist=np.array([30,40,50,60,70,80,90,100,110,120,140,160,180,200,220,250,300,350,400,450])

    filename=opts.filename

    data = reader(filename)
    data2 = logconv(data)

    #x,y,e the data. Calculating usefull sums
    x = data2[0]
    y = data2[1]
    e = data2[2]

    xoe2 = np.sum(x/e**2)
    yoe2 = np.sum(y/e**2)
    xyoe2 = np.sum(x*y/e**2)
    oe2 = np.sum(1/e**2)
    x2oe2 = np.sum(x**2/e**2)

    aslope = (xoe2*yoe2-xyoe2*oe2)/(xoe2**2-x2oe2*oe2)
    binter = (xyoe2-aslope*x2oe2)/xoe2
    aerr = np.sqrt(oe2/(x2oe2*oe2-xoe2**2))
    berr = np.sqrt(x2oe2/(x2oe2*oe2-xoe2**2))

    print('slope is ',aslope,' +- ', aerr)
    print('inter is ',binter,' +- ', berr)

    fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax2 = fig.add_axes(ax1.get_position(), frameon=False)

ax1.errorbar(data[0],data[1],yerr=data[2],fmt='o')
ax1.set_xscale('log',basex=10)
ax1.set_yscale('log',basey=10)
ax1.set_yticks([])
ax1.set_xticks([])


ax2.plot(x,aslope*x+binter,'r')
ax2.plot(x,(aslope-aerr)*x+(binter+berr),'--')
ax2.plot(x,(aslope+aerr)*x+(binter-berr),'--')
ax2.set_xscale('linear')
ax2.set_yscale('linear')
plt.xticks(np.log10(freqlist),freqlist.astype('int'))
plt.yticks(np.log10(tdlist),tdlist.astype('float'))

plt.xlabel('Frequency (MHz)')
plt.ylabel('t_s (msec)')
fitndx1 = 'Fit slope '+"{0:.2f}".format(aslope)+u"\u00B1"+"{0:.2f}".format(aerr)
plt.legend(('Data',fitndx1))




plt.show()

按照莫莉的建议,我设法接近了我的目标,但还是没有实现。我正在添加一点关于我正在尝试做什么的信息,它可能会澄清一些事情。在

我正在将ax1设置为使用loglog比例的errobar图。我需要使用errorbar而不是loglog plot,这样我就可以用我的点显示错误。在

我使用ax2来绘制线性拟合的线性比例。在

此外,我不希望x轴和y轴显示的值是10的101001000次方,而是我自己的轴标签具有我想要的间距,因此我使用plt.xticks公司. 我尝试了ax1.set yticks和ax1.set_yticklab,但没有成功。下面是我得到的图像。在

我没有足够的声誉张贴一个图片,但这里是它的链接上传

http://postimg.org/image/uojanigab/

我的点的值应该是x范围=40-80和y范围=5-200,就像现在的拟合线一样。在


Tags: datanppltsumsetdata2ax1ax2
2条回答

我不能让两组轴与errorbar函数一起工作,所以我不得不把所有的东西都转换成对数刻度,包括我的线性图。下面是我用来获取它的代码可能对某些人有用。在

plt.errorbar(data[0],data[1],yerr=data[2],fmt='o')
plt.xscale('log',basex=10)
plt.yscale('log',basey=10)
plt.plot(data[0],data[0]**aslope*10**binter,'r')
plt.plot(data[0],data[0]**(aslope-aerr)*10**(binter+berr),' ')
plt.plot(data[0],data[0]**(aslope+aerr)*10**(binter-berr),' ')
plt.xticks(freqlist,freqlist.astype('int'))
plt.yticks(tdlist,tdlist.astype('float'))
plt.xlabel('Frequency (MHz)')
plt.ylabel('t_s (msec)')
fitndx1 = 'Fit slope '+"{0:.2f}".format(aslope)+u"\u00B1"+"{0:.2f}".format(aerr)
plt.legend(('Data',fitndx1))
plt.show()

这是最终图像的链接

http://postimg.org/image/bevj2k6nf/

可以使用figure的add_suplot方法创建两个重叠的轴。下面是一个例子:

from matplotlib import pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
ax2 = fig.add_axes(ax1.get_position(), frameon=False)

ax1.loglog([1,10,100,1000],[1000,1,100,10])
ax2.plot([5,10,11,13],'r')

plt.show()

log and linear scale on the same plot

然后,可以关闭线性比例图的x和y记号,如下所示:

^{pr2}$

enter image description here

相关问题 更多 >

    热门问题