matplotlib pyplot 2在同一图中具有不同轴的图

时间:2014-11-12 20:52:45

标签: python matplotlib plot axes

我对matplotlib.pyplot有一个小问题,我希望有人可能之前遇到过它。

我的数据包含X,Y,e值,它们是变量的X,Y测量值,e是Y中测量值的误差。我需要以对数对数刻度绘制它们。

我使用plt.errorbars函数绘制它们,然后将yscale和xscale设置为log,这样可以正常工作。但我还需要在同一图表上绘制一条需要线性比例的线。

我可以单独完成这些情节,但如果可能的话,我希望将它们放在同一图像中。你有什么想法?我正在发布我现在所做的事情。

干杯, Kimon的

    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,100,1000次幂的10​​,但我自己的轴标签有我想要的间距,因此我使用的是plt.xticks。我尝试过ax1.set_yticks和ax1.set_yticklabes但没有成功。以下是我得到的图片。

我没有足够的声誉来发布图片,但这里是上传的链接

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

我的点的值应该是x range = 40 - 80和y range = 5 -200,因为现在是拟合线。

2 个答案:

答案 0 :(得分:1)

您可以使用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刻度,如下所示:

ax2.set_xticks([])
ax2.set_yticks([])

enter image description here

答案 1 :(得分:0)

我无法使用错误栏功能获得两组轴,因此我必须将所有内容转换为对数比例,包括我的线性图。下面是我用来获取它可能对某人有用的代码。

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/

相关问题