Python / Matplotlib - Picture Borders in wxPython

I have a matplotlib figure on a canvas in a wxpython panel. My application runs in real time, and the user can resize the window. However, when this happens, the borders around my axes fail.

I know that I can use the fig.subplots_adjust function to adjust the borders, but the values ​​specified in the function are percentages, so when maximizing there is a TON of lost space around the borders, although the space is just right, the window is smaller.

Is there something similar to this function where I can specify a border in something like pixels so that the border is the same width, no matter what frame size?

Thanks as always!

+3
source share
1

, . , . , :

import numpy
import matplotlib.pyplot as plt

def adjust_borders(fig, targets):
    "Translate desired pixel sizes into percentages based on figure size."
    dpi = fig.get_dpi()
    width, height = [float(v * dpi) for v in fig.get_size_inches()]
    conversions = {
        'top': lambda v: 1.0 - (v / height),
        'bottom': lambda v: v / height,
        'right': lambda v: 1.0 - (v / width),
        'left': lambda v: v / width,
        'hspace': lambda v: v / height,
        'wspace': lambda v: v / width,
        }
    opts = dict((k, conversions[k](v)) for k, v in targets.items())
    fig.subplots_adjust(**opts)

fig = plt.figure(figsize=(7, 5))
for i in range(4):
    ax = fig.add_subplot(2, 2, i+1)
    ax.plot([1,2,3], [4,5,1])
    ax.set_xticks([])
    ax.set_yticks([])

# target sizes in pixels.
targets = dict(left=10, right=10, top=10, bottom=30, hspace=30, wspace=30)
# hook up a function to adjust the borders when the window is resized
fig.canvas.mpl_connect('resize_event', lambda e: adjust_borders(fig, targets))
adjust_borders(fig, targets)
plt.show()
+5

All Articles