Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================
#============= enthought library imports =======================
from traits.api import HasTraits, Instance, Any
from traitsui.api import View, Item, TableEditor
from chaco.api import HPlotContainer, ArrayPlotData, Plot
#============= standard library imports ========================
import os
#============= local library imports ==========================
from pychron.graph.image_underlay import ImageUnderlay
from pychron.graph.tools.xy_inspector import XYInspector, XYInspectorOverlay
class ImageViewer(HasTraits):
container = Instance(HPlotContainer, ())
plot = Any
def load_image(self, path):
if os.path.isfile(path):
with open(path, 'r') as fp:
self.set_image(fp)
def set_image(self, buf):
'''
buf is a file-like object
'''
self.container = HPlotContainer()
pd = ArrayPlotData(x=[0, 640],
y=[0, 480])
padding = [30, 5, 5, 30]
plot = Plot(data=pd, padding=padding,
def _container_factory(self):
pc = HPlotContainer(padding=[5, 5, 5, 20])
return pc
def _load_image_data(self, data):
cont = HPlotContainer()
pd = ArrayPlotData()
plot = Plot(data=pd, padding=[30, 5, 5, 30], default_origin='top left')
pd.set_data('img', data)
img_plot = plot.img_plot('img',
)[0]
self._add_inspector(img_plot)
self._add_tools(img_plot)
cont.add(plot)
cont.request_redraw()
self.image_container.container = cont
orientation='v',
resizable='v',
width=30,
padding=20)
colorbar.plot = plot
colorbar.padding_top = plot.padding_top
colorbar.padding_bottom = plot.padding_bottom
# Add pan and zoom tools to the colorbar
colorbar.tools.append(PanTool(colorbar, constrain_direction="y", constrain=True))
zoom_overlay = ZoomTool(colorbar, axis="index", tool_mode="range",
always_on=True, drag_button="right")
colorbar.overlays.append(zoom_overlay)
# Create a container to position the plot and the colorbar side-by-side
container = HPlotContainer(plot, colorbar, use_backbuffer=True, bgcolor="lightgray")
return container
def _container_default(self):
img_plot = self.img_plot
zoom_plot = self.zoom_plot
container = HPlotContainer(img_plot, zoom_plot)
return container
def set_image(self, buf):
'''
buf is a file-like object
'''
self.container = HPlotContainer()
pd = ArrayPlotData(x=[0, 640],
y=[0, 480])
padding = [30, 5, 5, 30]
plot = Plot(data=pd, padding=padding,
# default_origin=''
)
self.plot = plot.plot(('x', 'y'),)[0]
self.plot.index.sort_order = 'ascending'
imo = ImageUnderlay(self.plot,
padding=padding,
path=buf)
self.plot.overlays.append(imo)
self._add_tools(self.plot)
self.container.add(plot)
from chaco.api import ArrayPlotData, HPlotContainer, Plot, jet, ColorBar
from enable.component_editor import ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, View
import numpy as np
from chaco.linear_mapper import LinearMapper
N_POINTS = 100
class ColorbarExample(HasTraits):
plot = Instance(HPlotContainer)
traits_view = View(Item('plot', editor=ComponentEditor(), show_label=False),
width=600, height=600, resizable=True)
def __init__(self):
# Create some data
x = np.random.random(N_POINTS)
y = np.random.random(N_POINTS)
color = np.exp(-(x**2 + y**2))
# Create a plot data object and give it this data
data = ArrayPlotData(index=x, value=y, color=color)
# Create the plot
plot = Plot(data)
plot.plot(("index", "value", "color"), type="cmap_scatter",
self.colorbar.padding_bottom = self.tplot.padding_bottom
# create a range selection for the colorbar
self.range_selection = RangeSelection(component=self.colorbar)
self.colorbar.tools.append(self.range_selection)
self.colorbar.overlays.append(RangeSelectionOverlay(component=self.colorbar,
border_color="white",
alpha=0.8,
fill_color="lightgray"))
# we also want to the range selection to inform the cmap plot of
# the selection, so set that up as well
self.range_selection.listeners.append(self.my_plot)
# Create a container to position the plot and the colorbar side-by-side
container = HPlotContainer(use_backbuffer = True)
container.add(self.tplot)
container.add(self.colorbar)
container.bgcolor = "white"
return container
self.colorbar.padding_bottom = self.tplot.padding_bottom
# create a range selection for the colorbar
self.range_selection = RangeSelection(component=self.colorbar)
self.colorbar.tools.append(self.range_selection)
self.colorbar.overlays.append(RangeSelectionOverlay(component=self.colorbar,
border_color="white",
alpha=0.8,
fill_color="lightgray"))
# we also want to the range selection to inform the cmap plot of
# the selection, so set that up as well
self.range_selection.listeners.append(self.my_plot)
# Create a container to position the plot and the colorbar side-by-side
container = HPlotContainer(use_backbuffer = True)
container.add(self.tplot)
container.add(self.colorbar)
container.bgcolor = "white"
return container
# Attach some tools to the plot
plot.tools.append(PanTool(plot, constrain_key="shift"))
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
plot.overlays.append(zoom)
selection = ColormappedSelectionOverlay(cmap_renderer, fade_alpha=0.35,
selection_type="mask")
cmap_renderer.overlays.append(selection)
# Create the colorbar, handing in the appropriate range and colormap
colorbar = create_colorbar(plot.color_mapper)
colorbar.plot = cmap_renderer
colorbar.padding_top = plot.padding_top
colorbar.padding_bottom = plot.padding_bottom
# Create a container to position the plot and the colorbar side-by-side
container = HPlotContainer(use_backbuffer = True)
container.add(plot)
container.add(colorbar)
container.bgcolor = "lightgray"
return container