enthought / chaco

Chaco is a Python package for building interactive and custom 2-D plots.
http://docs.enthought.com/chaco/
Other
292 stars 99 forks source link

ScatterPlot example currently shows marker_size takes integer arrays #846

Open mechworrior opened 1 year ago

mechworrior commented 1 year ago

Problem Description ScatterPlot example scatter_1d currently shows marker_size takes integer arrays as an input This will return a typeerror in the Kiva backend.

Reproduction Steps:

Run the scatter_1d demo from etsdemo. with marker_size=randint(1,5, numpts)

Expected behavior:

This should have generated a scatterplot with different sized points.

OS, Python version: [windows, Python3.8]

mdickinson commented 1 year ago

I can reproduce on macOS. If I change the line

        marker_size=3,  # randint(1,5, numpts),

(line 78 of the current demo script) to

        marker_size=randint(1,5, numpts),

and then run the demo script, I get a blank plot, and the following traceback on the console (repeated several times):

Traceback (most recent call last):
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/qt4/base_window.py", line 255, in paintEvent
    self.handler.paintEvent(event)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/qt4/base_window.py", line 90, in paintEvent
    self._enable_window._paint(event)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/abstract_window.py", line 536, in _paint
    self.component.draw(gc, view_bounds=(0, 0, size[0], size[1]))
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/component.py", line 410, in draw
    self._draw(gc, view_bounds, mode)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/component.py", line 781, in _draw
    self._dispatch_draw(layer, bb, view_bounds, mode)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/container.py", line 270, in _dispatch_draw
    component._dispatch_draw(layer, gc, new_bounds, mode)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/component.py", line 809, in _dispatch_draw
    handler(gc, view_bounds, mode)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/chaco/base_xy_plot.py", line 495, in _draw_plot
    self._render(gc, pts)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/chaco/plots/scatterplot.py", line 541, in _render
    self.render_markers_func(
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/chaco/plots/scatterplot.py", line 191, in render_markers
    marker.add_to_path(gc, size)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/markers.py", line 61, in add_to_path
    self._add_to_path(path, size)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/enable/markers.py", line 89, in _add_to_path
    path.rect(-size, -size, size * 2, size * 2)
  File "/Users/mdickinson/.edm/envs/testing/lib/python3.8/site-packages/kiva/agg/agg.py", line 1168, in rect
    return _agg.GraphicsContextArray_rect(self, *args)
TypeError: Wrong number or type of arguments for overloaded function 'GraphicsContextArray_rect'.
  Possible C/C++ prototypes are:
    kiva::graphics_context_base::rect(kiva::rect_type &)
    kiva::graphics_context_base::rect(double,double,double,double)
homosapien-lcy commented 1 year ago

Yeah, I later find out that my test code was not exactly the same command... I think the solution is adding a check in plot_id() in the Plot class that will convert the int array into float before feeding to class construction

homosapien-lcy commented 1 year ago

OK, after looking at the code, I found there is a much better fix: the problem is caused by type mismatch in the ScatterPlot1D class which specifies marker_size as Float. But the function that actually uses this parameter (render_markers_func) is actually designed to handle both single int, float and their numpy arrays (in case of a single numeric input, it will repeat it into an array). So the fix is just change ScatterPlot1D to accept both number and array. I haven't find a Trait type that allows us to do "or". Thus I go with Any for this trait. PR #859 @corranwebster

mdickinson commented 1 year ago

I haven't find a Trait type that allows us to do "or".

Take a look at the Union trait type: https://docs.enthought.com/traits/traits_user_manual/defining.html#union

homosapien-lcy commented 1 year ago

Thanks Mark! Changed to use union