Calling plt.subplots() shrinks frame size

Here is an example: first run it with plt.subplots() commented – frame size is as specified, (1024, 768). Then uncomment plt.subplots() - the frame will shrink in size.
How can I avoid this shrinking?

import wx

import matplotlib.pyplot as plt

app = wx.App(False)
frame = wx.Frame(None, size=(1024, 768))

fig, ax = plt.subplots()


It doesn’t resize the window for me. Are you aware that pyplot will create its own frame for showing the plots? It can also have a tendency to create its own wx.App at unexpected times, so maybe there is some interaction there that is happening for you.

BTW, I’ve found this lib to be helpful for using matplotlib with wxPython applications, especially for those who are new to combining the two of them: Matplotlib is great as a general purpose tool, but it doesn’t really fit well with the wx way of doing things. The wxmplot package bridges those two worlds.

I appreciate your suggestion, but I have to use matplotlib in the app I’m working on.
So I would want to figure this problem out and solve it.

It’s interesting that it doesn’t shrink for you. What are the versions of wxPython and matplotlib you are using? If you are using conda, could you please show me the result of calling “conda list”?

You would still be using Matplotlib when you use wxmplot, it just makes integrating and using Matplotlib in a wxPython application easier.

Sorry for the late reply, but I was skiing for a week…

For your example to do something useful, you need at least to call

fig, ax = plt.subplots()

But anyway, you’re creating a wx.Frame and then using matplotlib’s plt interface to create another toplevel window. These two are not related anyhow.
You probably want to embed a matplotlib canvas into a wx.Frame. For this, the plt interface is not too useful.
The matplotlib documentation has some examples on embedding.

For more comprehensive examples, you may look at the ones included with wxGlade:

The first one is very basic. The third demonstrates most of the building blocks that you’ll need when embedding a canvas and handling matplotlib events.

Embedding typcially looks like this:

import matplotlib
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

figure = self.matplotlib_figure = Figure()
# 1x1 grid, first subplot
self.matplotlib_axes = figure.add_subplot(111)
self.matplotlib_canvas = FigureCanvas(parent, wx.ID_ANY, figure)

where parent is e.g. a wx.Frame or better a wx.Panel having sizers that will manage the layout.



I know how to “do something useful”, it was just a minimal example to replicate my issue.
I was already using embedding.
Thanks a lot for pointing out that I can create figures like this, avoiding a call to plt.subplots. I didn’t know. This solves my problem.
I guess when matplotlib is used as a standalone thing, you want a separate window for your figure, so all the examples I learned from used plt to create figures.