Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle invert_x/yaxis options on Image/RGB #1872

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions holoviews/plotting/bokeh/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ def get_data(self, element, ranges=None, empty=False):
if isinstance(element, Image):
l, b, r, t = element.bounds.lbrt()
else:
img = np.flipud(img.T)
img = img.T[::-1] if self.invert_yaxis else img.T
l, b, r, t = element.extents
dh, dw = t-b, r-l
if type(element) is Raster:

# Ensure axis inversions are handled correctly
if self.invert_xaxis:
l, r = r, l
img = img[:, ::-1]
if self.invert_yaxis:
b, t = t, b
if type(element) is not Raster:
img = img[::-1]
dh, dw = t-b, r-l

mapping = dict(image='image', x='x', y='y', dw='dw', dh='dh')
if empty:
Expand Down Expand Up @@ -62,9 +69,6 @@ class RGBPlot(RasterPlot):
_plot_methods = dict(single='image_rgba')

def get_data(self, element, ranges=None, empty=False):
l, b, r, t = element.bounds.lbrt()
dh, dw = t-b, r-l

img = np.dstack([element.dimension_values(d, flat=False)
for d in element.vdims])
if img.ndim == 3:
Expand All @@ -79,6 +83,16 @@ def get_data(self, element, ranges=None, empty=False):
#convert image NxM dtype=uint32
img = img.view(dtype=np.uint32).reshape((N, M))

# Ensure axis inversions are handled correctly
l, b, r, t = element.bounds.lbrt()
if self.invert_xaxis:
l, r = r, l
img = img[:, ::-1]
if self.invert_yaxis:
b, t = t, b
img = img[::-1]
dh, dw = t-b, r-l

mapping = dict(image='image', x='x', y='y', dw='dw', dh='dh')
if empty:
data = dict(image=[], x=[], y=[], dw=[], dh=[])
Expand Down
54 changes: 53 additions & 1 deletion tests/testplotinstantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from holoviews.element import (Curve, Scatter, Image, VLine, Points,
HeatMap, QuadMesh, Spikes, ErrorBars,
Scatter3D, Path, Polygons, Bars, Text,
BoxWhisker, HLine)
BoxWhisker, HLine, RGB)
from holoviews.element.comparison import ComparisonTestCase
from holoviews.streams import Stream, PointerXY, PointerX
from holoviews.operation import gridmatrix
Expand Down Expand Up @@ -1499,6 +1499,58 @@ def formatter(x):
plot = bokeh_renderer.get_plot(curve).state
self.assertIsInstance(plot.yaxis[0].formatter, FuncTickFormatter)

def test_image_invert_xaxis(self):
arr = np.random.rand(10, 10)
img = Image(arr).opts(plot=dict(invert_xaxis=True))
plot = bokeh_renderer.get_plot(img)
x_range = plot.handles['x_range']
self.assertEqual(x_range.start, 0.5)
self.assertEqual(x_range.end, -0.5)
cdata = plot.handles['source'].data
self.assertEqual(cdata['x'], [0.5])
self.assertEqual(cdata['y'], [-0.5])
self.assertEqual(cdata['dh'], [1.0])
self.assertEqual(cdata['dw'], [-1.0])
self.assertEqual(cdata['image'][0], arr[::-1, ::-1])

def test_image_invert_yaxis(self):
arr = np.random.rand(10, 10)
img = Image(arr).opts(plot=dict(invert_yaxis=True))
plot = bokeh_renderer.get_plot(img)
y_range = plot.handles['y_range']
self.assertEqual(y_range.start, 0.5)
self.assertEqual(y_range.end, -0.5)
cdata = plot.handles['source'].data
self.assertEqual(cdata['x'], [-0.5])
self.assertEqual(cdata['y'], [0.5])
self.assertEqual(cdata['dh'], [-1.0])
self.assertEqual(cdata['dw'], [1.0])
self.assertEqual(cdata['image'][0], arr)

def test_rgb_invert_xaxis(self):
rgb = RGB(np.random.rand(10, 10, 3)).opts(plot=dict(invert_xaxis=True))
plot = bokeh_renderer.get_plot(rgb)
x_range = plot.handles['x_range']
self.assertEqual(x_range.start, 0.5)
self.assertEqual(x_range.end, -0.5)
cdata = plot.handles['source'].data
self.assertEqual(cdata['x'], [0.5])
self.assertEqual(cdata['y'], [-0.5])
self.assertEqual(cdata['dh'], [1.0])
self.assertEqual(cdata['dw'], [-1.0])

def test_rgb_invert_yaxis(self):
rgb = RGB(np.random.rand(10, 10, 3)).opts(plot=dict(invert_yaxis=True))
plot = bokeh_renderer.get_plot(rgb)
y_range = plot.handles['y_range']
self.assertEqual(y_range.start, 0.5)
self.assertEqual(y_range.end, -0.5)
cdata = plot.handles['source'].data
self.assertEqual(cdata['x'], [-0.5])
self.assertEqual(cdata['y'], [0.5])
self.assertEqual(cdata['dh'], [-1.0])
self.assertEqual(cdata['dw'], [1.0])

def test_shared_axes(self):
curve = Curve(range(10))
img = Image(np.random.rand(10,10))
Expand Down