Chaco 中的自动填充?

Automatic paddings in Chaco?

是否可以让 chaco plot 自动显示完整输出而不隐藏部分刻度和标签?例如。这是标准示例的输出:

from chaco.api import ArrayPlotData, Plot
from enable.component_editor import ComponentEditor

from traits.api import HasTraits, Instance
from traitsui.api import View, Item


class MyPlot(HasTraits):
    plot = Instance(Plot)
    traits_view = View(Item('plot', editor = ComponentEditor(), show_label = False),
                   width = 500, height = 500, resizable = True)

def __init__(self, x, y, *args, **kw):
    super(MyPlot, self).__init__(*args, **kw)
    plotdata = ArrayPlotData(x=x,y=y)
    plot = Plot(plotdata)
    plot.plot(("x","y"), type = "line", color = "blue")
    self.plot = plot


import numpy as np
x = np.linspace(-300,300,10000)
y = np.sin(x)*x**3
lineplot = MyPlot(x,y)
lineplot.configure_traits()

如您所见,刻度标签的部分被隐藏了。我唯一能做的就是手动调整绘图的左填充。但是,当您在应用程序中绘制不同的数据和不同的比例或字体时,这会变得非常不方便。是否有可能以某种方式自动调整填充以包含所有相关信息?

UPD.: 我找到了 ensure_labels_bounded 属性 轴,但似乎没有效果。

Chaco 不支持这些高级布局功能。如果你使用 Chaco,你应该因为它的速度而使用它,而不是为了漂亮的图形或功能。话虽如此,这是我能得到的最接近的版本。它要求您至少用鼠标 re-size window 一次,以便进行填充校正。也许您可以找到一种方法来刷新 window 而无需手动调整它的大小,我没有任何运气。无论如何,希望能让你走上正轨。

from chaco.api import ArrayPlotData, Plot
from enable.component_editor import ComponentEditor

from traits.api import HasTraits, Instance
from traitsui.api import View, Item

class MyPlot(HasTraits):
    plot = Instance(Plot)
    traits_view = View(Item('plot', editor = ComponentEditor(), show_label = False),
                   width = 500, height = 500, resizable = True)

    def __init__(self, x, y, *args, **kw):
        super(MyPlot, self).__init__(*args, **kw)
        plotdata = ArrayPlotData(x=x,y=y)
        plot = Plot(plotdata, padding=25)
        plot.plot(("x","y"), type = "line", color = "blue", name='abc')
        self.plot = plot
        # watch for changes to the bounding boxes of the tick labels
        self.plot.underlays[2].on_trait_change(self._update_size, '_tick_label_bounding_boxes')
        self.plot.underlays[3].on_trait_change(self._update_size, '_tick_label_bounding_boxes')
    def _update_size(self):
        if len(self.plot.underlays[2]._tick_label_bounding_boxes) > 0:
            self.plot.padding_bottom = int(np.amax(np.array(self.plot.underlays[2]._tick_label_bounding_boxes),0)[1]+8+4)
        if len(self.plot.underlays[3]._tick_label_bounding_boxes) > 0:
            self.plot.padding_left = int(np.amax(np.array(self.plot.underlays[3]._tick_label_bounding_boxes),0)[0]+8+4)

import numpy as np
x = np.linspace(-300,300,10000)
y = np.sin(x)*x**3
lineplot = MyPlot(x,y)
lineplot.configure_traits()