Search code examples
pythonenthoughtchacotraitsui

Python+Chaco+Traits - rendering bug: unexpected fills of line plot of large data?


Using the minimal example below, the line plot of a large (some 110k points) plot I get (with python 2.7, numpy 1.5.1, chaco/enable/traits 4.3.0) is this:

chaco01

However, that is bizarre, because it is a line plot, and there shouldn't be any filled areas in there? Especially since the data is sawtooth-ish signal? It's as if there is a line at y~=37XX, above which there is color filling?! But sure enough, if I zoom into an area, I get the rendering I expect - without the unexpected fill:

chaco02

Is this a bug - or is there something I'm doing wrong? I tried to use use_downsampling, but it makes no difference...

The test code:

import numpy as np
import numpy.random as npr
from pprint import pprint
from traits.api import HasTraits, Instance
from chaco.api import Plot, ArrayPlotData, VPlotContainer
from traitsui.api import View, Item
from enable.component_editor import ComponentEditor
from chaco.tools.api import PanTool, BetterSelectingZoom

tlen = 112607
alr = npr.randint(0, 4000, tlen)
tx = np.arange(0.0, 30.0-0.00001, 30.0/tlen)
ty = np.arange(0, tlen, 1) % 10000 + alr
pprint(len(ty))


class ChacoTest(HasTraits):

  container = Instance(VPlotContainer)
  traits_view = View(
    Item('container', editor=ComponentEditor(), show_label=False),
    width=800, height=500, resizable=True,
    title="Chaco Test"
  )

  def __init__(self):
    super(ChacoTest, self).__init__()
    pprint(ty)
    self.plotdata = ArrayPlotData(x = tx, y = ty)
    self.plotobj = Plot(self.plotdata)
    self.plotA = self.plotobj.plot(("x", "y"), type="line", color=(0,0.99,0), spacing=0, padding=0, alpha=0.7, use_downsampling=True)
    self.container = VPlotContainer(self.plotobj, spacing=5, padding=5, bgcolor="lightgray")
    #~ container.add(plot)
    self.plotobj.tools.append(PanTool(self.plotobj))
    self.plotobj.overlays.append(BetterSelectingZoom(self.plotobj))

if __name__ == "__main__":
  ChacoTest().configure_traits()

Solution

  • I am able to reproduce the error and talking with John Wiggins (maintainer of Enable), it is a bug in kiva (which chaco uses to paint on the screen): https://github.com/enthought/enable The good news is that this is a bug in one of the kiva backend that you can use. So to go around the issue, you can run your script choosing a different backend:

    ETS_TOOLKIT=qt4.qpainter python <NAME OF YOUR SCRIPT>
    

    if you use qpainter or quartz, the plot looks (on my machine) as expected. If you choose qt4.image (the Agg backend), you will reproduce the issue. Unfortunately, the Agg backend is the default one. To change that, you can set the ETS_TOOLKIT environment variable to that value:

    export ETS_TOOLKIT=qt4.qpainter
    

    The bad news is that fixing this isn't going to be an easy task. Please feel free to report the bug in github (again https://github.com/enthought/enable) if you want to be involved in this. If you don't, I will log it in the next couple of days. Thanks for reporting it!