enthought / enthought/chaco

Ranges of renderers in Plots don't update properly

Open
#280 3 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
305
Forks
97
PR merge metrics
No merged PRs in 30d

Description

The example code below generates an error:

```
TraitError: The 'range' trait of a GridMapper instance must be a DataRange2D or None, but a value of was specified.
```

If you swap the range setting line to the other order:

```
scatter.index_range = img.index_range
```

it works correctly. If instead you try with value ranges, you get a crash. There are similar problems with any renderer which doesn't subclass from `BaseXYPlot`. Note that we're not messing with the renderer's ranges, but the range of the `Plot` which contains the renderer.

The root problem is in this method: https://github.com/enthought/chaco/blob/master/chaco/plot.py#L1256 It assumes that renderer's index and value ranges match the `Plot`'s, but that is not guaranteed.

Not entirely sure what fix is - it may be that we should be thinking in terms of `x` and `y` ranges for handling these sorts of changes.

```
from numpy import linspace, sin
from numpy import linspace, sin, meshgrid, exp

from chaco.api import ArrayPlotData, HPlotContainer, Plot, jet
from chaco.tools.api import PanTool, ZoomTool
from enable.component_editor import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, View

class ConnectedRange(HasTraits):

container = Instance(HPlotContainer)

traits_view = View(Item('container', editor=ComponentEditor(), show_label=False),
width=1000, height=600, resizable=True,
title="Connected Range")

def __init__(self):
# Create the data and the PlotData object
x = linspace(-14, 14, 100)
y = sin(x) * x**3
plotdata = ArrayPlotData(x = x, y = y)

# Create the scatter plot
scatter = Plot(plotdata)
scatter.plot(("x", "y"), type="scatter", color="blue")

# Create the line plot
x = linspace(0, 10, 50)
y = linspace(0, 5, 50)
xgrid, ygrid = meshgrid(x, y)
z = exp(-(xgrid*xgrid + ygrid*ygrid) / 100)

plotdata = ArrayPlotData(imagedata = z)
img = Plot(plotdata)
# Create an image plot in the Plot
img.img_plot("imagedata", colormap=jet)

# Create a horizontal container and put the two plots inside it
self.container = HPlotContainer(scatter, img)

# Add pan/zoom so we can see they are connected
scatter.tools.append(PanTool(scatter))
scatter.tools.append(ZoomTool(scatter))
img.tools.append(PanTool(img))
img.tools.append(ZoomTool(img))

# Set the two plots' ranges to be the same
img.index_range = scatter.index_range

#===============================================================================
# demo object that is used by the demo.py application.
#===============================================================================
demo=ConnectedRange()
if __name__ == "__main__":
demo.configure_traits()
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.