Python Matplotlib figsize在WXAgg中行为奇怪

Python Matplotlib figsize在WXAgg中行为奇怪,python,matplotlib,Python,Matplotlib,我正在尝试将图形调整为A4纸张大小,以将其另存为pdf。这在使用WXAgg时会产生奇怪的行为。以下是一个例子: import matplotlib as mpl mpl.use("WXAgg") import numpy as np import matplotlib.pyplot as plt # data to plot x = np.arange(0, 10, 0.005) y = np.sin(x) # plot the data fig = plt.figure() ax = fi

我正在尝试将图形调整为A4纸张大小,以将其另存为pdf。这在使用WXAgg时会产生奇怪的行为。以下是一个例子:

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

# data to plot
x = np.arange(0, 10, 0.005)
y = np.sin(x)

# plot the data
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.plot(x, y)

# resize figure and immediatly print it's size
page_width, page_height = (8.2765, 11.70475) #A4
fig.set_size_inches(page_width, page_height)
print fig.get_size_inches()
我得到的输出是
[8.27 7.59]
,与预期不符。当我注释掉
mpl.use(“WXAgg”)
时,它工作得很好。然后,输出为预期的
[8.2765 11.70475]

编辑:显然这与屏幕的大小有关。我更改了屏幕(将显示器插入笔记本电脑),输出更改为
[8.27 9.39]

这里发生了什么?如何使用WXAgg获得正确的体形尺寸

有关我的系统的一些信息:

  • 操作系统:macOS High Sierra
    10.13.1
  • python:
    2.7.13
  • Matplotlib:
    2.2.3

你能试一下
fig=plt.figure(figsize=(8.2765,11.70475))
并注释掉最后第二行和最后第三行吗?对我来说,你的代码运行良好,并打印出正确的dimensions@Bazingaa这确实给出了预期的输出。但是,我需要在创建后更改图形大小。奇怪的是,您没有得到相同的结果……如果两个人运行相同的代码,得到不同的结果,那么可能是版本控制问题。你正在使用哪个版本的matplotlib?我试过的版本是
2.0.2
@ImportanceOfBeingErnest,我在帖子中添加了关于版本的信息。