Skip to content

Qt4Agg backend changes figure size #9913

@ImportanceOfBeingErnest

Description

@ImportanceOfBeingErnest

Edit: Note that this issue is also still present with Qt5Agg and matplotlib 3.0.1

Showing a figure with Qt4Agg backend changes the figure size. This is observed with Windows 8.1, python 2.7, matplotlib 2.1.

import matplotlib
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,4])

def update(evt):
    print(fig.get_size_inches())
       
cid = fig.canvas.mpl_connect("key_press_event", update)

print(fig.get_size_inches())
plt.show()

The original figure is [ 6.4 4.8]. After the figure is drawn (and in this case you press a key), it is only [ 6.4 4.78].
One could argue that this is only a small amount, but saving the figure from within the event loop now gives a different figure size of the saved figure. To make this more obvious, consider the following code which saves a new image after each press of space.

import matplotlib
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
import numpy as np

class NextPlotter(object):
    def __init__(self, fig, func, n):
        self.__dict__.update(locals())
        self.i = 0
        self.cid = self.fig.canvas.mpl_connect("key_press_event", self.adv)
        
    def adv(self, evt):
        if evt.key == " " and self.i < self.n:
            self.func(self.i)
            self.i+=1
        elif self.i >= self.n:
            plt.close("all")

# Create data
pos = range(8)
scatterdata = np.random.normal(0, 5, size=(50,len(pos)))

#Create plot
fig, ax = plt.subplots()
ax.axis([-10,10,-10,10])
scatter = ax.scatter([], [], c="crimson", s=60)

# define updating function
def update(i):
    print("++ update " + str(i))
    scatter.set_offsets(np.c_[pos,scatterdata[i,:]])
    fig.canvas.draw()
    print(fig.get_size_inches())
    plt.savefig("plot_{i}.png".format(i=i))
    print(fig.get_size_inches())

# instantiate NextPlotter; press <space> to advance to the next image        
c = NextPlotter(fig, update, len(scatterdata))

plt.show()

resulting in
nextplotter_qtagg3

Is this a problem of PyQt4? Can anyone reproduce this outside of Windows 8?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions