Python:以对数比例、循环方式绘制错误条,然后保存图像

Python:以对数比例、循环方式绘制错误条,然后保存图像,python,plot,errorbar,loglog,Python,Plot,Errorbar,Loglog,我有58个文件需要打印。其中一些是空的(不重要,我已经用if条件跳过了它们)。我需要在文件中绘制数据,使用对数刻度,带有误差条。最后我想保存这些情节。 我正在使用Python,spyder。 我编写了以下代码: route='/......./' L=np.arange (1,59, 1) for i in range (L.shape[0]): I=L[i] name_sq= 'Spectra_without_quiescent_'+('{}'.format(I))+'.dat

我有58个文件需要打印。其中一些是空的(不重要,我已经用if条件跳过了它们)。我需要在文件中绘制数据,使用对数刻度,带有误差条。最后我想保存这些情节。 我正在使用Python,spyder。 我编写了以下代码:

route='/......./'
L=np.arange (1,59, 1)
for i in range (L.shape[0]):
    I=L[i]
    name_sq= 'Spectra_without_quiescent_'+('{}'.format(I))+'.dat' 
    Q=np.loadtxt(route+name_sq)
    if (len(Q) != 0):
        x=Q[:,1]
        y=Q[:,2]
        z=Q[:,3]
        fig=plt.errorbar(x,y,yerr=z, fmt = 'b')
        fig.set_yscale('log')
        fig.set_xscale('log')
        xlabel='Frequency'
        ylabel='Flux'
        title='Spectrum_'+('{}'.format(I))+'.dat'
        name='Spectrum_without_quiescent_'+('{}'.format(I))+'.pdf'
        fig.savefig(route+name, fig)
但是,当我运行它时,会出现以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "/media/chidiac/My Passport/DOCUMENTS/My_Publications/2-3C273_radio_spectra/Maximum_flux_code.py", line 49, in <module>
    fig.set_yscale('log')
AttributeError: 'ErrorbarContainer' object has no attribute 'set_yscale'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.7/dist packages/spyderlib/widgets/externalshell/sitecustomize.py”,第540行,在runfile中
execfile(文件名、命名空间)
文件“/media/chidiac/My Passport/DOCUMENTS/My_Publications/2-3C273_radio_spectrum/Maximum_flux_code.py”,第49行,在
图设置刻度('log')
AttributeError:“ErrorbarContainer”对象没有属性“set_yscale”
我仍然是Python的初学者,我找不到错误,也找不到如何修复错误。
非常感谢您的帮助

我的一位朋友帮我解决了这个问题,如果有人感兴趣,下面是解决方案:

route='/....../'
L=np.arange (1,59, 1)
print L 
for i in range (L.shape[0]):
    I=L[i]
    name_sq= 'Spectra_without_quiescent_'+('{}'.format(I))+'.dat' 
    Q=np.loadtxt(route+name_sq)
    if (len(Q) != 0):
        x=np.log(Q[:,1])
        y=np.log(Q[:,2])
        z=np.log(Q[:,3])
        fig, ax = plt.subplots(facecolor='w', edgecolor='k')
    plt.errorbar(x,y,yerr=z, fmt = 'b')
    plt.ylabel('Flux', size='x-large')
    plt.xlabel('Frequency', size='x-large')
    title='Spectrum_'+('{}'.format(I))+'.dat'
    name='Spectrum_without_quiescent_'+('{}'.format(I))+'.pdf'
    pylab.savefig(route+name)
第一个技巧是,首先获取数据的日志值,然后绘制它们。由于我不知道有任何命令允许我以对数刻度绘制错误条,我认为这是最好的解决方案。 第二个技巧是使用子图。否则,我在一个图中得到了58条曲线,58次


我希望这个解决方案是有帮助的

这篇文章可能有点老了,但是我也有同样的问题,也许这会在将来帮助别人

您最初的解决方案实际上几乎是正确的。但是,
set_yscale
是轴的方法,而不是图形。因此,if语句中的代码应该如下所示:

import matplotlib.pyplot as plt

# other stuff you did ..

x=Q[:,1]
y=Q[:,2]
z=Q[:,3]
fig = plt.figure()
ax = plt.axes()
ax.set_xscale("log")
ax.set_yscale("log")
ax.errorbar(x,y,yerr=z, fmt = 'b')
ax.set_xlabel("Frequency")
ax.set_ylabel("Flux")
ax.set_title("Spectrum_{}.dat".format(I))
name="Spectrum_without_quiescent_{}.pdf".format(I)
plt.savefig(route+name)
我还调整了
格式
函数的使用

请注意,您的第二个解决方案并不总是正常工作。如果您有非常小的值和小的错误条,这些小值的对数将变大(例如,当使用以10为底的对数时,log(10^(-6))=-6),并且您将有巨大的错误条,尽管您的实际错误很小


长话短说:使用
ax.set_*scale
。很安全。

我建议您将上述答案标记为正确答案。