如何在python中的固定图形中更改轴的大小?

时间:2019-08-16 14:07:07

标签: python axes

此代码:

import matplotlib.pyplot as plt
fig = plt.figure(facecolor=(1, 0, 0, .1))
ax = fig.add_subplot(1,1,1) 
plt.show()

为图形输出一个粉红色框,其中为轴对象输出一个白色框。现在,是否可以在不更改图形尺寸的情况下更改图形中轴对象的尺寸?

1 个答案:

答案 0 :(得分:0)

尝试此代码,看看这是否是您想要的。您可以更改函数内部提供的固定大小值。

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import Divider, Size
from mpl_toolkits.axes_grid1.mpl_axes import Axes


def demo_fixed_size_axes():
    fig = plt.figure(facecolor=(1, 0, 0, .1))


    # The first items are for padding and the second items are for the axes.
    # sizes are in inch.
    h = [Size.Fixed(1.0), Size.Fixed(4.5)]
    v = [Size.Fixed(0.7), Size.Fixed(5.)]

    divider = Divider(fig, (0.0, 0.0, 1., 1.), h, v, aspect=False)
    # the width and height of the rectangle is ignored.
    ax = Axes(fig, divider.get_position())
    ax.set_axes_locator(divider.new_locator(nx=1, ny=1))

    fig.add_axes(ax)

    # ax.plot([1, 2, 3])
    plt.show()

demo_fixed_size_axes()