matplotlib - loglog给了我破碎的情节

时间:2014-06-25 16:15:09

标签: python matplotlib

我将我的情节嵌入到GTK2程序中。从下面的两张图片中可以看出,如果我以线性方式绘制数据,图表就可以了,但使用loglog方法会给我一个无法使用的图表:

Linear sample Log-Log example

使用相同的数据生成这两个图。以下是代码:

win2 = gtk.Window()
win2.set_default_size(400,300)
win2.set_title("Embedding in GTK")
fig2 = Figure(figsize=(5, 8), dpi=100)
freqStart = float(txtFreqStart.get_text())
freqEnd = float(txtFreqEnd.get_text())

vbox2 = gtk.VBox()
win2.add(vbox2)
ax2 = fig2.add_subplot(221)
ax3 = fig2.add_subplot(222)
ax4 = fig2.add_subplot(223)
ax5 = fig2.add_subplot(224)

ya = list(subData[ls / 0.005:le / 0.005])

for i in xrange(0, len(ya)):
    if ya[i] < 0:
        ya[i] = 0

yb = list(subData[ls / 0.005:le / 0.005])

for i in xrange(0, len(yb)):
    if yb[i] > 0:
        yb[i] = 0

Ya = fft(ya)
Yb = fft(yb)

ax2.plot(list(frange(ls, le, 0.005)), ya)
ax3.plot(list(frange(ls, le, 0.005)), yb)

if radLoglog.get_active():
    print ax4.loglog(list(frange(freqStart, freqEnd, 1)), Ya[freqStart:freqEnd])[0].get_xdata()
    ax5.loglog(list(frange(freqStart, freqEnd, 1)), Yb[freqStart:freqEnd])
else:
    print ax4.plot(list(frange(freqStart, freqEnd, 1)), Ya[freqStart:freqEnd])[0].get_xdata()
    ax5.plot(list(frange(freqStart, freqEnd, 1)), Yb[freqStart:freqEnd])

canvas2 = FigureCanvas(fig2)
canvas2.draw()
vbox2.pack_end(canvas2)

toolbar2 = NavigationToolbar(canvas2, win2)
vbox2.pack_start(toolbar2, False, False)

win2.show_all()

1 个答案:

答案 0 :(得分:2)

正如@PaulH在评论中所说,这是正确的行为。

log的负数未定义,numpy通过返回NaN(np.isnan(np.log(-1)))来处理。在绘图时matplotlib掩盖了NaNs。