How to make the shape of the shape suitable for the panel?

I am trying to display some data using Matplotlib and wxPython. I have a figure added to FigureCanvasWxAgg. Then the canvas is added to BoxSizer and installed in wx.EXPAND | wx.ALL, BoxSizer, in 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 is determined relative to other added panels. Although I am pleased with the outer size of the panel, I cannot get the shape to fit the panel:

enter image description here

A large panel with all paws must be scaled to fit the panel, while maintaining its aspect ratio.

So, I am wondering why the figure will not expand to fit the panel?

+6
source share
1 answer

Instead of getting your axes from the add_subplot method, you can create an axis object with explicit boundaries. Therefore, instead of

 self.axes = self.figure.add_subplot(111) 

using

 self.axes = self.figure.add_axes([0,0,1,1]) 

Four digits are the left edge, the lower edge, the width and height of the axes in fractions of the width and height of the figure. [0,0,1,1] will expand the image to fit the entire figure. Of course, there are still problems with the aspect ratio. If you want to keep the aspect ratio of your image, it will not always fill the entire space of the widget (depending on the shape of the widget). If the proportions are alright, you can call im_show like this

 self.axes.imshow(self.data, interpolation="quadric", aspect='auto') 

which will make the image fill the widget, regardless of shape.

Without an automatic aspect ratio, this means:

enter image description here

+6
source

Source: https://habr.com/ru/post/890691/


All Articles