如何使FigureCanvas适合Panel?
我正在尝试使用 Matplotlib 和 wxPython 显示一些数据。我有一个已添加到FigureCanvasWxAgg 的图形。然后画布被添加到 BoxSizer 并设置为 wx.EXPAND|wx.ALL,BoxSizer 又由 SetSizerAndFit 设置。
self.figure = Figure(None, dpi = 75)
self.displaycanvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.imshow(self.data, interpolation="quadric")
self.mainSizer = wx.BoxSizer()
self.mainSizer.Add(self.displaycanvas, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizerAndFit(self.mainSizer)
然后将该面板添加到另一个面板,其中其大小是相对于要添加的其他面板确定的。虽然我对面板的外部尺寸感到满意,但我无法让图形适合面板:
带有所有爪子的大面板应缩放以适合面板,同时保持其纵横比。
所以我想知道,为什么图形不会扩展以适应面板?
I'm trying to display some data using Matplotlib and wxPython. I've got a Figure that's added to a FigureCanvasWxAgg. The canvas is then added to a BoxSizer and set to wx.EXPAND|wx.ALL, the BoxSizer on its turn is set by SetSizerAndFit.
self.figure = Figure(None, dpi = 75)
self.displaycanvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_subplot(111)
self.axes.imshow(self.data, interpolation="quadric")
self.mainSizer = wx.BoxSizer()
self.mainSizer.Add(self.displaycanvas, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizerAndFit(self.mainSizer)
This panel is then added to another panel, where its size get's determined relative to other panels that are being added. While I'm happy with the outer size of the panel, I can't get the figure to fit the panel:
The large panel with all the paws should be scaled to fit the panel, while maintaining its aspect ratio.
So I'm wondering, why won't the figure expand to fit the panel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个具有明确边界的坐标区对象,而不是从
add_subplot
方法获取坐标区。因此,不要使用
这四个数字是轴的左边缘、下边缘、宽度和高度(以图形宽度和高度的分数形式表示)。
[0,0,1,1]
将扩展图像以适合整个图形。当然,长宽比仍然存在问题。如果您想保持图像的纵横比,那么它不会总是填充整个小部件空间(取决于小部件的形状)。如果宽高比更改没问题,您可以像这样调用 im_show ,这将使图像填充小部件,无论形状如何。
如果没有自动宽高比,则会导致:
Instead of getting your axes from the
add_subplot
method, you can create an axes object with explicit boundaries. So instead ofuse
The four numbers are the left edge, bottom edge, width and height of the axes in fractions of figure width and height.
[0,0,1,1]
will expand the image to fit the entire figure. Of course, there are still issues with the aspect ratio. If you want to maintain the aspect ratio of your image, then it won't always fill the entire widget space (depending on the shape of the widget). If aspect ratio changes are ok, you can call im_show like thiswhich will make the image fill the widget, no matter what the shape.
Without the automatic aspect ratio this results in: