Matplotlib 如何在不拉伸绘图的情况下展开matplolib窗口?

Matplotlib 如何在不拉伸绘图的情况下展开matplolib窗口?,matplotlib,Matplotlib,我想增加绘图周围的灰色区域,但保持绘图大小不变。我已经尝试过改变人物的大小,这最终会拉伸情节。 在matplotlib图形的窗口中,有一个名为“配置子绘图”的按钮(请参见下图,matplotlib版本为1.5.2的Windows 10上的屏幕截图)。尝试更改参数“left”和“right”。您还可以使用plt.subplot\u adjust(left=…,bottom=…,right=…,top=…,wspace=…,hspace=…)更改这些参数。 在matplotlib图形的窗口中,有一个

我想增加绘图周围的灰色区域,但保持绘图大小不变。我已经尝试过改变人物的大小,这最终会拉伸情节。

在matplotlib图形的窗口中,有一个名为“配置子绘图”的按钮(请参见下图,matplotlib版本为1.5.2的Windows 10上的屏幕截图)。尝试更改参数“left”和“right”。您还可以使用
plt.subplot\u adjust(left=…,bottom=…,right=…,top=…,wspace=…,hspace=…)更改这些参数。


在matplotlib图形的窗口中,有一个名为“配置子绘图”的按钮(请参见下图,matplotlib版本为1.5.2的Windows 10上的屏幕截图)。尝试更改参数“left”和“right”。您还可以使用
plt.subplot\u adjust(left=…,bottom=…,right=…,top=…,wspace=…,hspace=…)更改这些参数。


图形内部的轴相对于图形进行定位。根据默认设置,例如,图形宽度的0.125分之一作为左侧的空间。这意味着调整图形的大小也会缩放轴

您可以计算需要更改的间距,以便在图形重新缩放时,轴大小保持不变。然后需要使用
fig.subplot\u adjust
设置新的间距

import matplotlib.pyplot as plt

def set_figsize(figw,figh, fig=None):
    if not fig: fig=plt.gcf()
    w, h = fig.get_size_inches()
    l = fig.subplotpars.left
    r = fig.subplotpars.right
    t = fig.subplotpars.top
    b = fig.subplotpars.bottom
    hor = 1.-w/float(figw)*(r-l)
    ver = 1.-h/float(figh)*(t-b)
    fig.subplots_adjust(left=hor/2., right=1.-hor/2., top=1.-ver/2., bottom=ver/2.)

fig, ax=plt.subplots()

ax.plot([1,3,2])

set_figsize(9,7)

plt.show()
然后,当调整“地物”窗口的大小时,也可以使用此函数更新子地块参数

import matplotlib.pyplot as plt

class Resizer():
    def __init__(self,fig=None):
        if not fig: fig=plt.gcf()
        self.fig=fig
        self.w, self.h = self.fig.get_size_inches()
        self.l = self.fig.subplotpars.left
        self.r = self.fig.subplotpars.right
        self.t = self.fig.subplotpars.top
        self.b = self.fig.subplotpars.bottom

    def set_figsize(self, figw,figh):  
        hor = 1.-self.w/float(figw)*(self.r-self.l)
        ver = 1.-self.h/float(figh)*(self.t-self.b)
        self.fig.subplots_adjust(left=hor/2., right=1.-hor/2., top=1.-ver/2., bottom=ver/2.)

    def resize(self, event):
        figw = event.width/self.fig.dpi
        figh = event.height/self.fig.dpi
        self.set_figsize( figw,figh)

fig, ax=plt.subplots()

ax.plot([1,3,2])

r = Resizer()
cid = fig.canvas.mpl_connect("resize_event", r.resize)

plt.show()

图形内部的轴相对于图形进行定位。根据默认设置,例如,图形宽度的0.125分之一作为左侧的空间。这意味着调整图形的大小也会缩放轴

您可以计算需要更改的间距,以便在图形重新缩放时,轴大小保持不变。然后需要使用
fig.subplot\u adjust
设置新的间距

import matplotlib.pyplot as plt

def set_figsize(figw,figh, fig=None):
    if not fig: fig=plt.gcf()
    w, h = fig.get_size_inches()
    l = fig.subplotpars.left
    r = fig.subplotpars.right
    t = fig.subplotpars.top
    b = fig.subplotpars.bottom
    hor = 1.-w/float(figw)*(r-l)
    ver = 1.-h/float(figh)*(t-b)
    fig.subplots_adjust(left=hor/2., right=1.-hor/2., top=1.-ver/2., bottom=ver/2.)

fig, ax=plt.subplots()

ax.plot([1,3,2])

set_figsize(9,7)

plt.show()
然后,当调整“地物”窗口的大小时,也可以使用此函数更新子地块参数

import matplotlib.pyplot as plt

class Resizer():
    def __init__(self,fig=None):
        if not fig: fig=plt.gcf()
        self.fig=fig
        self.w, self.h = self.fig.get_size_inches()
        self.l = self.fig.subplotpars.left
        self.r = self.fig.subplotpars.right
        self.t = self.fig.subplotpars.top
        self.b = self.fig.subplotpars.bottom

    def set_figsize(self, figw,figh):  
        hor = 1.-self.w/float(figw)*(self.r-self.l)
        ver = 1.-self.h/float(figh)*(self.t-self.b)
        self.fig.subplots_adjust(left=hor/2., right=1.-hor/2., top=1.-ver/2., bottom=ver/2.)

    def resize(self, event):
        figw = event.width/self.fig.dpi
        figh = event.height/self.fig.dpi
        self.set_figsize( figw,figh)

fig, ax=plt.subplots()

ax.plot([1,3,2])

r = Resizer()
cid = fig.canvas.mpl_connect("resize_event", r.resize)

plt.show()

我认为没有任何理由否决这个答案。这是正确和有用的,即使它没有给出一个精确的算法来设置所讨论的参数值。我不认为有任何理由否决这个答案。它是正确和有用的,即使它没有给出一个精确的算法来设置所讨论的参数值。