是否可以让chaco图自动显示完整输出而不隐藏刻度和标签的部分?例如。这是标准示例的输出:
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属性,但似乎没有效果。
答案 0 :(得分:1)
Chaco不支持这些高级布局功能。如果你使用Chaco,你应该使用它的速度,而不是漂亮的图形或功能。话虽这么说,这是一个尽可能接近的版本。它需要您使用鼠标重新调整窗口大小至少一次,以进行填充校正。也许你可以找到一种方法刷新窗口而不必手动调整它,我没有任何运气。无论如何希望能让你走上正轨。
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()