【问题标题】:loglog plot with matplotlib in pyqt - axes disappear在pyqt中使用matplotlib的loglog图 - 轴消失
【发布时间】:2015-01-05 08:21:37
【问题描述】:

我正在尝试使用 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

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'

【问题讨论】:

    标签: python matplotlib pyqt pyqt4


    【解决方案1】:

    这可能是因为您的 x 范围包括 0。真正的对数刻度不能达到 0。如果要包含 0,则需要改用 'semilog'

    从您收到的错误消息来看,我敢打赌您使用的是相当旧的 matplotlib 版本?值得一提的是,在较新的版本中,包括0 将导致比例自动更改为semilog 而不是log

    无论哪种方式,请尝试指定一个半对数刻度,看看是否有帮助。例如

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

    【讨论】:

    • 感谢您的快速回复。不幸的是我正在升级(python,matplotlib),现在我得到了一些新错误:'ImportError:需要六个 1.3 或更高版本;你有 1.2.0-mpl' 一旦我解决了这个问题,我会回复你的建议。
    • 我尝试了你的建议,但它引发了一个错误:ValueError: Unknown scale type 'semilog' 我还更新了 matplotlib(1.3 到 1.4.2),它适用于第一个代码,它也适用。我也使用“self.axes.lolglog()”得到了正确的结果,但是在日志图中显示网格时出现问题,因为“self.axes.grid(True)”不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2016-01-21
    相关资源
    最近更新 更多