单个figu的Matplotlib rcparams(自动限制模式)

2024-10-01 00:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我对新的Matplotlib 2.0.0有一个问题。在

默认情况下,新的autolimit_mode值会向帧添加填充。 我不想把它变成一个数字。在

如果我更改rcParams,这些更改会影响到任何生成的图形。在

我可以为单个图形更改此参数而不影响其他图形的行为吗?在


更新

我用一些代码更新了问题,得到了不同的结果。在

import numpy as np
import matplotlib
import matplotlib.pyplot as plt


def remove_frame_border(ax):
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)


def draw1(xserie, yserie):
    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    fig.savefig('out1.png', format='png', bbox_inches='tight', pad_inches=0)


def draw2(xserie, yserie):
    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    # Fixed autolimit padding in matplotlib 2.0.0
    ax.set_xmargin(0)
    ax.set_ymargin(0)
    ax.autoscale_view()

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    # Restore default matplotlib params
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)

    fig.savefig('out2.png', format='png', bbox_inches='tight', pad_inches=0)


def draw3(xserie, yserie):
    # Fixed autolimit padding in matplotlib 2.0.0
    matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
    matplotlib.rcParams['axes.xmargin'] = 0
    matplotlib.rcParams['axes.ymargin'] = 0

    fig = plt.figure(figsize=(8,3))
    ax = fig.add_subplot(111)

    ax.plot(xserie, yserie)
    ax.grid(True)
    remove_frame_border(ax)

    # Restore default matplotlib params
    matplotlib.rcParams.update(matplotlib.rcParamsDefault)

    fig.savefig('out3.png', format='png', bbox_inches='tight', pad_inches=0)



xserie = np.array([1,2,3,4,5,6,7,8,9,10])
yserie = np.array([0, 22.2, 33.4, 55.6, 66.6, 77.7, 100.3, 123.3, 90.4, 93.3])

draw1(xserie, yserie)
draw2(xserie, yserie)
draw3(xserie, yserie)
  • draw1是默认行为
  • 抽签2呼叫最大设定值以及斧头在
  • draw3更改rcParams并在生成图表后恢复它

out1.pngout1.png

out2.pngout2.png

out3.pngout3.png


Tags: pngmatplotlibdeffigpltaxframeremove
1条回答
网友
1楼 · 发布于 2024-10-01 00:30:54

Matplotlib提供方法^{}

Set padding of X data limits prior to autoscaling. m times the data interval will be added to each end of that interval before it is used in autoscaling.

如此设置

ax.set_xmargin(0) 

删除填充。在

然而,这似乎要产生效果,人们还需要打电话 ^{}

^{pr2}$

我不完全理解autoscale_view的docstring,但是这里的组合似乎可以工作。在

相关问题 更多 >