Python 如何用Chaco覆盖两张图像?
注意:我将自己回答这个问题,以帮助将来遇到这个问题的其他人。如果你愿意,请随时提交你自己的答案,但要知道答案已经给出了Python 如何用Chaco覆盖两张图像?,python,plot,overlay,chaco,Python,Plot,Overlay,Chaco,注意:我将自己回答这个问题,以帮助将来遇到这个问题的其他人。如果你愿意,请随时提交你自己的答案,但要知道答案已经给出了 在Chaco中,如何将带有一个颜色贴图的蒙版图像覆盖到另一个带有不同颜色贴图的图像上?另外,我如何为每种颜色添加色条?在Chaco中以这种方式覆盖图像的做法没有很好的文档记录,但绝对是可能的。首先,你如何与查科一起绘制一幅蒙面图像?使用Plot().img_Plot()打印时,Chaco使用np.nan值作为透明像素。例如,打印: img = np.eye(100) img[i
在Chaco中,如何将带有一个颜色贴图的蒙版图像覆盖到另一个带有不同颜色贴图的图像上?另外,我如何为每种颜色添加色条?在Chaco中以这种方式覆盖图像的做法没有很好的文档记录,但绝对是可能的。首先,你如何与查科一起绘制一幅蒙面图像?使用
Plot().img_Plot()
打印时,Chaco使用np.nan
值作为透明像素。例如,打印:
img = np.eye(100)
img[img==0] = np.nan
将绘制一条带有透明背景的对角线
但是你如何将这个图像覆盖到另一个图像上呢
有两种主要方法可以做到这一点
OverlayPlotContainer
plot
对象上打印多个图像时(使用img\u plot()
),默认情况下,它们都将使用相同的颜色映射器。这意味着两者将缩放到相同的范围。这可能不是必需的结果,因此必须为这两个图像创建新的颜色映射器
下面是TraitsUI的一些示例代码,它是从Qt代码改编而来的
from traits.api import HasTraits, Instance
from traitsui.api import Item, View
from enable.api import ComponentEditor
from chaco.api import ArrayPlotData, Plot, ColorBar, LinearMapper, HPlotContainer, DataRange1D, ImageData
import chaco.default_colormaps
#
import numpy as np
class ImagePlot(HasTraits):
plot = Instance(HPlotContainer)
traits_view = View(
Item('plot', editor=ComponentEditor(), show_label=False), width=500, height=500, resizable=True, title="Chaco Plot")
def _plot_default(self):
bottomImage = np.reshape(np.repeat(np.linspace(0, 5, 100),100), (100,100))
topImage = np.eye(50)
topImage = topImage*np.reshape(np.repeat(np.linspace(-2, 2, 50),50), (50,50))
topImage[topImage==0] = np.nan
#
bottomImageData = ImageData()
bottomImageData.set_data(bottomImage)
#
topImageData = ImageData()
topImageData.set_data(topImage)
#
plotData = ArrayPlotData(imgData=bottomImageData, imgData2=topImageData)
plot = Plot(plotData, name='My Plot')
plot.img_plot("imgData")
plot.img_plot("imgData2")
# Note: DO NOT specify a colormap in the img_plot!
plot.aspect_ratio = 1.0
#
bottomRange = DataRange1D()
bottomRange.sources = [plotData.get_data("imgData")]
topRange = DataRange1D()
topRange.sources = [plotData.get_data("imgData2")]
plot.plots['plot0'][0].color_mapper = chaco.default_colormaps.gray(bottomRange)
plot.plots['plot1'][0].color_mapper = chaco.default_colormaps.jet(topRange)
#
colormapperBottom = plot.plots['plot0'][0].color_mapper
colormapperTop = plot.plots['plot1'][0].color_mapper
#
colorbarBottom = ColorBar(index_mapper=LinearMapper(range=colormapperBottom.range), color_mapper=colormapperBottom, orientation='v', resizable='v', width=30, padding=20)
colorbarBottom.padding_top = plot.padding_top
colorbarBottom.padding_bottom = plot.padding_bottom
#
colorbarTop = ColorBar(index_mapper=LinearMapper(range=colormapperTop.range), color_mapper=colormapperTop, orientation='v', resizable='v', width=30, padding=20)
colorbarTop.padding_top = plot.padding_top
colorbarTop.padding_bottom = plot.padding_bottom
#
container = HPlotContainer(resizable = "hv", bgcolor='transparent', fill_padding=True, padding=0)
container.spacing = 0
container.add(plot)
container.add(colorbarBottom)
container.add(colorbarTop)
#
return container
if __name__ == "__main__":
ImagePlot().configure_traits()
我没有为此获得100%的信任,通过在线快速搜索,我发现您可以使用以下代码进行简单的覆盖: 找到的来源: 参考代码:
class OverlayImageExample(HasTraits):
plot = Instance(OverlayImage)
traits_view = View(
Item('plot', editor=ComponentEditor(), show_label=False),
width=800, height=600, resizable=True
)
def _plot_default(self):
# Create data
x = linspace(-5, 15.0, 100)
y = jn(3, x)
pd = ArrayPlotData(index=x, value=y)
zoomable_plot = Plot(pd)
zoomable_plot.plot(('index', 'value'),
name='external', color='red', line_width=3)
# Attach tools to the plot
zoom = ZoomTool(component=zoomable_plot,
tool_mode="box", always_on=False)
zoomable_plot.overlays.append(zoom)
zoomable_plot.tools.append(PanTool(zoomable_plot))
# Create a second inset plot, not resizable, not zoom-able
inset_plot = Plot(pd)
inset_plot.plot(('index', 'value'), color='blue')
inset_plot.set(resizable = '',
bounds = [250, 150],
position = [450, 350],
border_visible = True
)
# Create a container and add our plots
container = OverlayPlotContainer()
container.add(zoomable_plot)
container.add(inset_plot)
return container
对于任何可能感兴趣的人,我发现PyQtGraph在这方面非常优秀。@kezzos使用Chaco的主要原因是我们发现性能比PyQtGraph好。我们需要实时快速显示图像。一般来说,缺少CHACO文档是一个很好的理由来考虑PyqtGrand之类的东西。我的经验是ChaCo很快减慢了添加到场景中的元素(例如很多单独的行),而PyqtCGRAPH在这个上下文中表现得更好。使用chaco也很可怕:)这对非图像打印效果很好,但叠加图像的问题主要是确定nan值被认为是透明的。这与使用numpy数组掩码的库(如matplotlib)不同。此外,OverlayPlotContainer还创建了第二组轴,如果所有图像都具有相同的尺寸,则不需要这些轴。