在pyqt中使用matplotlib的loglog图 - 轴消失

时间:2014-11-08 17:23:55

标签: python matplotlib pyqt pyqt4

我正在尝试使用PyQt4内的matplotlib绘图库来绘制loglog图。我在课程中添加了两行代码:

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""
        def compute_initial_figure(self):
            t = arange(0.0, 3.0, 0.01)
            s = abs((t * 1E+1) * sin(2 * pi * t) + 1E1)

            self.axes.plot(t, s)
            self.axes.set_yscale('log') #added code
            self.axes.set_xscale('log') #added code

然后轴消失了,下面出现错误。我想问你如何在PyQt4中使用matplotlib库来绘制loglot图。如下图所示。 Python 2.7.5,matplotlib 1.3.0,PyQt 4.10.4

enter image description here

Traceback (most recent call last):
  File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_qt4.py", line 299, in resizeEvent
    self.draw()
  File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 148, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\Lib\site-packages\matplotlib\figure.py", line 1034, in draw
    func(*args)
  File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\Lib\site-packages\matplotlib\axes.py", line 2086, in draw
    a.draw(renderer)
  File "C:\Python27\Lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python27\Lib\site-packages\matplotlib\axis.py", line 1093, in draw
    renderer)
  File "C:\Python27\Lib\site-packages\matplotlib\axis.py", line 1042, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "C:\Python27\Lib\site-packages\matplotlib\text.py", line 754, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "C:\Python27\Lib\site-packages\matplotlib\text.py", line 329, in _get_layout
    ismath=ismath)
  File "C:\Python27\Lib\site-packages\matplotlib\backends\backend_agg.py", line 210, in get_text_width_height_descent
    self.mathtext_parser.parse(s, self.dpi, prop)
  File "C:\Python27\Lib\site-packages\matplotlib\mathtext.py", line 3009, in parse
    self.__class__._parser = Parser()
  File "C:\Python27\Lib\site-packages\matplotlib\mathtext.py", line 2193, in __init__
    - ((lbrace + float_literal + rbrace)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

1 个答案:

答案 0 :(得分:2)

这可能是因为您的x范围包含0。真实日志比例不能达到0.如果要包含0,则需要使用'semilog'

从您收到的错误消息判断,我敢打赌您使用的是相当旧的matplotlib版本?对于它的价值,在较新的版本中,包括0会导致比例自动更改为semilog而不是log

无论哪种方式,尝试指定一个半音阶,看看是否有帮助。 E.g。

self.axes.set(xscale='semilog', yscale='semilog')