PyCharm中的matplotlib.pyplot.show()会移动我的轴!我怎么能阻止这个?

问题描述 投票:0回答:1

当我在matplotlib中创建一个图形并使用matplotlib.pyplot.show()在PyCharm中渲染该图形时,图形会移动!

这不会发生在PyCharm之外的show()。

import matplotlib.pyplot as plt    

def axes_position_test():
    """Witness change in position after plt.show()."""
    fig = plt.figure()
    ax1 = fig.add_subplot(111)

    # original position
    print(ax1.get_position())

    plt.show()

    # position has changed
    print(ax1.get_position())

axes_position_test()
# output
# Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88)
# Bbox(x0=0.07300347222222223, y0=0.08067129629629632, x1=0.959375, y1=0.9572916666666668)
python matplotlib pycharm
1个回答
1
投票

我无法重现问题 - 代码导致运行matplotlib 3.0.2时出错。也许正在使用不同的版本?

在任何情况下,通常回答这个问题: 位置可能会随着时间的推移而改变,特别是如果图形显示在GUI内部,GUI本身(稍微)会改变图形大小。

关于最终目标的问题并不是很清楚,但正如我所解释的那样,目标是让两个轴相互叠加。这很容易通过

fig = plt.figure()
ax1 = fig.add_subplot(111, label="first axes")
ax2 = fig.add_subplot(111, label="second axes")

使用gridspec可以制作更复杂的几何图形,总是使用add_subplot来添加子图。

从OP编辑:正如评论中所揭示的那样,PyCharm渲染器interagg就是问题所在。禁用interagg(设置>工具> Python Scientific>取消选中“显示绘图”)可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.